mysql增量备份脚本
#!/bin/bash
dateDIR=$(date -d "yesterday" +"%y-%m-%d")
mkdir -p /data/backup/mysql/binlog/$dateDIR
##刷新缓存中的日志
mysqladmin -uroot -p flush-logs
#7天前的时间
TIME=$(date "-d 7 day ago" +"%Y-%m-%d %H:%M:%S")
#24小时前的时间
StartTime=$(date "-d 1 day ago" +"%Y-%m-%d %H:%M:%S")
#删除7天前的2进制文件
mysql -uroot -p -e "purge master logs before '${TIME}';"
#针对每个数据库
for db in $(mysql -uroot -p -e "show databases" | grep -e "zabbixdatabases" -e"serverdatabases")
do
#针对所有的日志文件
for log in $(cat /var/lib/mysql/mysql-bin.index)
do
#备份每个数据库24小时之前到现在时间的所有日志,写入到相应文件中
mysqlbinlog -uroot -p -d $db --start-datetime="$StartTime" ${log} > /data/backup/mysql/binlog/$dateDIR/${db}_${dateDIR}.sql
done
#tar jcvf /data/backup/mysql/binlog/$dateDIR/${db}_${dateDIR}.tar.bz2 /data/backup/mysql/binlog/$dateDIR/${db}_${dateDIR} 2&>/dev/null
#rm -rf /data/backup/mysql/binlog/$dateDIR/${db}_${dateDIR}
done
#删除20天以前的增量备份文件和目录
find /data/backup/mysql/binlog/* -mtime +19 -type d -exec rm -rf {} \;
mysql全备份脚本
A:
[root@localhost ~]# cat /data/sh/login_back_mysql.sh
#!/bin/bash
##-------------------------------------------------------------- -----------
#Filename: login_mysql.sh
#Revision: 0.1
#Date: 2015/11/07
#Author: han
#Email: hz77@163.com
#Description: use mysqldump tools to backup mysql
#Notes:
#-------------------------------------------------------------- ----------
date=`date +%Y%m%d%H`
bakdir="/data/backup/back_mysql"
logFile="/home/backup/back_mysql/mysqlbak.log"
user="root"
pass="1234"
PORT=3307
MySQL_PORT=3306
if [ ! -d $bakdir ];then mkdir -pv $bakdir;fi
cd $bakdir
function backup_mysql {
for dbname in $1 $2 $3 $4 $5 $6 $7 $8 $9
do
mysqldump -u$user -p$pass -S /tmp/mysql${MYSQL_PORT}.sock --default-character-set=utf8mb4 --opt --master-data=2 --extended-insert=false --triggers -R --hex-blob --events --single-transaction --quick -B $dbname > $bakdir/${dbname}_${date}.sql
tar zcvf ${dbname}_${date}.sql.tar.gz ${dbname}_${date}.sql
rm ${dbname}_${date}.sql -f
#scp $bakdir/${dbname}_${date}.sql.tar.gz 192.168.0.100:/data/backup/db-1/ || scp $bakdir/${dbname}_${date}.sql.tar.gz
done
}
backup_mysql db1 db2 db3 db4 db5 db6 db7 db8
find /data/backup/back_mysql/ -ctime +20 -exec rm -fr {} \;
1 #!/bin/bash
2
3 date=`date +%Y-%m-%d`
4
5 bakdir="/home/mysqlbackup"
6 mvdir="/home/mysqlbackup/mv"
7 logFile="/home/mysqlbackup/log/mysqlbak.log"
8 user="root"
9 pass="passwd"
10 filename="database-$date.sql"
11 filenamee="databases-$date.sql"
12
13 if [ ! -d $bakdir ];then mkdir -pv $bakdir;fi
14
15 cd $bakdir
16 mv $bakdir/$filename.tgz $mvdir/
17 mv $bakdir/$filenamee $mvdir/
18 rm -f $bakdir/$filename
19
20 /home/mysql/bin/mysqldump --default-character-set=utf8 --opt --extended-insert=false --triggers -R --hex-blob --flush-logs --events --single-transaction --quick -B dabases -u$user -p$pass > $filename
21 /home/mysql/bin/mysqldump --default-character-set=utf8 --opt --extended-insert=false --triggers -R --hex-blob --flush-logs --events --single-transaction --quick -B databases -u$user -p$pass >
$filenamee 22
23 echo "开始打包备份文件"
24 echo "$filename $filenamee 数据库备份成功,备份文件在192.168.0.10:/root/home/mysqlbackup目录下;" > $logFile
25 /bin/tar czvf $filename.tgz $filename
26
27 /bin/find $mvdir -type f -ctime +3 -exec rm -fr {} \;
28 #/bin/tar czvf $filenamee.tgz $filename
29 scp $bakdir/$filename.tgz 192.168.0.10:/root/home/mysqlbackup
30 scp $bakdir/$filenamee 192.168.0.10:/root/home/mysqlbackup
31
32 mv $bakdir/$filename.tgz $mvdir/
33 mv $bakdir/$filenamee $mvdir/
34 rm -f $bakdir/$filename
35
36 ssh 192.168.0.10 '/bin/find /root/home/mysqlbackup -type f -atime +3 -exec rm -fr {} \;'
37 #只保留最近四天的数据
~
或者:
2 # Program
3 # # use mysqldump to Fully backup mysql data per week!
4 # # History
5 # # 2015-04-02 jin first
6 # # path
7 # # ....
8 # 备份文集的目录,日志目录,时间,用户,密码
9 BakDir="/root/djback/backup"
10 LogFile="/root/djback/backup/bak.log"
11 Date=`date +%Y%m%d`
12 Begin=`date +"%Y年%m月%d日 %H:%M:%S"`
13 user="root"
14 pass="passwd"
15 #如果文件不存在就创建
16 if [ ! -d $BakDir ];then mkdir -pv $BakDir;fi
17 cd $BakDir
18 #备份文件名字和压塑后的问件名
19 DumpFile=data-$Date.sql
20 GZDumpFile=data-$Date.sql.tgz
21 DumpFilee=databa-$Date.sql
22 GZDumpFilee=databa-$Date.sql.tgz
23 #备份qiv库
24 /home/mysql/bin/mysqldump --default-character-set=utf8 --opt --extended-insert=false --master-data=2 --triggers -R --hex-blob --flush-logs --events --single-transaction --quick -B data -u$user -p
$pass > $BakDir/$DumpFile 25 #备份qiv_secretcode库
26 /home/mysql/bin/mysqldump --default-character-set=utf8 --opt --extended-insert=false --master-data=2 --triggers -R --hex-blob --flush-logs --events --single-transaction --quick -B databa
e -u$user -p$pass > $BakDir/$DumpFilee 27 sed -n "22p" $BakDir/$DumpFilee | awk -F'=|,|;' '{print $2,$4}' >$BakDir/postion
28 # 压缩备份文件
29 /bin/tar czvf $GZDumpFile $DumpFile
30
31 /bin/tar czvf $GZDumpFilee $DumpFilee
32 # 删除以前的备份文件
33 /bin/rm $DumpFile
34 Last=`date +"%Y年%m月%d日 %H:%M:%S"`
35 echo "开始:$Begin 结束:$Last $GZDumpFile succ
36 sleep 2
37 echo "(备份成功 备份的目录为/root/djback/backup下)"
38 cd $BakDir/daily
39 rm -f *
40 echo "开始上传文件到192.168.0.10的/root/home/mysqlbackup/下"
41 scp /root/djback/$DumpFile 192.168.0.10:/root/home/mysqlbackup/
42 scp /root/djback/$DumpFilee 192.168.0.10:/root/home/mysqlbackup/
43 echo "上传成功请查看"
或者
[root@db-1 sh]# cat login_back_mysql.sh
#!/bin/bash
##-------------------------------------------------------------- -----------
#Filename: login_mysql.sh
#Revision: 0.1
#Date: 2015/11/07
#Author: hanye
#Email: hz77@163.com
#Description: use mysqldump tools to backup mysql
#Notes:
#-------------------------------------------------------------- ----------
date=`date +%Y%m%d%H`
bakdir="/data/backup/yx_back_mysql"
logFile="/home/backup/yx_back_mysql/mysqlbak.log"
user="root"
pass="passwd"
PORT=3307
MySQL_PORT=3306
if [ ! -d $bakdir ];then mkdir -pv $bakdir;fi
cd $bakdir
function backup_mysql {
for dbname in $1 $2 $3 $4 $5 $6 $7 $8 $9
do
mysqldump -u$user -p$pass -S /tmp/mysql${MYSQL_PORT}.sock --default-character-set=utf8mb4 --opt --master-data=2 --extended-insert=false --triggers -R --hex-blob --events --single-transaction --quick -B $dbname > $bakdir/${dbname}_${date}.sql
tar zcvf ${dbname}_${date}.sql.tar.gz ${dbname}_${date}.sql
rm ${dbname}_${date}.sql -f
scp $bakdir/${dbname}_${date}.sql.tar.gz 192.168.0.150:/data/backup/db-1/ || scp $bakdir/${dbname}_${date}.sql.tar.gz 192.168.0.151:/data/backup/db-1/
done
}
backup_mysql db_001 db_002 db_003 db_004 db_005 b_006 db_007 db_008
find /data/backup/yx_back_mysql/ -ctime +20 -exec rm -fr {} \;