Mysql backup/restore solution

Mysql backup scheme

Four backup schemes of Mysql reference:
http://www.cnblogs.com/SQL888/p/5751631.html

Here is the incremental plus full backup scheme
Reference : http://blog.csdn.net/ hijiankang/article/details/10999501

Full backup command:
uncompressed backup
mysqldump -uroot -p --single-transaction --flush-logs --master-data=2 --events --ignore-table=mysql.events -- all-databases --delete-master-logs >/data/mysql/backup/full_bk.sql;compress

backup
mysqldump -uroot -p --single-transaction --flush-logs --master-data=2 --events - -ignore-table=mysql.events --all-databases --delete-master-logs |gzip >/data/mysql/backup/db/full_bk_$(date +%Y%m%d%H%M%S) .tar.gz;

Note:
--flush-logs cuts the binlog, so you don't have to worry about how to distinguish the new data in the backup process.
--delete-master-logs Delete the backed up binlog files after

the . For remote backups, add the following:
-e --max_allowed_packet=4194304 --net_buffer_length=16384
max_allowed_packet and net_buffer_length should be consistent with the parameters on the server. Viewing method on the server:
mysql> show variables like 'max_allowed_packet';
mysql> show variables like 'net_buffer_length';

incremental backup:
mysqladmin -uroot -p flush-logs;


create a backup user
grant ALL on *.* to `backup`@`localhost` identified by '111111';

script

full_bk.sh

#! /bin/bash
/bin/su -s / bin/sh - mysql -c "mysqldump -ubackup -p111111 --single-transaction --flush-logs --master-data=2 --events --ignore-table=mysql.events --all-databases --delete -master-logs |gzip >/data/mysql/backup/db/full_bk_$(date +%Y%m%d%H%M%S).tar.gz" >> log_$(date +%Y-% m-%d_%H:%M:%S).log 2>& 1


increase_bk.sh

#! /bin/sh
/bin/su -s /bin/sh - mysql -c "mysqladmin -ubackup -p111111 flush-logs" >> increaseLog_$(date +%Y-%m-%d_%H:%M :%S).log

Set up timed tasks
crontab -e #Perform
full database backup at 3:00 every Saturday
00 3 * * 6 /home/mysql/full_bk.sh #Perform
incremental backup at 12:00 every day
00 12 * * * / home/mysql / full_bk.sh




database recovery

1. service mysql stop The following two steps are very important The slave on the DB machine is stopped to prevent data rewriting > SET sql_log_bin=OFF; #Turn off the  uncompressed restore of the binary log temporarily



















mysql -uroot -p < full_bk_20170824001553.sql

compress and restore
gunzip < db/full_bk_20170831071221.tar.gz | mysql -uroot -padmin


mysql> SHOW DATABASES; #Database recovery

Restore the data in the binlog file whose date is greater than the date of the last full database backup file
To restore the binlog-file binary log file:
mysqlbinlog --no-defaults /data/backup/mysql-bin.000007 /data/backup/mysql-bin.000008 /data/backup/mysql-bin.000009 | mysql -uroot -p123456
Restore from a certain (367) point:
mysqlbinlog --no-defaults --stop-position="367" mysql-bin.000001| mysql -uroot -p123456 test
Check that point first (use more to check)
[root @localhost mysql]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000002 --start-position="794" --stop-position="1055" | more
then resume:


[root@localhost mysql]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000002 --start-position="794" --stop-position="1055" | /usr/bin/mysql -uroot -p123456 test



mysql> SET sql_log_bin=ON; open binary log to

view master status
mysql> show master status;

update master information on slave machine.


Encounter problems:

slave startup reports this error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'Solution
:

stop slave;

reset slave;

start slave;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326116586&siteId=291194637