千家信息网

Centos 配置rsync远程同步及使用inotify+rsync实时备份

发表于:2024-09-29 作者:千家信息网编辑
千家信息网最后更新 2024年09月29日,博文目录一、rsync概述1、rsync命令的基本用法二、配置rsync1、配置同步身份验证的rsync2、rsync定期同步3、配置inotify+rsync实时同步一、rsync概述rsync(R
千家信息网最后更新 2024年09月29日Centos 配置rsync远程同步及使用inotify+rsync实时备份

博文目录
一、rsync概述
1、rsync命令的基本用法
二、配置rsync
1、配置同步身份验证的rsync
2、rsync定期同步
3、配置inotify+rsync实时同步

一、rsync概述

rsync(Remote Sync,远程同步)是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。rsync的官方站点是http://rsync.samba.org/ 作为一种常用的文件备份工具,rsync往往是Linux和UNIX系统默认安装的基本组件之一。

[root@centos01 ~]# rpm -q rsyncrsync-3.0.9-18.el7.x86_64

在远程同步任务中,负责发起rsync同步 操作的客户机称为发起端,而负责响应来自客户机的rsync同步操作的服务器称为同步源。在同步过程中,同步源负责提供文档的原始位置,发起端应对该位置具有读取权限。rsync作为同步源时以守护进程运行,为其他客户机提供备份源。配置rsync同步源需要建立配置文件rsync.conf,创建备份账号,然后将rsync程序以守护进程("--daemon"选项)方式运行。

1、rsync命令的基本用法

绝大多数的备份程序要求指定原始位置、目标位置,rsync命令也一样。最简单的rsync用法类似于cp命令。例如,可以将文件/etc/fstab、目录/boot/grub同步备份到/opt目录下,其中,"-r"选项表示递归整个目录树,"-l"选项用来备份链接文件。

备份的基本格式为"rsync [选项] 原始位置 目标位置",其中常用的一些命令选项如下:

  • -r:递归模式,包含目录及子目录中的所有文件;
  • -l:对于符号链接文件仍然复制为符号链接文件;
  • -v:显示同步过程的详细信息;
  • -a:归档模式,保留文件的权限、属性等信息,等同于组合选项"-rlptgoD";
  • -z:在传输文件时进行压缩;
  • -p:保留文件的权限标记;
  • -t:保留文件的时间标记;
  • -g:保留文件的属组标记(仅超级用户使用);
  • -o:保留文件的属主标记(仅超级用户使用);
  • -H:保留硬连接文件;
  • -A:保留ACL属性信息;
  • -D:保留设备文件及其他特殊文件;
  • --delete:删除目标位置有而原始位置没有的文件;
  • --checksum:根据校验和(而不是文件大小、修改时间)来决定是否跳过文件;

二、配置rsync

[root@centos01 ~]# cp /etc/rsyncd.conf /etc/rsyncd.conf.bak   [root@centos01 ~]# vim /etc/rsyncd.conf   uid = nobody          gid = nobody        port 873           pid file = /var/run/rsyncd.pid        dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2   auth users = bob            secrest file = /etc/rsync_user.db           address = 192.168.100.10             hosts allow = 192.168.100.0/24        read only = yes          [root@centos01 ~]# rsync --daemon          [root@centos01 ~]# netstat -anptu | grep rsync        tcp        0      0 192.168.100.10:873      0.0.0.0:*               LISTEN      1422/rsync     [root@centos01 ~]# kill 1422        [root@centos01 ~]# vim /etc/rc.d/rc.local          /usr/bin/rsync --daemon          [root@centos01 ~]# chmod +x /etc/rc.d/rc.local         [root@centos01 ~]# mkdir centos7             [root@centos01 ~]# rsync -alv /mnt/* ./centos7/                                                               [root@centos01 ~]# mkdir benet            [root@centos01 ~]# mkdir xsh          [root@centos01 ~]# echo "11111" > ./benet/1.txt          [root@centos01 ~]# echo "22222" > ./xsh/2.txt           [root@centos01 ~]# rsync -av --delete ./benet/ ./xsh                         sending incremental file list./deleting 2.txt1.txtsent 92 bytes  received 34 bytes  252.00 bytes/sectotal size is 6  speedup is 0.05[root@centos01 ~]# cd xsh          [root@centos01 xsh]# ls           1.txt[root@centos01 ~]# rsync -av ./xsh/ root@192.168.100.20:/                                The authenticity of host '192.168.100.20 (192.168.100.20)' can't be established.ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.Are you sure you want to continue connecting (yes/no)? yes       Warning: Permanently added '192.168.100.20' (ECDSA) to the list of known hosts.root@192.168.100.20's password:         sending incremental file list./1.txtsent 92 bytes  received 34 bytes  19.38 bytes/sectotal size is 6  speedup is 0.05[root@centos02 ~]# cd /      [root@centos02 /]# ls         1.txt  boot  etc   lib    media  opt   root  sbin  sys  usrbin    dev   home  lib64  mnt    proc  run   srv   tmp  var

1、配置同步身份验证的rsync

[root@centos01 ~]# vim /etc/rsync_user.db                                          bob:pwd@123[root@centos01 ~]# chmod 600 /etc/rsync_user.db     [root@centos01 ~]# vim /etc/rsyncd.conf        [accp]                  path = /accp            comment = test            auth users bob             secrets file = /etc/rsync_user.db             read only = yes       [root@centos01 ~]# mkdir /accp      [root@centos01 ~]# echo "accp.com" > /accp/qq.txt      [root@centos01 ~]# rsync -av bob@192.168.100.10::accp ./xsh/    receiving incremental file list ./aa.txtsent 48 bytes  received 118 bytes  332.00 bytes/sectotal size is 4  speedup is 0.02[root@centos01 ~]# rsync -av rsync://bob@192.168.100.10/accp ./xsh/       receiving incremental file list./aa.txtsent 48 bytes  received 118 bytes  332.00 bytes/sectotal size is 4  speedup is 0.02[root@centos01 ~]# cd xsh/         [root@centos01 xsh]# lsaa.txt

2、rsync定期同步

[root@centos01 ~]# mount /dev/cdrom /mnt/     mount: /dev/sr0 写保护,将以只读方式挂载[root@centos01 ~]# tar zxvf /mnt/inotify-tools-3.14.tar.gz -C /usr/src/   [root@centos01 ~]# cd /usr/src/inotify-tools-3.14/   [root@centos01 inotify-tools-3.14]# ./configure     [root@centos01 inotify-tools-3.14]# make && make install  [root@centos01 ~]# cat /proc/sys/fs/inotify/max_queued_events                                                                    16384[root@centos01 ~]# cat /proc/sys/fs/inotify/max_user_instances 128[root@centos01 ~]# cat /proc/sys/fs/inotify/max_user_watches 8192[root@centos01 ~]# vim /etc/sysctl.conf  fs.inotify.max_queued_events = 16384fs.inotify.max_user_instances = 1024fs.inotify.max_user_watches = 1048576[root@centos01 ~]# sysctl -p          fs.inotify.max_queued_events = 16384fs.inotify.max_user_instances = 1024fs.inotify.max_user_watches = 1048576[root@centos01 ~]# inotifywait -mrq -e modify,create,delete,move,attrib /accp/                              [root@centos01 ~]# cd /accp/                                    [root@centos01 accp]# lsaa.txt[root@centos01 accp]# touch 11.txt [root@centos01 accp]# echo "111" > 1.txt [root@centos01 accp]# rm -rf 11.txt[root@centos01 ~]# inotifywait -mrq -e modify,create,delete,move,attrib /accp/             /accp/ CREATE 11.txt/accp/ ATTRIB 11.txt/accp/ CREATE 1.txt/accp/ MODIFY 1.txt/accp/ DELETE 11.txt

3、配置inotify+rsync实时同步

[root@centos01 ~]# vim rsync.sh         #!/bin/bashINW="inotifywait -mrq -e modify,create,delete,move,attrib /accp/"RSYNC="rsync -avzH /accp/ root@192.168.100.20:/baidu/ --delete"$INW | while read DIRECTORY EVENT FILE;do$RSYNC &> /dev/nulldone[root@centos01 ~]# chmod +x rsync.sh      [root@centos01 ~]# ssh-keygen -t rsa          [root@centos01 ~]# ssh-copy-id -i ./.ssh/id_rsa.pub root@192.168.100.20                                                                  [root@centos01 ~]# netstat -anptu | grep rsync     tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      7657tcp6       0      0 :::873                  :::*                    LISTEN      765[root@centos01 ~]# kill 7657             [root@centos01 ~]# rsync --daemon      [root@centos02 ~]# mkdir baidu          [root@centos02 ~]# cd baidu/             [root@centos02 baidu]# echo "111" > 333.txt       [root@centos02 baidu]# ls         333.txt[root@centos01 ~]# ./rsync.sh &          [3] 11160[root@centos02 ~]# cd /baidu/                       [root@centos02 baidu]# lsw.txt[root@centos01 ~]# vim /etc/rc.d/rc.local                               /root/rsync.sh &           [root@centos02 ~]# kill 7984       [root@centos02 ~]# rsync --daemon    
0