Create the directory mkdir/usr/local/dbbak for storing the backup file: vi/usr/local/mysqlback. sh #/bin/bashDB_NAME = & quot; dsideal_db & quot; DB_USER = & quot; root & quot; DB_PASS = & quot; 123456 & quot; BACK_D
Create a directory for storing backup files
mkdir /usr/local/dbbak
Script: vi/usr/local/mysqlback. sh
#/Bin/bashDB_NAME = "dsideal_db" DB_USER = "root" DB_PASS = "123456" BACK_DIR = "/usr/local/dbbak" DATE = "$ DB_NAME-'date + '% y-% m-% d = % H: % M: % s' "LogFile =" $ BACK_DIR "/dbbakup. log # the directory in which the log is saved: BackNewFile = $ DATE. sqlmysqldump -- opt -- force-u $ DB_USER-p $ DB_PASS $ DB_NAME> $ BACK_DIR/$ DATE. sqlecho ----------------------- "$ (date +" % y-% m-% d % H: % M: % S ")" ---------------------> $ LogFileecho createFile: "$ BackNewFile"> $ LogFilefind "/usr/local/dbbak/"-mtime + 7-type f-name "*. SQL "-print> deleted.txt echo-e" delete files: \ n ">>$ LogFile # cyclically delete the matched file cat deleted.txt | while read LINEdo rm-rf $ LINE echo $ LINE >>$ LogFiledoneecho" >>> $ LogFile
Add permissions to the sh script
chmod 777 /usr/local/mysqlback.sh
Use crontab to regularly execute the task at every morning.
crontab -e0 03 * * * /usr/local/mysqlback.sh > /dev/null 2>&1
[Remarks] simple backup
mysqldump --databases test --flush-logs> /usr/local/backup-`date +%F-%H-%M`.sql