Use shell to automatically back up mysql Databases and shellmysql databases at regular intervals
#! /Bin/bashid = "root" # username pwd = "123456" # password dbs = "blog.ewsd.cn dangjian.ewsd.cn idc.ewsd.cn shangfa.ewsd.cn" # list of database names, separate multiple databases with spaces. backuppath = "/home/mysqlbackup" # Save the backup location day = 30 # Keep the backup for the last few days [! -D $ backuppath] & mkdir-p $ backuppath # Check whether the backup directory exists. If no backup directory exists, create a new one. Cd $ backuppath # Go To The Backup Directory, which can be omitted. You can directly export the path to the command. Backupname = mysql _ $ (date + % Y-% m-% d) # prefix of the name of the backup file generated without the suffix. For db in $ dbs; # dbs is a set of data names. Traverse all data. Do mysqldump-u $ id-p $ pwd-S/tmp/mysql. sock $ db> $ backupname _ $ db. SQL # Back up a single data file as a. SQL file. Put it in the current location if ["$? "=" 0 "] # $? Returns the execution result of the previous shell command. 0 indicates that the execution is successful. Others indicate errors. And write the results to the log. Then echo $ (date + % Y-% m-% d) "$ db mysqldump sucess"> mysql. log else echo $ (date + % Y-% m-% d) "$ db mysql dump failed"> mysql. log exit 0 fidonetar-czf unzip backupname.tar.gz *. SQL # compress all SQL files if ["$? "=" 0 "] then echo $ (date + % Y-% m-% d)" tar sucess "> mysql. logelse echo $ (date + % Y-% m-% d) "tar failed"> mysql. log exit 0 firm-f *. SQL # Delete All SQL files delname = mysql _ $ (date-d "$ day ago" +%y-%m-%d=.tar.gz # obtain the name of the old backup to be deleted. Rm-f $ delname # delete an object.
Add the preceding script to the crontab of the scheduled task,Note the script execution permission settings.