• MySQL——备份和还原


    备份

    热备

    MySQL服务在运行的时候进行的备份 

    mysqldump命令

    1. mysqldump --databases db1 db2 db3 > dump.sql
    2. mysqldump -uroot -p'Sanchuang1234#' --all-databases >all_db.sql
    3. mysqldump -uroot -p'Sanchuang123#' --databases TENNIS >/backup/tennis.sql
    4. mysqldump -uroot -p'Sanchuang123#' TENNIS PLAYERS >tennis_players.sql

    冷备

    即关闭MySQL服务,不关闭机器时,进行的备份

    cp tar scp rsync命令

    异地备份

    配置ssh免密码登录

    写脚本

    1. [root@wudang-mysql-2 mysql]# cat backup_db.sh
    2. #!/bin/bash
    3. #得到时间
    4. ctime=$(date +%F_%H%M%S)
    5. #在本地新建存放目录/backup_db
    6. mkdir -p /backup_db
    7. #备份TENNIS库到/backup_db叫tennis.sql
    8. mysqldump -uroot -pSanchuang1234# TENNIS >/backup_db/${ctime}_tennis.sql
    9. #备份mysql库里user表
    10. mysqldump -uroot -pSanchuang1234# mysql user >/backup_db/${ctime}_mysql_user.sql
    11. #备份服务器上新建文件夹/backup_db
    12. ssh root@192.168.0.136 mkdir -p /backup_db
    13. #上传当天备份的文件到备份服务器里192.168.0.136
    14. scp /backup_db/${ctime}*.sql root@192.168.0.136:/backup_db
    15. #本地保留最近30天的备份文件
    16. find /backup_db -mtime +30 -type f -name "*.sql" -exec rm -rf {} \;
    17. #备份服务器上也保留最近30天的文件
    18. #生成一个脚本文件
    19. cat >del_30days_file.sh <<EOF
    20. find /backup_db -mtime +30 -type f -name "*.sql" -exec rm -rf {} \;
    21. EOF
    22. #上传脚本文件到备份服务器
    23. scp del_30days_file.sh root@192.168.0.136:/root
    24. #远程执行脚本
    25. ssh root@192.168.0.136 bash /root/del_30days_file.sh

    备份方式

    参考:https://www.cnblogs.com/zywu-king/p/10745600.html 

    完全备份(全备)

    mysqldump命令

    优点:备份了所有内容

    缺点:备份的东西比较多的话,时间比较长

    增量备份

    备份的好处是每次备份需要备份的数据较少,耗时较短,占用的空间较小;

    坏处是数据恢复比较麻烦

    差异备份

    差异备份也要先进行一次完全备份,但是和增量备份不同的是,每次差异备份都备份和原始的完全备份不同的数据。也就是说,差异备份每次备份的参照物都是原始的完全备份,而不是上一次的差异备份。

    案例

    备份方案:
    每天的下午16:50点做全备,刚好到了下午17点10分的时候,数据库被删除了,如何将数据恢复到17点10分的状态?

    mysqlbinlog命令

    根据位置号来恢复

    1. 备份和还原操作:
    2. 1.产生一个全新的二进制文件
    3. root@(none) 10:33 sc-mysql>show master status;
    4. +---------------------+----------+--------------+------------------+-------------------+
    5. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    6. +---------------------+----------+--------------+------------------+-------------------+
    7. | sc-mysql-bin.000004 | 23054 | | | |
    8. +---------------------+----------+--------------+------------------+-------------------+
    9. 1 row in set (0.00 sec)
    10. root@(none) 10:34 sc-mysql>flush logs;
    11. Query OK, 0 rows affected (0.01 sec)
    12. root@(none) 10:35 sc-mysql>show master status;
    13. +---------------------+----------+--------------+------------------+-------------------+
    14. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    15. +---------------------+----------+--------------+------------------+-------------------+
    16. | sc-mysql-bin.000005 | 154 | | | |
    17. +---------------------+----------+--------------+------------------+-------------------+
    18. 1 row in set (0.01 sec)
    19. root@(none) 10:35 sc-mysql>
    20. 2.给数据库做全备
    21. [root@sc-mysql mysql]# mysqldump -uroot -p'Sanchuang123#' --databases sanchuang >/backup/sanchuang.sql
    22. 3.让数据发生变化,进行insert 和删除操作等
    23. root@sanchuang 10:40 sc-mysql>insert into emp(id,name,deptid) values(3,'苏文洋',20);
    24. Query OK, 1 row affected (0.00 sec)
    25. root@sanchuang 10:40 sc-mysql>insert into emp(id,name,deptid) values(4,'qinjiahui',20);
    26. Query OK, 1 row affected (0.00 sec)
    27. root@sanchuang 10:40 sc-mysql>insert into emp(id,name,deptid) values(5,'chenran',20);
    28. Query OK, 1 row affected (0.00 sec)
    29. root@sanchuang 10:41 sc-mysql>show master status;
    30. +---------------------+----------+--------------+------------------+-------------------+
    31. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    32. +---------------------+----------+--------------+------------------+-------------------+
    33. | sc-mysql-bin.000005 | 998 | | | |
    34. +---------------------+----------+--------------+------------------+-------------------+
    35. 1 row in set (0.00 sec)
    36. root@sanchuang 10:41 sc-mysql>
    37. 4.模拟出现故障,删除数据库
    38. root@sanchuang 10:41 sc-mysql>drop database sanchuang;
    39. 5.开始取恢复数据
    40. 1步恢复全备
    41. [root@sc-mysql backup]# mysql -uroot -p'Sanchuang123#' < sanchuang.sql
    42. mysql: [Warning] Using a password on the command line interface can be insecure.
    43. [root@sc-mysql backup]#
    44. 2步:查看二进制日志找到删除数据库之前的position 位置号
    45. [root@sc-mysql mysql]# mysqlbinlog -v sc-mysql-bin.000005|egrep -C 5 "drop database"
    46. #210401 10:42:39 server id 1 end_log_pos 1063 CRC32 0x8623d686 Anonymous_GTID last_committed=3 sequence_number=4 rbr_only=no
    47. SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
    48. # at 1063
    49. #210401 10:42:39 server id 1 end_log_pos 1170 CRC32 0xf9eee347 Query thread_id=14 exec_time=0 error_code=0
    50. SET TIMESTAMP=1617244959/*!*/;
    51. drop database sanchuang
    52. /*!*/;
    53. # at 1170
    54. #210401 10:44:13 server id 1 end_log_pos 1235 CRC32 0xa92ef3c4 Anonymous_GTID last_committed=4 sequence_number=5 rbr_only=no
    55. SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
    56. # at 1235
    57. [root@sc-mysql mysql]#
    58. 3步:使用二进制日志去恢复
    59. [root@sc-mysql mysql]# mysqlbinlog --start-position=154 --stop-position=1063 sc-mysql-bin.000005|mysql -uroot -p'Sanchuang123#'
    60. mysql: [Warning] Using a password on the command line interface can be insecure.
    61. [root@sc-mysql mysql]#
    62. 6.查看数据是否恢复
    63. root@sanchuang 10:54 sc-mysql>select * from emp;
    64. +----+-----------+--------+
    65. | id | NAME | deptid |
    66. +----+-----------+--------+
    67. | 1 | 张三 | 10 |
    68. | 2 | 李四 | 10 |
    69. | 3 | 苏文洋 | 20 |
    70. | 4 | qinjiahui | 20 |
    71. | 5 | chenran | 20 |
    72. | 19 | 吴佩 | 10 |
    73. +----+-----------+--------+
    74. 6 rows in set (0.00 sec)
    75. root@sanchuang 10:55 sc-mysql>

    根据时间点来恢复

    1. mysqlbinlog --start-datetime="2005-04-20 9:55:00" \
    2. --stop-datetime="2005-04-20 10:05:00" \
    3. /var/log/mysql/bin.123456 > /tmp/mysql_restore.sql

    难点是找到起始点和结束点

    配合grep语句,当时最后做的删除操作 

    [root@zabbix-4-centos7 mysql]# mysqlbinlog -vv zabbix-4-centos7-bin.000003|grep -i -n -A 150  "create table student"

    自动化脚本——计划任务

    练习:
        1.编写一个脚本备份tennis库和mysql库到/backup目录下,要求备份的文件里有时间,精确到秒 
        2.每天的凌晨3点开始备份
       3.本地备份完成后,上传到专门的网络上的另外一台备份服务器里/backup/业务+机器名.tar.gz

    1. [root@wudang-mysql-2 mysql]# cat backup_db.sh
    2. #!/bin/bash
    3. #得到时间
    4. ctime=$(date +%F_%H%M%S)
    5. #在本地新建存放目录/backup_db
    6. mkdir -p /backup_db
    7. #备份TENNIS库到/backup_db叫tennis.sql
    8. mysqldump -uroot -pSanchuang1234# TENNIS >/backup_db/${ctime}_tennis.sql
    9. #备份mysql库里user表
    10. mysqldump -uroot -pSanchuang1234# mysql user >/backup_db/${ctime}_mysql_user.sql
    11. #备份服务器上新建文件夹/backup_db
    12. ssh root@192.168.0.136 mkdir -p /backup_db
    13. #上传当天备份的文件到备份服务器里192.168.0.136
    14. scp /backup_db/${ctime}*.sql root@192.168.0.136:/backup_db
    15. #本地保留最近30天的备份文件
    16. find /backup_db -mtime +30 -type f -name "*.sql" -exec rm -rf {} \;
    17. #备份服务器上也保留最近30天的文件
    18. #生成一个脚本文件
    19. cat >del_30days_file.sh <<EOF
    20. find /backup_db -mtime +30 -type f -name "*.sql" -exec rm -rf {} \;
    21. EOF
    22. #上传脚本文件到备份服务器
    23. scp del_30days_file.sh root@192.168.0.136:/root
    24. #远程执行脚本
    25. ssh root@192.168.0.136 bash /root/del_30days_file.sh

    物理备份和逻辑备份

    物理备份由存储数据库内容的目录和文件的原始副本组成。这种类型的备份适用于需要在出现问题时快速恢复的大型、重要的数据库。

    逻辑备份保存以逻辑数据库结构(CREATE DATABASE、 CREATE TABLE语句)和内容(INSERT语句或分隔文本文件)表示的信息。这种类型的备份适用于较小的数据量,您可以在其中编辑数据值或表结构,或者在不同的机器架构上重新创建数据。

    物理备份: 直接是数据 --》鱼
    逻辑备份: 备份是表结构和产生数据的过程(create,insert)  --》工艺(方法)和流程--》可以产生想要的数据 --》渔

    物理的: 硬件的
        磁盘对拷:
        
    逻辑的: 软件的

    远程备份rsync

    rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。已支持跨平台,可以在Windows与Linux间进行数据同步。

    参考:https://blog.csdn.net/qq_36045024/article/details/105072818

    实验步骤 

    1. rsync命令的使用
    2. scp
    3. [root@sc-mysql backup]# ssh-keygen -t rsa
    4. [root@sc-mysql backup]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.2.197
    5. [root@sc-mysql backup]# ssh 'root@192.168.2.197' mkdir /backup
    6. [root@sc-mysql backup]# ssh 'root@192.168.2.197' mkdir /backup/fengdeyong
    7. [root@sc-mysql backup]# ls
    8. all_db.sql hunan_liangliang.sql tennis_player.sql ws.sql
    9. [root@sc-mysql backup]# scp ws.sql root@192.168.2.197:/backup
    10. ws.sql 100% 2024 454.2KB/s 00:00
    11. [root@sc-mysql backup]#
    12. 每天的晚上230分钟备份所有的数据库,然后scp到备份服务器上
    13. [root@sc-mysql backup]# cat backup_db.sh
    14. #!/bin/bash
    15. #导出数据
    16. mysqldump -uroot -p'Sanchuang1234#' --databases wangshuai >/backup/$(date +%F)_wangshuai.sql
    17. #scp到远程的备份服务器
    18. scp /backup/$(date +%F)_wangshuai.sql root@192.168.2.197:/backup
    19. [root@sc-mysql backup]# bash backup_db.sh
    20. mysqldump: [Warning] Using a password on the command line interface can be insecure.
    21. 2022-08-18_wangshuai.sql 100% 2077 660.9KB/s 00:00
    22. [root@sc-mysql backup]#
    23. [root@sc-mysql backup]# crontab -e
    24. [root@sc-mysql backup]# crontab -l
    25. 30 2 * * * bash /backup/bacup_db.sh
    26. [root@sc-mysql backup]#
    27. ======================
    28. rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
    29. 已支持跨平台,可以在Windows与Linux间进行数据同步。
    30. raid
    31. 1 5 6 10
    32. 镜像卷: raid1
    33. 2块,可以坏一块,两块磁盘里存放相同的数据
    34. mirror 镜像,镜子
    35. rsync 全称 remote synchronize,即 远程同步。
    36. 使用 rsync 进行数据同步时,第一次进行完全备份,以后则是增量备份,利用 rsync 算法(差分编码),只传输差异部分数据。
    37. ====
    38. rsync+sersync文件实时同步
    39. 原文链接:https://blog.csdn.net/qq_36045024/article/details/105072818
    40. 一:什么是Rsync?
    41.   Rsync(Remote Synchronize)是一款开源的、快速的、多功能的、可以实现全量及增量的本地或远程数据同步备份的优秀工具,并且支持多种操作系统平台运行。
    42. 二:什么Sersync?
    43. 1、sersync是基于inotify开发的,类似于inotify-tools的工具,Sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的文件或者目录,因此效率更高。
    44. 2、主要应用场景为数据体积大,并且文件很多。
    45. 小结:Rsync+sersync
    46. 1、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;
    47. 2、rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。
    48. 三:环境
    49. 备份服务器:192.168.2.197  操作系统:Centos7.9  
    50. 数据源服务器:192.168.2.196 操作系统:Centos7.9
    51. [root@sc-mysql2 backup]# cat /etc/centos-release
    52. CentOS Linux release 7.9.2009 (Core)
    53. [root@sc-mysql2 backup]#
    54. 四:备份服务器操作
    55. 1、关闭 selinux #永久关闭linux防火墙
    56. [root@sc-mysql2 backup]# getenforce
    57. Permissive
    58. [root@sc-mysql2 backup]# vim /etc/selinux/config
    59. SELINUX=disabled
    60. 2、关闭防火墙
    61. [root@sc-mysql2 backup]# service firewalld stop
    62. Redirecting to /bin/systemctl stop firewalld.service
    63. 3、安装rsync服务端软件
    64. [root@sc-mysql2 backup]# yum install rsync xinetd -y
    65. [root@sc-mysql2 backup]# vi /etc/rc.d/rc.local # #设置开机启动
    66. /usr/bin/rsync --daemon --config=/etc/rsyncd.conf # 添加开机启动
    67. [root@sc-mysql2 backup]# chmod +x /etc/rc.d/rc.local
    68. [root@sc-mysql2 backup]# systemctl start xinetd #启动xinetd
    69. xinetd是一个提供保姆服务的进程,rsync是它照顾的进程
    70. 独立的服务:ssh,dhcp,mysql
    71. 非独立的服务,非独立的服务需要依赖其他的服务来管理,rsync就是一个非独立的服务,依赖xinetd来管理
    72. 4、创建rsyncd.conf配置文件
    73. [root@sc-mysql2 backup]# vim /etc/rsyncd.conf 添加下面的配置
    74. uid = root
    75. gid = root
    76. use chroot = yes
    77. max connections = 0
    78. log file = /var/log/rsyncd.log
    79. pid file = /var/run/rsyncd.pid
    80. lock file = /var/run/rsync.lock
    81. secrets file = /etc/rsync.pass
    82. motd file = /etc/rsyncd.Motd
    83. [back_data] #配置项名称(自定义)
    84. path = /backup #备份文件存储地址
    85. comment = A directory in which data is stored
    86. ignore errors = yes
    87. read only = no
    88. hosts allow = 192.168.2.196 #允许的ip地址(数据源服务器地址)
    89. 5、创建用户认证文件
    90. $ vi /etc/rsync.pass # 配置文件,添加以下内容,添加允许传输用户和密码
    91. sunline:sunline # 格式,用户名:密码,可以设置多个,每行一个用户名:密码
    92. sc:sc123456
    93. 6、设置文件权限
    94. $ chmod 600 /etc/rsyncd.conf #设置文件所有者读取、写入权限
    95. $ chmod 600 /etc/rsync.pass #设置文件所有者读取、写入权限
    96. 7、启动rsync和xinetd
    97. [root@sc-mysql2 backup]# /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
    98. [root@sc-mysql2 backup]# ps aux|grep rsync
    99. root 9455 0.0 0.0 114844 584 ? Ss 16:13 0:00 /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
    100. root 9457 0.0 0.0 112824 988 pts/0 S+ 16:13 0:00 grep --color=auto rsync
    101. [root@sc-mysql2 backup]#
    102. [root@sc-mysql2 backup]# systemctl start xinetd
    103. [root@sc-mysql2 backup]# ps aux|grep xinetd
    104. root 9425 0.0 0.0 25044 584 ? Ss 16:00 0:00 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid
    105. root 9465 0.0 0.0 112824 988 pts/0 S+ 16:14 0:00 grep --color=auto xinetd
    106. [root@sc-mysql2 backup]#
    107. 8.查看rsync监听的端口号
    108. [root@sc-mysql2 backup]# netstat -anplut
    109. Active Internet connections (servers and established)
    110. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
    111. tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 9455/rsync
    112. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1040/sshd
    113. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1256/master
    114. tcp 0 36 192.168.2.197:22 192.168.2.105:58059 ESTABLISHED 3556/sshd: root@pts
    115. tcp6 0 0 :::873 :::* LISTEN 9455/rsync
    116. tcp6 0 0 :::3306 :::* LISTEN 2597/mysqld
    117. tcp6 0 0 :::22 :::* LISTEN 1040/sshd
    118. tcp6 0 0 ::1:25 :::* LISTEN 1256/master
    119. udp 0 0 127.0.0.1:323 0.0.0.0:* 675/chronyd
    120. udp6 0 0 ::1:323 :::* 675/chronyd
    121. [root@sc-mysql2 backup]#
    122. =================
    123. 五:数据源服务器操作
    124. (1)安装rsync客户端软件
    125. 1、关闭 selinux #永久关闭linux防火墙
    126. [root@sc-mysql2 backup]# getenforce
    127. Permissive
    128. [root@sc-mysql2 backup]# vim /etc/selinux/config
    129. SELINUX=disabled
    130. 2、关闭防火墙
    131. [root@sc-mysql2 backup]# service firewalld stop
    132. Redirecting to /bin/systemctl stop firewalld.service
    133. 3、安装rsync服务端软件
    134. [root@sc-mysql2 backup]# yum install rsync xinetd -y
    135. [root@sc-mysql2 backup]# vi /etc/rc.d/rc.local # #设置开机启动
    136. /usr/bin/rsync --daemon --config=/etc/rsyncd.conf # 添加开机启动
    137. [root@sc-mysql2 backup]# chmod +x /etc/rc.d/rc.local
    138. [root@sc-mysql2 backup]# systemctl start xinetd #启动xinetd
    139. [root@sc-mysql backup]# vim /etc/rsyncd.conf
    140. log file = /var/log/rsyncd.log
    141. pid file = /var/run/rsyncd.pid
    142. lock file = /var/run/rsync.lock
    143. motd file = /etc/rsyncd.Motd
    144. [Sync]
    145. comment = Sync
    146. uid = root
    147. gid = root
    148. port= 873
    149. $ systemctl start xinetd #启动(CentOS中是以xinetd来管理rsync服务的)
    150. 4、创建认证密码文件
    151. [root@sc-mysql backup]# vim /etc/passwd.txt
    152. sc123456 #编辑文件,添加以下内容,该密码应与目标服务器中的/etc/rsync.pass中的密码一致
    153. [root@sc-mysql backup]#
    154. [root@sc-mysql backup]# chmod 600 /etc/passwd.txt #设置文件权限,只设置文件所有者具有读取、写入权限即可
    155. [root@sc-mysql backup]#
    156. 5、测试数据同步
    157. 数据源服务器192.168.2.196 到备份服务器192.168.2.197之间的数据同步
    158. $ rsync -avH --port=873 --progress --delete /var/www/data(要备份的数据源目录 ) root@***.***.***.222::back_data(rsyncd.conf文件配置名称) --password-file=/etc/passwd.txt
    159. $ rsync -avH --port=873 --progress --delete /backup root@192.168.2.197::back_data --password-file=/etc/passwd.txt
    160. rsync --help
    161. [root@sc-mysql backup]# rsync -avH --port=873 --progress --delete /backup root@192.168.2.197::back_data --password-file=/etc/passwd.txt
    162. sending incremental file list
    163. backup/
    164. backup/2022-08-18_wangshuai.sql
    165. 2,077 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=5/7)
    166. backup/all_db.sql
    167. 936,290 100% 34.34MB/s 0:00:00 (xfr#2, to-chk=4/7)
    168. backup/backup_db.sh
    169. 222 100% 8.34kB/s 0:00:00 (xfr#3, to-chk=3/7)
    170. backup/hunan_liangliang.sql
    171. 19,833 100% 717.34kB/s 0:00:00 (xfr#4, to-chk=2/7)
    172. backup/tennis_player.sql
    173. 3,106 100% 112.34kB/s 0:00:00 (xfr#5, to-chk=1/7)
    174. backup/ws.sql
    175. 2,024 100% 73.21kB/s 0:00:00 (xfr#6, to-chk=0/7)
    176. sent 964,284 bytes received 134 bytes 1,928,836.00 bytes/sec
    177. total size is 963,552 speedup is 1.00
    178. [root@sc-mysql backup]#
    179. 增加文件,或者删除文件,测试是否可以增量备份
    180. [root@sc-mysql backup]# ls
    181. 2022-08-18_wangshuai.sql all_db.sql backup_db.sh hunan_liangliang.sql passwd tennis_player.sql ws.sql
    182. [root@sc-mysql backup]# rm -rf passwd
    183. [root@sc-mysql backup]# cp /etc/hosts .
    184. [root@sc-mysql backup]# ls
    185. 2022-08-18_wangshuai.sql all_db.sql backup_db.sh hosts hunan_liangliang.sql tennis_player.sql ws.sql
    186. [root@sc-mysql backup]# rsync -avH --port=873 --progress --delete /backup root@192.168.2.197::back_data --password-file=/etc/passwd.txt
    187. sending incremental file list
    188. deleting backup/passwd
    189. backup/
    190. backup/hosts
    191. 158 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=3/8)
    192. sent 472 bytes received 56 bytes 1,056.00 bytes/sec
    193. total size is 963,710 speedup is 1,825.21
    194. 目前为止,只是实现了手工的去同步数据
    195. 还没有实现自动的实时的同步
    196. ===
    197. (2)安装sersync工具,实时触发rsync进行同步 --》安装到数据源服务器上
    198. 备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:#uname -a查看内核
    199. CentOS 7.0内核为3.10.0,默认已经支持inotify
    200. inotify已经默认在内核里安装了,不需要安装
    201. 1、修改inotify默认参数(inotify默认内核参数值太小) 修改参数:
    202. [root@sc-mysql backup]# sysctl -w fs.inotify.max_queued_events="99999999"
    203. fs.inotify.max_queued_events = 99999999
    204. [root@sc-mysql backup]# sysctl -w fs.inotify.max_user_watches="99999999"
    205. fs.inotify.max_user_watches = 99999999
    206. [root@sc-mysql backup]# sysctl -w fs.inotify.max_user_instances="65535"
    207. fs.inotify.max_user_instances = 65535
    208. [root@sc-mysql backup]#
    209. 永久修改参数
    210. [root@sc-mysql backup]# vim /etc/sysctl.conf
    211. fs.inotify.max_queued_events=99999999
    212. fs.inotify.max_user_watches=99999999
    213. fs.inotify.max_user_instances=65535
    214. 2、安装sersync
    215. [root@sc-mysql backup]# yum install wget -y
    216. [root@sc-mysql backup]# wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
    217. [root@sc-mysql backup]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
    218. [root@sc-mysql backup]# mv GNU-Linux-x86/ /usr/local/sersync
    219. 3、创建rsync
    220. [root@sc-mysql backup]# cd /usr/local/sersync/
    221. [root@sc-mysql sersync]# ls
    222. confxml.xml sersync2
    223. [root@sc-mysql sersync]#
    224. 备份配置文件,防止修改错了,不知道哪里出错,好还原
    225. [root@sc-mysql sersync]# cp confxml.xml confxml.xml.bak
    226. [root@sc-mysql sersync]# cp confxml.xml data_configxml.xml
    227. [root@sc-mysql sersync]# ls
    228. confxml.xml confxml.xml.bak data_configxml.xml sersync2
    229. [root@sc-mysql sersync]#
    230. data_configxml.xml 是后面需要使用的配置文件
    231. 4、修改配置 data_configxml.xml 文件
    232. 24行后的配置
    233. <localpath watch="/backup">
    234. <remote ip="192.168.2.197" name="back_data"/>
    235. <!--<remote ip="192.168.8.39" name="tongbu"/>-->
    236. <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    237. </localpath>
    238. <rsync>
    239. <commonParams params="-artuz"/>
    240. <auth start="false" users="root" passwordfile="/etc/passwd.txt"/>
    241. <userDefinedPort start="false" port="874"/><!-- port=874 -->
    242. <timeout start="false" time="100"/><!-- timeout=100 -->
    243. <ssh start="false"/>
    244. 5、启动服务
    245. [root@sc-mysql sersync]# PATH=/usr/local/sersync/:$PATH
    246. [root@sc-mysql sersync]# which sersync2
    247. /usr/local/sersync/sersync2
    248. [root@sc-mysql sersync]#
    249. [root@sc-mysql sersync]# echo 'PATH=/usr/local/sersync/:$PATH' >>/root/.bashrc
    250. [root@sc-mysql sersync]# sersync2 -d -r -o /usr/local/sersync/data_configxml.xml
    251. set the system param
    252. execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
    253. execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
    254. parse the command param
    255. option: -d run as a daemon
    256. option: -r rsync all the local files to the remote servers before the sersync work
    257. option: -o config xml name: /usr/local/sersync/data_configxml.xml
    258. daemon thread num: 10
    259. parse xml config file
    260. host ip : localhost host port: 8008
    261. daemon start,sersync run behind the console
    262. config xml parse success
    263. please set /etc/rsyncd.conf max connections=0 Manually
    264. sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
    265. Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
    266. please according your cpu ,use -n param to adjust the cpu rate
    267. ------------------------------------------
    268. rsync the directory recursivly to the remote servers once
    269. working please wait...
    270. execute command: cd /backup && rsync -artuz -R --delete ./ 192.168.2.197::back_data >/dev/null 2>&1
    271. run the sersync:
    272. watch path is: /backup
    273. [root@sc-mysql sersync]#
    274. [root@sc-mysql backup]# ps aux|grep sersync
    275. root 25655 0.0 0.0 92324 704 ? Ssl 17:47 0:00 sersync2 -d -r -o /usr/local/sersync/data_configxml.xml
    276. root 25673 0.0 0.0 112824 988 pts/1 S+ 17:48 0:00 grep --color=auto sersync
    277. [root@sc-mysql backup]#
    278. 验证:去/backup目录下新建一些文件或者文件夹,测试是否在备份服务器上可以看到
    279. 6、设置sersync监控开机自动执行
    280. [root@sc-mysql backup]# vim /etc/rc.local
    281. /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/data_configxml.xml
    282. 密码不正确或者用户名不正确,也可以实时同步文件,思考底层是否利用了我们建立的ssh免密通道?
    283. 熟悉rsync+sersync实现文件的实时同步
    284. =====
    285. 错误的问题:
    286. 1.配置文件没有写内容,或者打错
    287. 2.两边的服务器没有新建目录/backup -->特别是备份服务器上没有新建/backup
    288. [root@sc-mysql2 backup]# vim /etc/rsyncd.conf
    289. uid = root
    290. gid = root
    291. use chroot = yes
    292. max connections = 0
    293. log file = /var/log/rsyncd.log
    294. pid file = /var/run/rsyncd.pid
    295. lock file = /var/run/rsync.lock
    296. secrets file = /etc/rsync.pass
    297. motd file = /etc/rsyncd.Motd
    298. [back_data]
    299. path = /backup2 #存放备份文件的目录
    300. comment = A directory in which data is stored
    301. ignore errors = yes
    302. read only = no
    303. hosts allow = 192.168.2.196
    304. 备份服务器里的备份目录是/backup2,但是这个文件没有创建,导致不能备份过去
    305. 在备份服务器上需要我们去创建mkdir /backup2
    306. 修改了rsyncd.conf配置文件,需要重启xinetd服务,会帮我们去重启rsync进程
    307. [root@sc-mysql2 backup]# service xinetd restart
    308. Redirecting to /bin/systemctl restart xinetd.service
    309. [root@sc-mysql2 backup]#
    310. 排错查看日志
    311. [root@sc-mysql2 backup]# cat /var/log/rsyncd.log
    312. 2022/08/18 16:13:19 [9455] rsyncd version 3.1.2 starting, listening on port 873
    313. 2022/08/18 16:35:08 [9472] name lookup failed for 192.168.2.196: Name or service not known
    314. 2022/08/18 16:35:08 [9472] connect from UNKNOWN (192.168.2.196)
    315. 2022/08/18 17:16:37 [9532] rsync: chroot /backup2 failed: No such file or directory (2) 错误信息
    316. [root@sc-mysql2 backup]#
    317. [root@sc-mysql backup]# rsync -avH --port=873 --progress --delete /backup root@192.168.2.197::back_data --password-file=/etc/passwd.txt
    318. @ERROR: chroot failed
    319. rsync error: error starting client-server protocol (code 5) at main.c(1649) [sender=3.1.2]
    320. [root@sc-mysql backup]#

    还原

    [root@Sanchuang backup]# mysql -uroot -p'Sanchuang123#' 

    基于时间:https://dev.mysql.com/doc/refman/5.7/en/point-in-time-recovery-binlog.html

    基于位置号:https://dev.mysql.com/doc/refman/5.7/en/point-in-time-recovery-positions.html

  • 相关阅读:
    vsCode 忽略 文件上传
    常用压缩解压缩命令
    C#宏定义
    HTTP 原理与CND原理
    美食杰项目(一)登录注册页
    论文阅读:GPT-too- A language-model-first approach for AMR-to-text generation Manuel
    多叉树+图实现简单业务流程
    澳大利亚博士后招聘|皇家墨尔本理工学院材料科学
    Vue根据屏幕分辨率计算div可以显示的数量,dom渲染在v-if之后造成的复杂处理
    消防宣传科普|消防安全知识网上答题挑战赛活动方案
  • 原文地址:https://blog.csdn.net/ZhouXin1111112/article/details/132780071