Mysql backup script

Ensuring data integrity and security is paramount. Regular backups play a crucial role in safeguarding your MySQL databases against unforeseen disasters

#!/bin/bash
# Author: Arun N S

# Variables
DATE="$(date +"%d-%m-%Y")"
TIME="$(date +"%d-%m-%Y-%H%M")"
USER=username
PASSWORD=password
DATABASE=dbname

# Create backup directory with today's date
/bin/mkdir -p /backup/Mysql/$DATE

# Perform MySQL dump to the backup directory
/usr/bin/mysqldump -l -F -u $USER --password=$PASSWORD $DATABASE > /backup/Mysql/$DATE/backup_$TIME.sql

# Compress backup files
/usr/bin/bzip2 /backup/Mysql/*/*.sql

# Remove files older than x days (e.g., 90 days)
for i in `/usr/bin/find /backup/Mysql/ -maxdepth 1 -type d -mtime +90 -print`; do
    /bin/echo -e "Deleting old directories $i"
    /bin/rm -rf $i
done

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *