Automatic backup of mysql scripts

Source: Internet
Author: User
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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.