標籤:linux mysql 備份 shell
#!/bin/bash# mysql 的備份指令碼# 備份原理:# 1# 使用列舉出所有的庫;# 2# 使用每個庫,列舉出每張表,除了指定忽略的庫;# 3# 使用mysqldump 匯出每一張表到檔案:主機名稱/年月日/庫/表.mysqldump.sql# 4# 驗證每張表的sql檔案是否包含完成標誌;# 5#壓縮每個sql檔案並刪除本sql檔案# 6#強制移除超過x天的備份檔案夾全部檔案# 7#發送處理日誌到指定email# 8# 請配合約步工具多處伺服器備份# mysql備份配置資訊mysqlBackupUser="backuper"# 密碼不要包含"號mysqlBackupPwd="**#&&&#ddddddd("# 記錄檔路徑/var/log/檔案名稱.log,只記錄每次啟動並執行日誌# 不備份的資料庫名稱,每個名稱使用()號包住,如不備份 abc.d 和 abc.e二個資料庫,就拼字成"(abc.d)(abc.e)",名字不區分大小寫notBackupDatabases="(mysql)(information_schema)(performance_schema)"#備份的目錄,後面需要加/backupRoot="/var/backup/hostname-mysql-data/"# 刪除存在大於以下天數的備份目錄deleteRootOutDays=30#必須是完整的email地址,因為正面的命令使用到smtpUser="[email protected]"#smtp://協議是必須的smtpHost="smtp://smtp.qq.com:25"#密碼不能包含又引號防止shell出錯smtpPwd="pwd"smtpTo="[email protected]"smtpSubject="主機上的mysql自動備份指令碼執行資訊"# 配置結束行shName=$(basename $0)shLogPath="/var/log/${shName}.log"ver=$(realpath --version 2>&1)if [ "$?" -ne "0" ];then echo "測試realpath --version的版本時出錯,中止,出錯資訊:${ver}" exit 10fiecho -e "$(date "+%Y-%m-%d %R:%S") By $(realpath ${0})\n" > $shLogPathfunction myExit(){ exitCode=$1 ver=$(mailx -V 2>&1) appendLog "退出時間:$(date +%Y-%m-%d/%R:%S)" appendLog "伺服器資訊:\n$(ifconfig 2>&1)" if [ "$?" -ne "0" ];then appendLog "測試用來發送email的命令mailx時出錯,請安裝,如centos使用yum install mailx,忽略發送email通知的步驟,出錯資訊:${ver}" else #發送email mailInfo=$(mailx -v -s "${smtpSubject}" -S from="${smtpUser}" -S smtp-auth="login" -S smtp="${smtpHost}" -S smtp-auth-user="${smtpUser}" -S smtp-auth-password="${smtpPwd}" -S ssl-verify=ignore "${smtpTo}" < $shLogPath 2>&1) # 無法附加發送過程的日誌給email通知中,所以,只能儲存到日誌中,如果需要瞭解email的互動過程,請到記錄檔中查看 appendLog "退出時間到發送email的時間:$(date +%Y-%m-%d/%R:%S)\n使用mailx發送emial通知互動如下:\n\n${mailInfo}" fi exit $exitCode}# 追加日誌function appendLog(){ echo -e "${1} \n" >> $shLogPath}if [ ! -e "${backupRoot}" ];then appendLog "備份根目錄 ${backupRoot} 不存在,已建立" mkInfo=$(mkdir -p $backupRoot 2>&1) if [ "$?" -ne "0" ];then appendLog "嘗試建立備份目錄 ${backupRoot}失敗:${mkInfo}" myExit 1 fielif [ ! -d "${backupRoot}" ];then appendLog "備份根目錄路徑雖然存在,但是它不是目錄,中止:${backupRoot}" myExit 2fi#今天的備份目錄todayRoot="${backupRoot}$(date +%Y%m%d%H)/"if [ ! -e "${todayRoot}" ];then mkInfo=$(mkdir $todayRoot 2>&1) if [ "$?" -ne "0" ];then appendLog "嘗試建立本輪的備份目錄 ${todayRoot} 失敗,中止:${mkInfo}" myExit 3 fifiappendLog "今天的備份目錄:${todayRoot}"ver=$(mysql --version 2>&1)if [ "$?" -ne "0" ];then appendLog "測試mysql的版本時出錯,中止,出錯資訊:${ver}" myExit 4fiver=$(mysqldump -V 2>&1)if [ "$?" -ne "0" ];then appendLog "測試mysqldump命令出錯,請安裝,中止,出錯資訊:${ver}" myExit 5fiver=$(tail --version 2>&1)if [ "$?" -ne "0" ];then appendLog "測試tail命令的版本時出錯,中止,出錯資訊:${ver}" myExit 41fiver=$(tar --version 2>&1)if [ "$?" -ne "0" ];then appendLog "測試tar命令的版本時出錯,中止,出錯資訊:${ver}" myExit 42fidatabases=$(mysql --host="127.0.0.1" --user="${mysqlBackupUser}" --password="${mysqlBackupPwd}" --execute="show databases;" --silent --skip-column-names --unbuffered 2>&1)if [ "$?" -ne "0" ]; then appendLog "嘗試使用配置資訊列舉mysql的資料庫名時出錯,中止:${databases}" myExit 6else appendLog "全部的資料庫名稱列表如下:\n ${databases}"fifor database in $databases; do # 匹配時不區分大小寫 echo $notBackupDatabases|grep -i "(${database})" >/dev/null # 屬於不需要備份的庫 if [ "$?" -eq "0" ];then appendLog "資料庫 ${database} 被指定不需要備份,跳過" continue fi databaseRoot="${todayRoot}${database}/" if [ ! -e "${databaseRoot}" ];then mkInfo=$(mkdir $databaseRoot 2>&1) if [ "$?" -ne "0" ];then appendLog "嘗試建立資料庫 ${databaseRoot} 的目錄失敗,中止:${mkInfo}" myExit 7 fi fi tables=$(mysql --host="127.0.0.1" --user="${mysqlBackupUser}" --password="${mysqlBackupPwd}" --execute="show tables from \`${database}\`;" --silent --skip-column-names --unbuffered 2>&1)if [ "$?" -ne "0" ]; then appendLog "嘗試使用配置資訊列舉mysql的資料庫 ${database} 表的列表時出錯,中止:${tables}" myExit 8else appendLog "${database}資料庫表的全部列表如下:\n ${tables}"fi for table in $tables; do sqlPath="${databaseRoot}${table}.sql" timeStart="開始dump時間點: $(date +%Y-%m-%d/%R:%S)" dumpInfo=$(mysqldump --host="127.0.0.1" --user="${mysqlBackupUser}" --password="${mysqlBackupPwd}" --dump-date --comments --quote-names --result-file="${sqlPath}" --quick --databases "${database}" --tables "${table}" 2>&1 ) timeEnd="完成dump時間點: $(date +%Y-%m-%d/%R:%S)" if [ "$?" -ne "0" ];then appendLog "嘗試匯出資料庫 ${database}的表 ${table} 失敗,中止:${dumpInfo}" myExit 9 else appendLog "匯出資料庫 ${database}的表 ${table} 到 ${sqlPath} 成功:${dumpInfo}; 耗時: 從 ${timeStart} 至 ${timeEnd}" tail --lines=10 "${sqlPath}" |grep "\-\- Dump completed" 2>&1 > /dev/null if [ "$?" -ne "0" ];then appendLog "沒有發發現內容中的 'Dump completed' 正常完成標誌字元,請檢查dump檔案${sqlPath},請登入ssh查看此檔案是否備份成功" else appendLog "檢測到備份檔案內容中的'Dump completed'標誌字元,dump檔案${sqlPath}應該備份成功了" fi fi donedoneappendLog "\n ------Database Backup全部完成------\n"# 開始壓縮,把壓縮放到備份結束是防止壓縮時間過長,如果出現鎖表,會影響網站運行sqls=$(ls --almost-all --ignore-backups --indicator-style=slash -1 ${todayRoot}*/*.sql 2>&1)for path in $sqls; do #路徑中包含了絕對路徑,tar命令還需要改進 sqlDir=$(dirname $path) sql=$(basename $path) tarInfo=$(tar --create --remove-files --bzip2 --absolute-names --directory="${sqlDir}" --add-file="${sql}" --file="${path}.tar.bz2") if [ "$?" -ne "0" ];then appendLog "壓縮並刪除${path}檔案時出錯:\n${tarInfo}" else appendLog "已壓縮並刪除${path}" fidoneappendLog "------完成壓縮操作-----"#開始清理大於x天的備份daysDir=$(ls --almost-all --ignore-backups --indicator-style=slash -1 "${backupRoot}" 2>&1)for bkDir in $daysDir;do bkDir="${backupRoot}${bkDir}" if [ ! -d "${bkDir}" ];then appendLog "準備刪除到期的備份操作時,因為${bkDir}不是一個目錄,跳過" continue fi dirName=$(basename $bkDir) #test echo $dirName | grep -P "^\d{10}$" 2>&1 >/dev/null if [ "$?" -ne "0" ];then appendLog "準備刪除到期備份目錄時,檢測到待刪除目錄名不是10位元字,跳過:${bkDir}" continue fi outDay=$(date --date="-${deleteRootOutDays}day" "+%Y%m%d00") #如果檔案時間小於這個到期時間那麼就強制移除整個目錄 if [ "${dirName}" -lt "${outDay}" ];then rmInfo=$(rm --force --preserve-root --recursive "${bkDir}" 2>&1) appendLog "發現一個到期備份目錄 ${bkDir},已經超過 ${deleteRootOutDays} 天,也就是小於${outDay}就會被刪除 ,被強制移除狀態(0為成功):${?} ${rmInfo};" fidoneappendLog "------完成清理到期備份檔案夾操作----"appendLog "空間使用方式如下:\n $(df -h)"appendLog "當前備份檔案佔用空間情況:\n $(du -hs ${todayRoot})"myExit 0
linux下的mysql自動備份shell