≡ Menu

Backup Mysql with Bash Script


Here is how to perform backups on the mysql database by using a bash script:

First step just create a file backup.sh

vi backup.sh

and copy the following scripts into backup.sh file

# Database credentials
 user="databse user"
 password="database password"
 db_name="database name"
# Backup location
 date=$(date +"%d-%b-%Y")
# Set default file permissions
 umask 177
# Dump database into SQL file
 mysqldump --user=$user --password=$password --host=$host $db_name > $backup_path/$db_name-$date.sql
# Delete files older than 30 days
find $backup_path/* -mtime +30 -exec rm {} \;

Now you just need to make it executable:

chmod 700 backup.sh

And then add it to the crontab so it’ll run automagically:

crontab -e
0 1 * * * /path/to/backup.sh

In this case it’ll run every day at 1 AM.

{ 0 comments… add one }

Leave a Comment