Simple database backup shellscript

I made a simple shellscript it will:

  • dump a few databases
  • create a tar.gz from them
  • scp a copy of the tar.gz to my backups folder on a┬áremote server, this way I will always have a backup of my most important databases on a remote location.

mysqldump db_name1 -u myUser -pMypassword > db_namedump1$(date +%F_%R).sql
mysqldump db_name1 -u myUser -pMypassword > db_namedump2$(date +%F_%R).sql
mysqldump db_name1 -u myUser -pMypassword > db_namedump3$(date +%F_%R).sql

FILENAME="compressed-$(date +%F_%R).tar.gz"
tar -czf ./backups/$FILENAME *.sql
rm ./*.sql
scp ./backups/$FILENAME [email protected]:backups/

Just save it in a file, and add it to your cronjob (crontab -l) I run it every 4 hours, by adding this to my crontab:

0 */4 * * * /home/user/


Leave a Reply

Your email address will not be published.