Linux · February 8, 2016 0

Automate Database backups using cron/ssh

Cron is a right solution to take backup of your website database at certain intervals like take backup every day.

You can add following command into your cron for daily backup of your website.

To know about cronjob follow the link https://lempjs.com/2016/02/07/how-to-set-php-script-to-cronjob/

#automate backups
0 0 * * * /usr/bin/mysqldump -udbusrname -hdbhost -pdbpassword dbname | gzip -c  > /path/to/db-backup/dbname-`date +\%Y-\%m-\%d`.gz

Example:

0 0 * * * /usr/bin/mysqldump -uroot -hlocalhost -proot123 cd_collection | gzip -c  > /home/myuser/backups/db/cd_collection-`date +\%Y-\%m-\%d`.gz

Explanation
0 0 Don’t run at every minute or every hour
* * * Run every day of every month of every year
/usr/bin/mysqldump -uroot -hlocalhost -proot123 cd_collection | gzip -c Command to take backup of database and compress it as gzip
/path/to/db-backup/dbname-`date +\%Y-\%m-\%d`.gz Path to save the backup database file.

Save you cron and this will start taking backup every day.

Please add your comments and suggestions

Thanks.