Backup MySQL database automatically with Cronjob on Ubuntu 22

Last updated:
WebDev
Setup Cronjob to backup MySQL database (everyday for example)

mysqldump

At home directory, create a file .my.cnf

Content

[mysqldump]
password=YourSecret

When run command mysqldump, it will auto look for password in ~/.my.cnf, no need to -p option.

Script

DATE=`date +%Y_%m%d` && 
BACKUP_PATH=/home/hungbeo/${DATE}_db.sql && 
mysqldump -u userName dbName > ${BACKUP_PATH} ;

Cronjob

crontab -e

Add this line to bottom

# Backup mysite database
0 0 * * * bash /path/to/your/backupScript.sh

backupScript.sh will be executed everyday at 0h00.

Comments

No comments!