默认未安装,使用yum install -y rsyncpython
拷贝一个目录的数据到另外一个目录,拷贝的同时目录的内容处在不断更新中,在这里使用cp命令就可能会有些问题:没法实现增量拷贝,浪费时间;屡次全量拷贝,对磁盘的读写压力大;bash
rsync命令就十分使用于这样的状况,能够实现增量备份ssh
[root@localhost system]# rysnc -av /etc/passwd root@192.168.65.133:/tmp/1.txt -bash: rysnc: 未找到命令 [root@localhost system]# rsync -av /etc/passwd root@192.168.65.133:/tmp/1.txt The authenticity of host '192.168.65.133 (192.168.65.133)' can't be established. ECDSA key fingerprint is 42:50:a7:09:91:db:af:77:a5:3a:b3:67:1c:8a:5b:99. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.65.133' (ECDSA) to the list of known hosts. root@192.168.65.133's password: sending incremental file list passwd sent 1156 bytes received 31 bytes 67.83 bytes/sec total size is 1082 speedup is 0.91
# 指定远程主机的格式能够不指定用户名,默认是指远程主机上的当前用户 [root@localhost ~]# rsync -av /etc/passwd root@192.168.65.130:/tmp/1.txt root@192.168.65.133's password: sending incremental file list sent 31 bytes received 12 bytes 3.19 bytes/sec total size is 1128 speedup is 26.23
[root@localhost ~]# rsync -av /root/111/ /tmp/111_dest/ sending incremental file list created directory /tmp/111_dest ./ file.py test -> /etc/passwd try.py 222/ sent 576 bytes received 60 bytes 1272.00 bytes/sec total size is 382 speedup is 0.60 [root@localhost ~]# ls -l 111/ 总用量 8 drwxr-xr-x. 2 root root 6 10月 27 19:49 222 -rw-r--r--. 1 root root 146 10月 27 19:45 file.py lrwxrwxrwx. 1 root root 11 10月 27 19:49 test -> /etc/passwd -rw-r--r--. 1 root root 225 10月 27 19:45 try.py [root@localhost ~]# ls -l /tmp/111_dest/ 总用量 8 drwxr-xr-x. 2 root root 6 10月 27 19:49 222 -rw-r--r--. 1 root root 146 10月 27 19:45 file.py lrwxrwxrwx. 1 root root 11 10月 27 19:49 test -> /etc/passwd -rw-r--r--. 1 root root 225 10月 27 19:45 try.py
[root@localhost ~]# rsync -avL /root/111/ /tmp/111_dest/ sending incremental file list test sent 1265 bytes received 32 bytes 2594.00 bytes/sec total size is 1499 speedup is 1.16 [root@localhost ~]# ls -l /tmp/111_dest/ 总用量 12 drwxr-xr-x. 2 root root 6 10月 27 19:49 222 -rw-r--r--. 1 root root 146 10月 27 19:45 file.py -rw-r--r--. 1 root root 1128 10月 23 20:22 test -rw-r--r--. 1 root root 225 10月 27 19:45 try.py [root@localhost ~]# ls -l 111/ 总用量 8 drwxr-xr-x. 2 root root 6 10月 27 19:49 222 -rw-r--r--. 1 root root 146 10月 27 19:45 file.py lrwxrwxrwx. 1 root root 11 10月 27 19:49 test -> /etc/passwd -rw-r--r--. 1 root root 225 10月 27 19:45 try.py
[root@localhost ~]# rsync --delete -avL /root/111/ /tmp/111_dest/ sending incremental file list ./ deleting 1.txt sent 97 bytes received 16 bytes 226.00 bytes/sec total size is 1499 speedup is 13.27
[root@localhost ~]# rsync -avL --exclude "*.py" /root/111/ /tmp/111_dest/ sending incremental file list created directory /tmp/111_dest ./ test 222/ sent 1244 bytes received 38 bytes 2564.00 bytes/sec total size is 1128 speedup is 0.88
[root@localhost ~]# rsync -aP /root/111/ /tmp/111_dest/ sending incremental file list created directory /tmp/111_dest ./ file.py 146 100% 0.00kB/s 0:00:00 (xfer#1, to-check=3/5) test -> /etc/passwd try.py 225 100% 219.73kB/s 0:00:00 (xfer#2, to-check=1/5) 222/ sent 576 bytes received 60 bytes 1272.00 bytes/sec total size is 382 speedup is 0.60
# 修改目标的文件内容,使目标的文件比源新 [root@localhost ~]# vi /tmp/111_dest/try.py # 使用-u参数,防止源中文件同步过来 [root@localhost ~]# rsync -avu /root/111/ /tmp/111_dest/ sending incremental file list ./ sent 113 bytes received 16 bytes 258.00 bytes/sec total size is 382 speedup is 2.96 # 验证 # 目标文件添加一行注释,较源文件更新 [root@localhost ~]# cat /tmp/111_dest/try.py #!/usr/bin/env python # test try: filename = raw_input('Enter file name: ') fobj = open(filename, 'r') for eachLine in fobj: print eachLine, fobj.close() except IOError as e: print "file open error:", e [root@localhost ~]# cat 111/try.py #!/usr/bin/env python try: filename = raw_input('Enter file name: ') fobj = open(filename, 'r') for eachLine in fobj: print eachLine, fobj.close() except IOError as e: print "file open error:", e
[root@localhost ~]# rsync -avPz /root/111/ /tmp/111_dest/ sending incremental file list try.py 225 100% 0.00kB/s 0:00:00 (xfer#1, to-check=1/5) sent 297 bytes received 32 bytes 658.00 bytes/sec total size is 382 speedup is 1.16
其余的几个参数(因为-a默认包括了不少参数,因此几乎使用-a替代了)工具
P、L等参数配合-a参数使用,会替代默认的p、l参数功能spa
前提:本机和远程主机都须要安装rsynccode
经过ssh访问来同步文件/目录的几种写法:ci
[root@localhost ~]# rsync -av /etc/passwd 192.168.65.130:/tmp/1.txt
[root@localhost ~]# rsync -av 192.168.65.130:/etc/passwd /tmp/1.txt
# 这里假设远端ssh端口为66 [root@localhost ~]# rsync -av -e "ssh -p 66" /etc/passwd 192.168.133.130:/tmp/1.txt