MySQL/MariaDB Backup Cron 1-liner

a nullvoid blog

MySQL/MariaDB Backup Cron 1-liner

Here’s a quick one-liner you can put in your crontab to dump and compress a MySQL or MariaDB database and keep the previous backup. Adjust accordingly.

0 5 * * * cd /home/user/backups/ && mv mysql-database-backup.gz mysql-database-backup-last.gz; mysqldump --host localhost -u root -pPASSWORD DATABASE | gzip > /home/user/backups/mysql-database-backup.gz

This will run at 5AM every day, rename the old backup filename and dump and gzip the new backup.

Note there is no space between -p and your password.

To restore the backup you would use the following example:

[user@linux ~]$ gunzip < /home/user/backups/mysql-database-backup.gz | mysql -u root -pPASSWORD DATABASE

Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *