Tag: shellscript

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.
#!/bin/sh

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/db_backup.sh