xtrabackup: Creating suspend file ‘/backup/mysql/data/2017-04-04_12-46-24/xtrabackup_log_copied’ with pid ‘21223’
xtrabackup: Transaction log of lsn (1639325) to (1639325) was copied.
170404 12:46:30 innobackupex: All tables unlocked
innobackupex: Backup created in directory ‘/backup/mysql/data/2017-04-04_12-46-24’
innobackupex: MySQL binlog position: filename ‘mysql-bin.000019’,position 967
170404 12:46:30 innobackupex: Connection to database server closed
170404 12:46:30 innobackupex: completed OK!
出现上面的信息,表示备份已经ok.
上面执行的备份语句会将mysql数据文件(即由my.cnf里的变量datadir指定)拷贝至备份目录下(/backup/mysql/data)
注意:如果不指定–defaults-file,默认值为/etc/my.cnf. 备份成功后,将在备份目录下创建一个时间戳目录(本例创建的目录为/backup/mysql/data/2017-04-04_12-46-24),在该目录下存放备份文件.
[root@master data]# ll /backup/mysql/data/
总用量 4
drwxr-xr-x 6 root root 4096 4月 4 16:56 2017-04-04_16-56-35
[root@master data]# ll 2017-04-04_16-56-35/
总用量 18468
-rw-r–r– 1 root root 188 4月 4 16:56 backup-my.cnf
-rw-r—– 1 root root 18874368 4月 4 16:56 ibdata1
drwxr-xr-x 2 root root 4096 4月 4 16:56 mysql
drwxr-xr-x 2 root root 4096 4月 4 16:56 performance_schema
drwxr-xr-x 2 root root 4096 4月 4 16:56 test
-rw-r–r– 1 root root 13 4月 4 16:56 xtrabackup_binary
-rw-r–r– 1 root root 23 4月 4 16:56 xtrabackup_binlog_info
-rw-r—– 1 root root 89 4月 4 16:56 xtrabackup_checkpoints
-rw-r—– 1 root root 2560 4月 4 16:56 xtrabackup_logfile
drwxr-xr-x 2 root root 4096 4月 4 16:56 xtra_test
还可以在远程进行全量备份,命令如下:
innobackupex –defaults-file=/etc/my.cnf –user=root –host=127.0.0.1 –parallel=2 –throttle=200 /backup/mysql/data 2>/backup/mysql/data/bak.log 1>/backup/mysql/data/`data +%Y-%m-%d_%H-%M%S`
参数解释:
–user=root 备份操作用户名,一般都是root用户
–host=127.0.0.1 主机ip,本地可以不加(适用于远程备份).注意要提前在mysql中授予连接的权限,最好备份前先测试用命令中的用户名、密码和host能否正常连接mysql.
–parallel=2 –throttle=200 并行个数,根据主机配置选择合适的,默认是1个,多个可以加快备份速度.
/backup/mysql/data 备份存放的目录
2>/backup/mysql/data/bak.log 备份日志,将备份过程中的输出信息重定向到bak.log
这种备份跟上面相比,备份成功后,不会自动在备份目录下创建一个时间戳目录,需要如上命令中自己定义.
[root@master src]# ll /backup/mysql/data/
总用量 8
drwxr-xr-x 6 root root 4096 4月 4 12:46 2017-04-04_12-46-24
-rw-r–r– 1 root root 106 4月 4 12:57 bak.log //备份信息都记录在这个日志里,如果备份失败,可以到这里日志里查询
2)全量备份后的恢复操作
#进入数据库
[root@master data]# mysql
#查看当前所有数据库
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| test |
| xtra_test |
+——————–+
5 rows in set (0.09 sec)
#切换到 xtra_test数据库下
mysql> use xtra_test;
Database changed
#查看当前数据库里的表
mysql> show tables;
+———————+
| Tables_in_xtra_test |
+———————+
| I |
| M |
+———————+
2 rows in set (0.04 sec)
#删除整个 xtra_test库
mysql> drop database xtra_test;
Query OK,2 rows affected (0.34 sec)
#现在已经看不到 xtra_test库了
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| test |
+——————–+
4 rows in set (0.00 sec)
注意:恢复之前
1)要先关闭数据库
2)要删除数据文件和日志文件(也可以mv移到别的地方,只要确保清空mysql数据存放目录就行)
[root@master data]# ps -ef|grep mysqld
root 10929 1 0 10:32 pts/0 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe –datadir=/application/mysql/data –pid-file=/application/mysql/data/master.pid
mysql 11227 10929 0 10:32 pts/0 00:00:14 /application/mysql/bin/mysqld –basedir=/application/mysql –datadir=/application/mysql/data –plugin-dir=/application/mysql/lib/plugin –user=mysql –log-error=/application/mysql/data/master.err –pid-file=/application/mysql/data/master.pid –port=3306
root 21514 1896 0 13:55 pts/0 00:00:00 grep mysqld
由上面可以看出mysql的数据和日志存放目录是/application/mysql/data
#关闭MySQL数据库服务
[root@master data]# service mysqld stop
Shutting down MySQL…. SUCCESS!
#移动数据文件和日志文件到/tmp(当然删除也可以)
[root@master data]# mv /application/mysql/data/* /tmp/
[root@master data]# innobackupex –defaults-file=/etc/my.cnf –user=root –use-memory=1G –apply-log /backup/mysql/data/2017-04-04_13-04-05/
[root@master data]# innobackupex –defaults-file=/etc/my.cnf –user=root –copy-back /backup/mysql/data/2017-04-04_13-04-05/
[root@master ~]# chown -R mysql.mysql /application/mysql/data/
可能报错:
sh: xtrabackup: command not found
innobackupex: Error: no ‘mysqld’ group in MySQL options at /home/mysql/admin/bin/percona-xtrabackup-2.1.9/innobackupex line 4350.
解决:将xtrabackup_55复制成xtrabackup即可
[root@master src]# ls /home/mysql/admin/bin/percona-xtrabackup-2.1.9/
innobackupex xbstream xtrabackup_55 xtrabackup_innodb55
[root@master src]# cd /home/mysql/admin/bin/percona-xtrabackup-2.1.9/
[root@master percona-xtrabackup-2.1.9]# cp xtrabackup_55 xtrabackup
[root@master percona-xtrabackup-2.1.9]# ls
innobackupex xbstream xtrabackup xtrabackup_55 xtrabackup_innodb55
检验:执行之后就OK了
[root@master percona-xtrabackup-2.1.9]# innobackupex –defaults-file=/etc/my.cnf –user=root –copy-back /backup/mysql/data/2017-04-04_13-04-05/
(编辑:ASP站长网)
|