Centos configuration rsync remote synchronization and real-time backup using inotify + rsync

Bowen directory
a, rsync Overview
1, the basic usage of rsync command of
two, rsync configuration
1, configuration synchronization authenticated rsync
2, rsync regularly synchronized
3, real-time synchronization configuration inotify + rsync

A, rsync Overview

rsync (Remote Sync, remote synchronization) is an open source fast backup tool, you can mirror the entire directory tree synchronization between different hosts, supports incremental backups, keeping links and permissions, and the use of synchronization algorithm optimization, compression is performed prior to transmission, making it ideal for remote backup, mirror server applications. rsync's official site is http://rsync.samba.org/ commonly used as a file backup tool, rsync is often one of the basic components of Linux and UNIX systems installed by default.

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

In remote synchronization tasks, responsible for initiating client rsync synchronization of operations as the initiator, and responds to the server from the client rsync synchronous operation called synchronization source. During the synchronization process, the synchronization position is responsible for providing the original source document, initiated to deal with the end position have read access. rsync as a daemon running synchronization source to provide a backup source for other clients. Configuring rsync synchronization source need to create the configuration file rsync.conf, create a backup account, then rsync daemon program ( "--daemon" option) run.
Centos configuration rsync remote synchronization and real-time backup using inotify + rsync

Basic usage 1, rsync command

The vast majority of backup program requires you to specify the original position, target position, rsync command is the same. The simplest usage is similar to rsync cp command. For example, the file / etc / fstab, directory / boot / grub sync back to the / opt directory, wherein, "- r" represents option recursively entire directory tree, "- l" option is used to back up the link file.

The basic format of the backup is "rsync [options] original target position", in which some commonly used command options are as follows:

  • -r: recursive mode, contains all the files in the directory and subdirectories;
  • -l: For symbolic link files are still copying is a symbolic link file;
  • -v: display details of the synchronization process;
  • -a: archive mode, preserve file permissions, attributes and other information, equivalent to a combination of options "-rlptgoD";
  • -z: compressed when the file transfer;
  • -p: keep the file permissions mark;
  • -t: retention timestamp on the file;
  • -g: retention is a group of tag files (only the super user);
  • -o: Reserved owner markup file (only super user);
  • -H: hard-wired to retain documents;
  • -A: Reserved ACL attribute information;
  • -D: reserved special device files and other documents;
  • --delete: delete the target location and the original location does not have a file;
  • --checksum: The checksum (instead of a file size, modify time) to decide whether to skip files;

Second, configure rsync

[root@centos01 ~]# cp /etc/rsyncd.conf /etc/rsyncd.conf.bak   <!--备份rsync主配置文件-->
[root@centos01 ~]# vim /etc/rsyncd.conf   <!--编辑主配置文件-->
uid = nobody         <!--管理rsync的用户-->
 gid = nobody       <!--管理rsync的组-->
 port 873           <!--rsync的端口号-->
pid file = /var/run/rsyncd.pid        <!--rsync进程id位置-->
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  <!--同步在压缩的文件类型-->
 auth users = bob           <!--验证账户-->
 secrest file = /etc/rsync_user.db          <!--密码数据库-->
 address = 192.168.100.10            <!--rsync服务监听的ip地址-->
 hosts allow = 192.168.100.0/24       <!--允许192.168.100.0网段访问-->
 read only = yes          <!--允许读取权限-->
[root@centos01 ~]# rsync --daemon          <!--启动rsync服务-->
[root@centos01 ~]# netstat -anptu | grep rsync        <!--监听rsync服务是否正常启动-->
tcp        0      0 192.168.100.10:873      0.0.0.0:*               LISTEN      1422/rsync     
[root@centos01 ~]# kill 1422        <!--停止服务使用kill结束进程-->
[root@centos01 ~]# vim /etc/rc.d/rc.local          <!--设置开机自动启动rsync服务-->
/usr/bin/rsync --daemon          <!--rsync启动服务添加到配置文件中-->
[root@centos01 ~]# chmod +x /etc/rc.d/rc.local         <!--添加执行权限-->

[root@centos01 ~]# mkdir centos7             <!--创建centos7目录-->
[root@centos01 ~]# rsync -alv /mnt/* ./centos7/      
                                                         <!--将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      
                   <!--将源benet目录中数据同步到目录xsh目录,删除xsh目录中的历史数据-->
sending incremental file list
./
deleting 2.txt
1.txt

sent 92 bytes  received 34 bytes  252.00 bytes/sec
total size is 6  speedup is 0.05
[root@centos01 ~]# cd xsh          <!--进入xsh目录-->
[root@centos01 xsh]# ls           <!--查看是否同步成功-->
1.txt
[root@centos01 ~]# rsync -av ./xsh/ [email protected]:/          
                      <!--将本地xsh目录中的数据,同步到远程主机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       <!--输入yes-->
Warning: Permanently added '192.168.100.20' (ECDSA) to the list of known hosts.
[email protected]'s password:         <!--输入密码-->
sending incremental file list
./
1.txt

sent 92 bytes  received 34 bytes  19.38 bytes/sec
total size is 6  speedup is 0.05
[root@centos02 ~]# cd /      <!--进入根目录-->
[root@centos02 /]# ls         <!--查看目录下文件-->
1.txt  boot  etc   lib    media  opt   root  sbin  sys  usr
bin    dev   home  lib64  mnt    proc  run   srv   tmp  var

1, configuration synchronization authenticated rsync

[root@centos01 ~]# vim /etc/rsync_user.db   
                                       <!--创建rsync验证数据库,账户是bob,密码是pwd@123-->
bob:pwd@123
[root@centos01 ~]# chmod 600 /etc/rsync_user.db     <!--验证数据库文件添加600权限-->
[root@centos01 ~]# vim /etc/rsyncd.conf      <!--修改rsync主配置文件创建共享-->
  [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 [email protected]::accp ./xsh/    
<!--客户端同步数据,将远程服务器192.168.100.10的accp数据同步到当前位置的xsh目录-->
receiving incremental file list 
./
aa.txt

sent 48 bytes  received 118 bytes  332.00 bytes/sec
total size is 4  speedup is 0.02
[root@centos01 ~]# rsync -av rsync://[email protected]/accp ./xsh/       <!--第二种方式同步-->
receiving incremental file list
./
aa.txt

sent 48 bytes  received 118 bytes  332.00 bytes/sec
total size is 4  speedup is 0.02
[root@centos01 ~]# cd xsh/         <!--验证是否同步成功-->
[root@centos01 xsh]# ls
aa.txt

2, rsync regularly synchronization

[root@centos01 ~]# mount /dev/cdrom /mnt/     <!--切换Linux光盘安装inotify-->
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# tar zxvf /mnt/inotify-tools-3.14.tar.gz -C /usr/src/   <!--解压缩inotify-->
[root@centos01 ~]# cd /usr/src/inotify-tools-3.14/   <!--进入inotify目录-->
[root@centos01 inotify-tools-3.14]# ./configure     <!--配置inotify-->
[root@centos01 inotify-tools-3.14]# make && make install  <!--编译安装inotify-->
[root@centos01 ~]# cat /proc/sys/fs/inotify/max_queued_events 
                                                                   <!--查看inotify监控事件队列-->
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  <!--修改inotify配置文件,加大三个参数的值-->
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@centos01 ~]# sysctl -p          <!--更新内核参数-->
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@centos01 ~]# inotifywait -mrq -e modify,create,delete,move,attrib /
accp/     
                         <!--配置一次性监控,监控/accp目录发生的变化-->
[root@centos01 ~]# cd /accp/       
                             <!--进入accp目录,修改、删除、创建数据测试是否监控成功-->
[root@centos01 accp]# ls
aa.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, real-time synchronization configuration inotify + rsync

[root@centos01 ~]# vim rsync.sh         <!--创建编写实时同步脚本-->
#!/bin/bash
INW="inotifywait -mrq -e modify,create,delete,move,attrib /accp/"
RSYNC="rsync -avzH /accp/ [email protected]:/baidu/ --delete"
$INW | while read DIRECTORY EVENT FILE;do
$RSYNC &> /dev/null
done
[root@centos01 ~]# chmod +x rsync.sh      <!--脚本添加执行权限-->
[root@centos01 ~]# ssh-keygen -t rsa          <!--配置密钥对-->
[root@centos01 ~]# ssh-copy-id -i ./.ssh/id_rsa.pub [email protected]    
                                                              <!--上传ssh客户端的公钥到ssh服务器端-->
[root@centos01 ~]# netstat -anptu | grep rsync     <!--停止rsync服务重新启动-->
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      7657
tcp6       0      0 :::873                  :::*                    LISTEN      765
[root@centos01 ~]# kill 7657             <!--停止rsync服务-->
[root@centos01 ~]# rsync --daemon      <!--重新启动-->
[root@centos02 ~]# mkdir baidu          <!--服务器端创建baidu目录-->
[root@centos02 ~]# cd baidu/             <!--进入baidu目录-->
[root@centos02 baidu]# echo "111" > 333.txt       <!--插入数据-->
[root@centos02 baidu]# ls         <!--查看-->
333.txt
[root@centos01 ~]# ./rsync.sh &          <!--执行脚本-->
[3] 11160
[root@centos02 ~]# cd /baidu/     
                  <!--服务器端查看baidu目录是否删除历史数据插入客户端accp目录下的数据-->
[root@centos02 baidu]# ls
w.txt
[root@centos01 ~]# vim /etc/rc.d/rc.local 
                              <!--将rsync实时同步的脚本添加到开机自动启动配置文件中-->
/root/rsync.sh &           <!--执行脚本的路径添加进来-->
[root@centos02 ~]# kill 7984       <!--停止rsync服务-->
[root@centos02 ~]# rsync --daemon    <!--重新启动-->

Guess you like

Origin blog.51cto.com/14156658/2451097