mysql的全量备份与增量备份
全量备份:可以使用mysqldump直接备份整个库或者是备份其中某一个库或者一个库中的某个表。
备份所有数据库:
[root@my ~]# mysqldump -uroot -p123456 --all-databases >/opt/all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
备份单个数据库的所有表:
[root@my ~]# mysqldump -uroot -p123456 --databases test>/opt/test.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
备份数据库中的单个表的结构:
[root@my ~]# mysqldump -uroot -p123456 -d test userinfo >/opt/test_userinfo_jiegou.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
备份数据库中的单个表的内容:
[root@my ~]# mysqldump -uroot -p123456 test userinfo>/opt/test_userinfo_neirong.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
增量备份:增量备份是针对于数据库的bin-log日志进行备份的,需要开始数据库的bin-log日志。增量备份是在全量的基础上进行操作的。增量备份主要是靠mysql记录的bin-log日志。(可以把二进制日志保存成每天的一个文件)
1.开启二进制日志文件;
[root@my ~]# echo -e "log-bin = /usr/local/mysql/logs/mysql_bin \nserver_id = 1\n max_binlog_size = 100M " >>/etc/my.cnf
根据position值位置进行恢复;
[root@my ~]# mysqlbinlog --start-position=1 --stop-position=795 /usr/local/mysql/logs/mysql_bin.000001 |mysql -uroot -p123456
根据时间点位置进行恢复;-d指定数据库,选项-h指定主机
[root@my ~]#mysqlbinlog --start-date='2019-05-30 14:49:25' --stop-date='2019-05-30 14:52:39' -d linux -h127.0.0.1 /usr/local/mysql/logs/mysql-bin.000001 |mysql -uroot -p123456
将二进制日志文件中所有的数据记录全部恢复;
[root@my ~]# mysqlbinlog /usr/local/mysql/logs/mysql_bin.000001 |mysql -uroot -p123123
脚本:
全量备份:
[root@calldb1 ~]# cat /shell/fs_sql_bak.sh
#!/bin/bash
##beifen sql
Bakdir=/data/sql-bak
Time=$(date +"%F_%T")
[ -f /usr/bin/mysqldump ] && echo "ok" || exit
/usr/bin/mysqldump -uroot -p123456 -R --all-databases > $Bakdir/$Time"_34.sql"
#echo $Bakdir/$Time"_34.sql"
/usr/bin/mysqldump dbname -uroot -p123456 > $Bakdir/$Time"-dbname.sql"
增量备份:
[root@calldb2 shell]# cat zeng_sql.sh
#!/bin/bash
## zeng liang bak sql
Logdir=/usr/local/mysql/logs
Time=$(date +%F)
mkdir $Logdir/"log-"$Time
find $Logdir -type f -mmin 1 -exec cp {} $Logdir/"log-"$Time/ \;
if [ -f $Logdir/"log-"$Time/mysql-bin.* ];then
echo "mysql is zeng backup success on time-$(date +%F)" >>$Logdir/"log-"$Time/mysql_zeng_backup.log
else
echo "mysql is zeng backup fail on time-$(date +%F)" >>$Logdir/"log-"$Time/mysql_zeng_backup.log
fi
mysqladmin -uroot -p123456 flush-logs >/dev/null
生产环境:
建议一周或者三天进行一次全量备份,一天一次增量备份。