The main reality of this script is to implement backup of MySQL database and keep backup files for the last 15 days in CentOS. Avoid too many useless, stale backup footprints.
#!/bin/bashId="Root" #User namePwd="123123" #Passworddbs="conedu Commlib" #a list of database names, with multiple databases separated by spaces. Backuppath="/root/mysqlbackup" #where to save the backupDay=15#keep backups for the last few days[!-D$backpath] &&mkdir-p$backuppath#determine if the backup directory exists, and create a new directory when it is not saved. Cd$backuppath #go to the backup directory, this sentence can be omitted. You can direct the path to the command line as well. backupname=mysql_$ (Date +%y-%m-%d)#generates a prefix for the name of the backup file without a suffix. forDbinch $dbs;#DBS is a collection of data names. Traverse all the data. Domysqldump-U$id-P$pwd-s/var/lib/mysql/mysql.sock$db>$backupname _$db. sql#backs up individual data to. sql files. Put it in the current positionif["$?"=="0"]#$? Gets the return value of the execution of the last shell command. 0 indicates successful execution. The other represents an error. and writes the results to the log. Then Echo$ (Date +%y-%m-%d)"$db mysqldump sucess">>Mysql.logElseEcho$ (Date +%y-%m-%d)"$db MySQL dump failed">>Mysql.log Exit0Fidonetar-czf$backupname. tar.gz *.sql#compress all SQL filesif["$?"=="0"]then Echo$ (Date +%y-%m-%d)"Tar sucess">>Mysql.logElseEcho$ (Date +%y-%m-%d)"Tar failed">>Mysql.log Exit0firm- F*.sql#Delete all the SQL filesdelname=mysql_$ (date-d"$day day ago"+%y-%m-%d). tar.gz#get the name of the backup that is too old to be deleted. Rm- F $delname#Delete the file.
As long as you add a row to the/etc/crontab, you can automatically back up the
Go MySQL automatically backs up and saves nearly 15 days of recording scripts