文件同步和备份:rsync

1,一般用法: 类似scp

#centos base仓库有这个包:直接yum install rsync就行
[root@c7-docker ~]# yum info rsync          
Loaded plugins: fastestmirror               
Loading mirror speeds from cached hostfile  
 * base: mirror.bit.edu.cn                  
 * extras: mirrors.aliyun.com               
 * updates: mirror.bit.edu.cn               
Installed Packages                          
Name        : rsync                         
Arch        : x86_64                        
Version     : 3.1.2                         
Release     : 10.el7                        
Size        : 815 k                         
Repo        : installed                     
From repo   : base        

                  
[root@test-c7 ~]# ls cdh/                                                                                                                                                      
cdh-packs  mysql-rpms  rpm-deps

####### 1,传输目录: 带/----> 远程:只传输目录里面的内容,不是传送整个目录(没有该目录)
#######              不带/ -->远程:传输完整的目录和内容
[root@test-c7 ~]# rsync  -avz cdh root@docker:~/                                                                                                                              
root@docker's password:                                                                                                                                                        
sending incremental file list                                                                                                                                                  
./                                                                                                                                                                             
cdh-packs/                                                                                                                                                                     
cdh-packs/cloudera-manager-centos7-cm5.12.2_x86_64.tar.gz                                                                                                                      
cdh-packs/jdk-8u211-linux-x64.rpm                                                                                                                                              
mysql-rpms/                                                                                                                                                                    
mysql-rpms/MySQL-client-5.5.62-1.el7.x86_64.rpm                                                                                                                                
mysql-rpms/MySQL-devel-5.5.62-1.el7.x86_64.rpm                                                                                                                                 
mysql-rpms/MySQL-embedded-5.5.62-1.el7.x86_64.rpm                                                                                                                              
...                                                                                                                       
rpm-deps/                                                                                                                                                                      
rpm-deps/MySQL-python-1.2.5-1.el7.x86_64.rpm                                                                                                                                   
rpm-deps/apr-1.4.8-3.el7_4.1.x86_64.rpm                                                                                                                                        
...


######## 2,常用参数: -avz -b --delete
#-a: -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
#                                -r: 递归, -l:  --links 复制软连接, -p: --perms 保留权限,
#				 -t: --times 保留修改时间,	      -g: --group, -o: --owner    
#				 -z: --compress压缩传输		-b: --backup 如果目标文件不同,则做好备份
#				 --delete: 删除目录目录中的多余文件(源中不存在的文件/目录)
###### 目标目录: 有多余的目录和文件,使用--delete删除
[root@c7-docker ~]# mkdir cdh/aa
[root@c7-docker ~]# echo 123 > cdh/aa/a.txt
[root@c7-docker ~]# ls cdh/
cdh-packs  mysql-rpms  rpm-deps

##### 源文件处:传输参数
[root@test-c7 ~]# rsync   --delete -avz cdh root@docker:~/                         
root@docker's password:                                                            
sending incremental file list                                                      
deleting cdh/aa/a.txt                                                              
deleting cdh/aa/                                                                   
cdh/                                                                                                                                                                 
sent 5,186 bytes  received 51 bytes  2,094.80 bytes/sec                            
total size is 1,192,221,243  speedup is 227,653.47     

2,后台运行rsync服务

a, 配置文件

查看帮助手册:man rsyncd.conf

[root@c7-docker ~]# grep -Ev '^#|^$' /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.56.0/24
read only = false
fake super = true
max connections = 4
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
syslog facility = local5
pid file = /var/run/rsyncd.pid
[data1]
        path = /data/rsync
        comment = save rsync data
        auth users = a, b
        secrets file = /etc/rsyncd.secrets

#配置传输的:用户和密码
[root@c7-docker ~]# ll /etc/rsyncd.secrets                                     
-rw-------. 1 root root 12 Aug  6 07:46 /etc/rsyncd.secrets                    
[root@c7-docker ~]# cat /etc/rsyncd.secrets                                    
a:123                                                                          
b:456     

#文件存储目录: 需要先创建好
[root@c7-docker ~]# ll /data/rsync/ -d
drwxr-xr-x. 2 nobody nobody 19 Aug  6 08:36 /data/rsync/
[root@c7-docker ~]# ll /data/rsync/
total 4
-rw-r--r--. 1 nobody nobody 2 Aug  6 08:35 1.txt       

#启动服务
[root@c7-docker ~]# ll /etc/init.d/rsyncd
-rwxr-xr-x. 1 root root 378 Aug  6 08:58 /etc/init.d/rsyncd
[root@c7-docker ~]# cat /etc/init.d/rsyncd
	#!/bin/bash
	#description: self defined rsync daemon service
	#chkconfig: 90 90
	option=$1
	case $option in
	        start)
	                pid=$(ps -ef |grep -o  '.* rsync *--daemon' |awk '{print $2}')
	                [ ! -z $pid ] && kill $pid && sleep 1 &&  rsync --daemon
	                [ -z $pid ] && rsync --daemon
	        ;;
	        stop)
	                [ -f /var/run/rsyncd.pid ] && kill $(cat /var/run/rsyncd.pid)
	        ;;
	        status)
	
	                [ -f /var/run/rsyncd.pid ] && ps -ef |grep -o  '.* rsync *--daemon'
	esac
[root@c7-docker ~]# chkconfig rsyncd on
Note: Forwarding request to 'systemctl enable rsyncd.service'.
[root@c7-docker ~]# systemctl is-enabled rsyncd
enabled  
[root@c7-docker ~]# service rsyncd start
[root@c7-docker ~]# service rsyncd status
root     25998     1  0 08:58 ?        00:00:00 rsync --daemon                                                            

b, 查看rsync服务端: module名

[root@test-c7 test]# rsync -avz  1.txt  b@docker2::
data1           save rsync data
rsync-data2     save rsync data2

c, 从源端:发送数据

##源端:发送数据
#格式1
[root@test-c7 test]# echo test4 > 4.txt
[root@test-c7 test]# rsync -avz 4.txt b@docker::data1
Password:
sending incremental file list
4.txt
sent 93 bytes  received 35 bytes  36.57 bytes/sec
total size is 6  speedup is 0.05
#格式2
[root@test-c7 test]# rsync -avz  1.txt  rsync://b@docker2/rsync-data2
Password:
sending incremental file list
4.txt
sent 96 bytes  received 35 bytes  13.79 bytes/sec
total size is 8  speedup is 0.06


##目标端:查看
[root@c7-docker ~]# tail /var/log/rsyncd.log
2020/08/06 08:57:41 [25947] rsyncd version 3.1.2 starting, listening on port 873
2020/08/06 08:57:47 [25947] sent 0 bytes  received 0 bytes  total size 0
2020/08/06 08:57:48 [25974] rsyncd version 3.1.2 starting, listening on port 873
2020/08/06 08:58:57 [25974] sent 0 bytes  received 0 bytes  total size 0
2020/08/06 08:58:59 [25998] rsyncd version 3.1.2 starting, listening on port 873
2020/08/06 09:03:08 [26098] name lookup failed for 192.168.56.171: Name or service not known
2020/08/06 09:03:08 [26098] connect from UNKNOWN (192.168.56.171)
2020/08/06 09:03:11 [26098] rsync to data1/ from b@UNKNOWN (192.168.56.171)
2020/08/06 09:03:11 [26098] receiving file list
2020/08/06 09:03:11 [26098] sent 40 bytes  received 101 bytes  total size 6
[root@c7-docker ~]# ll /data/rsync/
total 8
-rw-r--r--. 1 nobody nobody 2 Aug  6 08:35 1.txt
-rw-r--r--. 1 nobody nobody 6 Aug  6 09:03 4.txt


##源端:免交换发送数据 --password-file=FILE
##The file should contain just the password on the first line (all other lines are ignored). Rsync will exit with an error if FILE is world readable or if a root-run rsync command finds a non-root-owned file.
[root@test-c7 test]# echo 456 > /etc/rsyncd.secrets
[root@test-c7 test]# chmod 600 /etc/rsyncd.secrets

[root@test-c7 test]# echo test456 > 4.txt
[root@test-c7 test]# rsync -az --password-file=/etc/rsyncd.secrets 4.txt b@docker::data1

3,实时备份:inotify + rsync

参考:https://www.cnblogs.com/clsn/p/8022625.html

a, 使用inotifywait 监控文件变化

在这里插入图片描述

b, 在监控到文件变化时,同步该文件

man inotifywait 查看帮助
在这里插入图片描述

[root@test-c7 data]# cat /mysync.sh
inotifywait -mrq /data --format "%w%f"  -e create,delete,moved_to,close_write|\
while read line
do
        rsync -az --delete /data/ --password-file=/etc/rsyncd.secrets a@docker::data1
done

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/eyeofeagle/article/details/107843557