This article describes how to automatically back up MySQL data in Linux, and provides basic methods for using shell scripts for full backup and incremental backup. For more information, see section 1. create a database backup directory first:
mkdir backupcd backupmkdir mysqlbackupcd mysqlbackup
2. create a backup script
vi mysqlautobackup
3. Compile the script:
Filename = 'date + % Y % m % d'/mysql bin directory/mysqldump -- opt database name-u (mysql account)-p (mysql password) | gzip>/directory to which the backup is saved/name1_filename.gz
Note: gzip compression and name can be written at will. there is no space or parentheses between-u and mysql accounts, and The-p and mysql passwords are also used.
4. set the script as executable:
chmod +x autobackup
5. write a scheduled task:
Crontab-e01 01 ***/bin/bash/script directory/mysqlautobackup
Execute the script at every morning.
Restart a scheduled task:
/etc/rc.d/init.d/crond restart
So far, all the steps have been completed.
Some common skills
1. create the path to save the backup file/mysqldata
#mkdir /mysqldata
2. create the/usr/sbin/baktest file.
Enter the following content:
mysqldump -uroot -proot test | gzip > /mysqldata/test`date +%Y-%m-%d_%H%M%S`.sql.gz
3. Modify file attributes so that they can be executed
#chmod +x /usr/sbin/baktest
4. modify/etc/crontab
Add
01 3 * * * root /usr/sbin/baktest
Indicates that the backup is performed at three o'clock every day.
5. restart crond.
#/etc/rc.d/init.d/crond restart
Complete.
PS:
Full backup script:
#! /Bin/bash # Name: qbk full backup script # mysql qbk scripts # By zxsdw.com # Last Modify: # define the script storage path # scriptsDir =/usr/sbin # define the user name and password user = rootuserPWD = password # define the database to be backed up = database # define the full backup file storage path bakDir =/backup/mysql # eMailFile = $ bakDir/email.txt # eMail = admin@zxsdw.com # define the log file LogFile = $ bakDir/mysqlbak. logDATE = 'date + % Y % m % d' echo ""> $ LogFileecho "" >>$ LogFileecho "--------------------------" >>$ LogFileecho $ (date + "% y- % M-% d % H: % M: % S ")> $ LogFileecho "-----------------" >>$ LogFilecd restart mysqldump -- flush-logs-u $ user-p $ userPWD -- quick $ database | gzip> $ DumpFileecho "Dump Done"> $ LogFileecho "[$ DumpFile] Backup Success! ">>$ LogFiledaily_databakDir = $ bakDir/daily_backupcd $ bakDir/daily_backupfind $ daily_databakDir-name" daily * "-type f-mtime + 35-exec rm {}\;> /dev/null 2> & 1
Incremental Backup Script
#! /Bin/bash # Name: zbk incremental backup # mysql zbk scripts # By zxsdw.com # Last modify: # define database username and password user = rootuserPWD = password # define database = database # generate a new mysql-bin.00000X file, if the err log is cleared, a new one is automatically created. /Usr/local/mysql/bin/mysqladmin-u $ user-p $ userPWD flush-logs # define the incremental backup location daily_databakDir =/backup/mysql/daily_backup # define the MYSQL data log Directory mysqlDataDir =/usr/local/mysql/var # defines the incremental log and directory eMailFile = $ daily_databakDir/email.txt # eMail = admin@zxsdw.com # defines the variable DATE format as 20150127 DATE = 'date + % Y % m % d' # define a total logFile log logFile = $ daily_databakDir/mysql $ DATE. log # beautify log template echo ""> $ eMailFileecho "-----------------------" >>$ eMailFile # time format Is 15-01-27 01: 06: 17 echo $ (date + "% y-% m-% d % H: % M: % S")> $ eMailFileecho "-----------------------"> $ eMailFile # defines the time range for deleting bin logs, the format is 20150124010540 TIME = $ (date "-d 3 day ago" + % Y % m % d % H % M % S) # defines the TIME range for incremental backup data, format: 2015-01-26 01: 04: 11 StartTime = $ (date "-d 1 day ago" + "% Y-% m-% d % H: % M: % S ") ########## start the Delete operation to beautify the log title ############## echo "Delete 3 days before the log" >>$ eMailFile # Delete the binfile three days ago, and update index records in the index to beautify the log title mysq L-u $ user-p $ userPWD-e "purge master logs before $ {TIME}" & echo "delete 3 days before log" | tee-a $ eMailFile # Search for index bin2 base file in the index and assign it to I. Filename = 'cat $ mysqlDataDir/mysql-bin.index | awk-F "/" '{print $2} ''for I in $ filenamedo ######## start incremental backup operation, beautification log title ########## echo "$ StartTime start backup binlog"> $ eMailFile # use mysqlbinlog to back up the added data one day ago, decompress and package gzip to the incremental backup directory/usr/local/mysql/bin/mysqlbinlog-u $ user-p $ userPWD-d $ database -- start-datetime = "$ StartTime" $ mysqlDataDir/$ I | gzip >>$ daily_databakDir/daily$DATE. SQL .gz | tee-a $ eMailFiledone # if the preceding backup Run the following delete script if [$? = 0] then # Delete the incremental log backup file of mtime> 32 find $ daily_databakDir-name "*. log "-type f-mtime + 32-exec rm {}\;> /dev/null 2> & 1cd $ daily_databakDirecho "Daily backup succeed"> $ eMailFileelseecho "Daily backup fail" >>$ eMailFile # mail-s "MySQL Backup" $ eMail <$ eMailFile # send email notification after backup failure # fi end IF judgment fi # replace the content of the variable eMailFile with the content of logFile cat $ eMailFile> $ logFile # IF the above IF judgment fails, run the "find $ daily_databakDir-name" * command again to delete the incremental log backup file of mtime> 32 "*. log "-type f-mtime + 32-exec rm {}\;> /dev/null 2> & 1 rsync-vzrtopg -- delete -- progress -- password-file =/usr/local/rsync. passwd root @ $ ip:/zxs/allimg/$ (date-d-1day + % y % m % d) /zxs/allimg/gunzip </backup/mysql/daily_backup/ceshi. SQL .gz |/usr/local/mysql/bin/mysql-u user name-p password database name -- force parameter ignored error