Bash script to take Mysqldump hourly and daily

Create a script as below and make it executable:

Now_hourly=$(date +%d-%b-%H_%M)
Now_daily=$(date +%d-%b-daily)
if [ "$1" == "hourly" ]; then
/usr/bin/mysqldump --all-databases | gzip -c | cat > /Backup_location/mysql_dump_all$Now_hourly.sql.gz
elif [ "$1" == "daily" ]; then
/usr/bin/mysqldump --all-databases | gzip -c | cat > /Backup_location/mysql_dump_all$Now_daily.sql.gz
echo "Error. Enter hourly or daily";

Now add the script in crontab for the hours it should run.

15,30 1,3 * * * script_name hourly
10,20 2,4 * * * script_name daily

Hourly backup will run at 01:15,01:30,03:15 and 03:30

Daily backup will run at 02:10,02:20,04:10 and 04:20 (since name would be same, it will replace each daily backup before)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s