#!/bin/bash
Dbname= User name #DB数据库
Backup_dir=/data/mysqlbackup/databackup #备份目录
dbrootpwd= Password #DB用户名root密码:
Expired_days=7 #BD备份存储7天
logfile= $backup _dir/db.log #日志文件
dumpfile= $backup _dir/db_lelaohui_$ (date +%y%m%d_%h). SQL #备份数据库文件
newfile= $backup _dir/db_lelaohui_$ (date +%y%m%d_%h). tgz #新增备份数据库文件
oldfile= $backup _dir/db_lelaohui_$ (date +%y%m%d--date= "$expired _days days Ago") *.tgz #旧的备份文件 "7 day ago" means 7 hours ago
[!-e "$backup _dir"] && mkdir-p $backup _dir #判断备份目录/data/mysqlbackup/databackup does not exist, then create a new backup directory
If [-n "' ls $OldFile 2>/dev/null '"];then #当串的长度大于0时为真 (string non-null), the 2>/DEV/NULL representative does not output an error message.
/bin/rm-f $OldFile
echo "[$OldFile] Delete old File Success" >> $LogFile
Else
echo "[$OldFile] Delete old Backup File" >> $LogFile
Fi
If [-E "$NewFile"];then #如果新文件存在, then the output log is then not backed up
echo "' Date +%y%m%d_%h ': [$NewFile] The backup File is exists, Can ' t Backup ' >> $LogFile
Else #否则创建新的备份文件, go to the backup path and compress the new backup database file. tgz, delete the backup. sql file.
/usr/bin/mysqldump-uroot-p$dbrootpwd-h10.66.126.90--opt--databases Dinner LLH llh_bomb > $DumpFile
CD $backup _dir
Tar czf $NewFile ${dumpfile##*/} >> $LogFile 2>&1 #${file##*/}: Delete the last/and left string: My.file.txt compress new Back up the database, and delete the. SQL database
echo "' Date +%y%m%d_%h ': [$NewFile] Backup Success" >> $LogFile #追加打印备份成功的日志
/bin/rm-f $DumpFile
Fi
MySQL backup script