hellopasswd
文件同步工具rsync
[root@localhost ~]# yum install -y rsyncLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * epel: mirrors.tongji.edu.cnResolving Dependencies--> Running transaction check---> Package rsync.x86_64 0:3.0.9-18.el7 will be installed--> Finished Dependency ResolutionDependencies Resolved=================================================================================================== Package Arch Version Repository Size===================================================================================================Installing: rsync x86_64 3.0.9-18.el7 base 360 kTransaction Summary===================================================================================================Install 1 PackageTotal download size: 360 kInstalled size: 732 kDownloading packages:rsync-3.0.9-18.el7.x86_64.rpm | 360 kB 00:00:01 Running transaction checkRunning transaction testTransaction test succeededRunning transaction Installing : rsync-3.0.9-18.el7.x86_64 1/1 Verifying : rsync-3.0.9-18.el7.x86_64 1/1 Installed: rsync.x86_64 0:3.0.9-18.el7 Complete!
[root@localhost ~]# rsync -av /etc/passwd /tmp/1.txtsending incremental file listpasswdsent 1506 bytes received 31 bytes 3074.00 bytes/sectotal size is 1432 speedup is 0.93
rsync常用选项
- -a包含-rtplgoD
- -r同步目录时要加上,类似cp时的-r选项
- -v同步时显示一些信息,让我们知道同步的过程
- -l保留软链接
- -L加上该选项后,同步软链接时会把源文件给同步
- -p保持文件的权限属性
- -o保持文件的属主
- -g保持文件的属组
- -D保持设备文件信息
- -t保持文件的时间属性
- --delete删除DEST中SRC没有的文件
- --exclude过滤指定文件,如--exclude "logs"会把文件名包含logs的文件或目录过滤掉,不同步
- -P显示同步过程,比如速率,比-v更加详细
- -u加上该选项后,如果DEST中的文件比SRC新,则不同步
- -z传输时压缩
-av
[root@localhost ~]# ls 111 anaconda-ks.cfg[root@localhost ~]# rsync -av /root/111/ /tmp/111_dest/sending incremental file listcreated directory /tmp/111_dest./1.txt2.txt3.txtsent 192 bytes received 72 bytes 528.00 bytes/sectotal size is 0 speedup is 0.00[root@localhost ~]# ls 111 anaconda-ks.cfg[root@localhost ~]# ls /tmp/111_dest ipt.txt
--delete
[root@localhost ~]# touch /tmp/111_dest/new.txt[root@localhost ~]# rsync -av /root/111/ /tmp/111_dest/sending incremental file list./sent 75 bytes received 15 bytes 180.00 bytes/sectotal size is 0 speedup is 0.00[root@localhost ~]# ls /tmp/111_dest/1.txt 2.txt 3.txt new.txt
[root@localhost ~]# touch /tmp/111_dest/new.txt[root@localhost ~]# rsync -av --delete /root/111/ /tmp/111_dest/sending incremental file list./deleting new.txtsent 75 bytes received 15 bytes 180.00 bytes/sectotal size is 0 speedup is 0.00[root@localhost ~]# ls /tmp/111_dest/1.txt 2.txt 3.txt
--exclude
[root@localhost ~]# rm -rf /tmp/111_dest/*[root@localhost ~]# rsync -av --exclude "1*" /root/111/ /tmp/111_dest/sending incremental file list./2.txt3.txtsent 139 bytes received 53 bytes 384.00 bytes/sectotal size is 0 speedup is 0.00[root@localhost ~]# ls /tmp/111_dest/2.txt 3.txt
-avP
[root@localhost ~]# rm -rf /tmp/111_dest/*[root@localhost ~]# rsync -avP /root/111/ /tmp/111_dest/sending incremental file list./1.txt 18 100% 0.00kB/s 0:00:00 (xfer#1, to-check=2/4)2.txt 12 100% 11.72kB/s 0:00:00 (xfer#2, to-check=1/4)3.txt 6 100% 5.86kB/s 0:00:00 (xfer#3, to-check=0/4)sent 240 bytes received 72 bytes 624.00 bytes/sectotal size is 36 speedup is 0.12
-avPu
[root@localhost ~]# echo "hello" > 111/1.txt [root@localhost ~]# rsync -avP /root/111/ /tmp/111_dest/sending incremental file list1.txt 6 100% 0.00kB/s 0:00:00 (xfer#1, to-check=2/4)sent 121 bytes received 31 bytes 304.00 bytes/sectotal size is 24 speedup is 0.16[root@localhost ~]# cat /tmp/111_dest/1.txt hello
[root@localhost ~]# echo "hello" >> !$echo "hello" >> /tmp/111_dest/1.txt[root@localhost ~]# rsync -avPu /root/111/ /tmp/111_dest/sending incremental file listsent 72 bytes received 12 bytes 168.00 bytes/sectotal size is 24 speedup is 0.29[root@localhost ~]# cat /tmp/111_dest/1.txt hellohello[root@localhost ~]# cat 111/1.txt hello
rsync通过ssh同步
- rsync -av test1/ 192.168.133.132:/tmp/test2/
- rsync -av -e "ssh -p 22" test1/ 192.168.133.132:/tmp/test2/
- rsync通过服务的方式同步
- 要编辑配置文件/etc/rsyncd.conf
- 启动服务rsync --daemon
- 格式:rsync -av test1/ 192.168.133.130::module/dir/
Linux主机A:192.168.9.134
[root@localhost ~]# rsync -avP /root/111/ root@192.168.9.233:/root/111_new/The authenticity of host '192.168.9.223 (192.168.9.223)' can't be established.ECDSA key fingerprint is 36:5f:cc:8b:04:f9:7b:2e:0f:8c:39:38:37:c3:d2:3a.Are you sure you want to continue connecting (yes/no)? yPlease type 'yes' or 'no': yes Warning: Permanently added '192.168.9.223' (ECDSA) to the list of known hosts.root@192.168.9.223's password: bash: rsync: command not foundrsync: connection unexpectedly closed (0 bytes received so far) [sender]rsync error: remote command not found (code 127) at io.c(605) [sender=3.0.9]
主机B必须也要安装rsync包
[root@localhost ~]# rsync -avP /root/111/ root@192.168.9.233:/root/111_new/root@192.168.9.233's password: sending incremental file listcreated directory /root/111_new./1.txt 6 100% 0.00kB/s 0:00:00 (xfer#1, to-check=2/4)2.txt 12 100% 11.72kB/s 0:00:00 (xfer#2, to-check=1/4)3.txt 6 100% 5.86kB/s 0:00:00 (xfer#3, to-check=0/4)sent 228 bytes received 72 bytes 120.00 bytes/sectotal size is 24 speedup is 0.08
Linux主机B:192.168.9.223
[root@centos11233 ~]# ls111_new anaconda-ks.cfg
推文件与拉文件
Linux主机A:192.168.9.134
[root@localhost ~]# rsync -avP root@192.168.9.233:/root/111_new/ /root/111_dest/root@192.168.9.233's password: receiving incremental file listcreated directory /root/111_dest./1.txt 6 100% 5.86kB/s 0:00:00 (xfer#1, to-check=2/4)2.txt 12 100% 11.72kB/s 0:00:00 (xfer#2, to-check=1/4)3.txt 6 100% 5.86kB/s 0:00:00 (xfer#3, to-check=0/4)sent 71 bytes received 233 bytes 67.56 bytes/sectotal size is 24 speedup is 0.08[root@localhost ~]# ls111 111_dest anaconda-ks.cfg
指定端口-e "ssh -p 端口号"
[root@localhost ~]# rsync -avP -e "ssh -p 22" /etc/passwd root@192.168.9.233:/root/1.txtroot@192.168.9.233's password: sending incremental file listpasswd 1432 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)sent 1506 bytes received 31 bytes 614.80 bytes/sectotal size is 1432 speedup is 0.93
扩展
[root@localhost ~]# ssh -p 22 192.168.9.233root@192.168.9.233's password: Last failed login: Wed Dec 6 03:34:39 CST 2017 from 192.168.9.134 on ssh:nottyThere was 1 failed login attempt since the last successful login.Last login: Wed Dec 6 00:33:06 2017 from 192.168.9.1[root@centos11233 ~]# [root@centos11233 ~]# exitlogoutConnection to 192.168.9.233 closed.
修改于 171205