Cold standby:
cd / data
tar cjvf /tmp/mysql_bak.tar.bz2 ./
Hot standby (logical backup):
# mysqldump -uroot -p456 test t1 t2> /tmp/t1.sql -- backup single or multiple tables # mysql -uroot -p123 test < /var/tmp/test_t1.sql restore the t1 and t2 tables of the test library # mysqldump -uroot -p456 --databases test test1 test2 > /tmp/test.sql --backup multiple databases # mysqldump -uroot -p456 --all-databases > /tmp/all.sql --backup all databases reduction: Restore a table: mysql> use test; mysql> drop table t1; # mysql -uroot -p456 test < /tmp/t1.sql # mysql -uroot -p456 -e "use test; show tables;" +----------------+ | Tables_in_test | +----------------+ | imptest | | t1 | +----------------+ Restore a library: mysql> drop database test; # mysql -uroot -p456 -e "create database test;" # mysql -uroot -p456 test < /tmp/test.sql # mysql -uroot -p456 -e "show databases;" +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | ## +--------------------+
Warm up at 4 a.m. every day
0 4 * * * /usr/bin/mysqldump -uroot -p -B -F -R -x --master-data=2 wiki|gzip >/opt/backup/wiki_$(date +%F).sql.gz Parameter Description: - B: Specify the database - F: Refresh the log - R: Backup stored procedures, etc. - x: Lock table --master-data: Add the CHANGE MASTER statement and the binlog file and location point information to the backup statement
Incremental backup:
mysql> show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]; Parameter explanation: IN ' log_name ' : specify the name of the binlog file to be queried (if not specified, it is the first binlog file) FROM pos: Specify which pos starting point to start from (if you don't specify it, it starts from the first pos point of the entire file) LIMIT [offset,] : offset (0 if not specified) row_count : The total number of queries (all rows if not specified) mysql> show master logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 695 | | mysql-bin.000002 | 106 | +------------------+-----------+ mysql> show binlog events in 'mysql-bin.000001'\G; *************************** 1. row *************************** Log_name: mysql-bin.000001 Pos: 4 Event_type: Format_desc Server_id: 1 End_log_pos: 106 Info: Server ver: 5.1.73-log, Binlog ver: 4 *************************** 2. row *************************** Log_name: mysql-bin.000001 Pos: 106 Event_type: Query Server_id: 1 End_log_pos: 244 Info: SET PASSWORD FOR 'root'@'localhost'='*22DF0FE262826C66C3A1E8BC8DC2AF2259B42F46' *************************** 3. row *************************** Log_name: mysql-bin.000001 Pos: 244 Event_type: Query Server_id: 1 End_log_pos: 359 Info: CREATE USER 'slave'@'172.16.147.%' IDENTIFIED BY 'slave' *************************** 4. row *************************** Log_name: mysql-bin.000001 Pos: 359 Event_type: Query Server_id: 1 End_log_pos: 510 Info: GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'172.16.147.%' *************************** 5. row *************************** Log_name: mysql-bin.000001 Pos: 510 Event_type: Query Server_id: 1 End_log_pos: 585 Info: flush privileges *************************** 6. row *************************** Log_name: mysql-bin.000001 Pos: 585 Event_type: Query Server_id: 1 End_log_pos: 695 Info: create database arrow charset utf8 6 rows in set (0.00 sec)
Recovery with binlog log
mysql> flush logs; #Generate a new binlog log # mysqlbinlog --start-position=190 --stop-position=833 mysqld.000001|mysql -uroot -p147258369 # mysqlbinlog --start-position=1554 --stop-datetime="2011-09-02 11:48:10" mysqld.000001 | mysql -uroot -p147258369
# /usr/bin/mysqlbinlog --stop-position=391 --database=wiki /var/lib/mysql/mysql-bin.000003 | /usr/bin/mysql -uroot -p'147258369' -v wiki ## 106-280