rsync命令是一个远程数据同步工具,可通过lan/wan快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。 rsync是一个功能非常强大的工具,其命令也有很多功能特色选项,我们下面就对它的选项一一进行分析说明。
能更新整个目录和树和文件系统;
有选择性的保持符号链链、硬链接、文件属于、权限、设备以及时间等;
对于安装来说,无任何特殊权限要求;
对于多个文件来说,内部流水线减少文件等待的延时;
能用rsh、ssh 或直接端口做为传输入端口;
支持匿名rsync 同步文件,是理想的镜像工具;
yum -y install rsync
apt-get -y install rsync
验证是否安装成功
命令:rsync --version #查看rsync版本
[root@localhost ~]# rsync --version rsync version 3.1.2 protocol version 31 copyright (c) 1996-2015 by andrew tridgell, wayne davison, and others. web site: http://rsync.samba.org/ capabilities: 64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints, socketpairs, hardlinks, symlinks, ipv6, batchfiles, inplace, append, acls, xattrs, iconv, symtimes, prealloc rsync comes with absolutely no warranty. this is free software, and you are welcome to redistribute it under certain conditions. see the gnu general public licence for details.
在本地机器上同步两个目录,使用==rsync -zvr==命令
[root@localhost ~]# rsync -zvr /opt/seafile-data/ /mnt/ sending incremental file list db/ db/aria_log.00000001 db/aria_log_control db/ib_logfile0
上述命令中:
-z 打开压缩功能
-v verbose更多打印信息
-r recursive
执行上述命令后,你会发现rsync copy会影响到文件的timestamp信息,这时因为默认rsync并不保护timestamp信息
注意:
/opt/seafile-data/ 这种写法将同步seafile-data文件夹下所有文件,不包括seafile-data文件夹
/opt/seafile-data 这种写法将同步seafile-data整个文件夹,包括seafile-data文件夹
[root@localhost ~]# rsync -avz /opt/seafile-data /mnt/ sending incremental file list seafile-data/ seafile-data/db/ seafile-data/db/aria_log.00000001 seafile-data/db/aria_log_control seafile-data/db/ib_logfile0
注意这时你会发现source,dest文件的时间戳、属主属组等信息是不变的
[root@localhost ~]# ls -l /opt/seafile-data/logs/seafile/seahub.log /mnt/seafile-data/logs/seafile/seahub.log -rw-r--r-- 1 root root 15858 3月 21 18:23 /mnt/seafile-data/logs/seafile/seahub.log -rw-r--r-- 1 root root 15858 3月 21 18:23 /opt/seafile-data/logs/seafile/seahub.log
只要在rsync命令中指定文件名称即可
[root@localhost ~]# rsync -v /opt/seafile-data/seafile/conf/install.sh /mnt/ install.sh sent 19,858 bytes received 35 bytes 39,786.00 bytes/sec total size is 19,776 speedup is 0.99 ll /mnt/install.sh -rw-r--r-- 1 root root 19776 3月 25 14:05 /mnt/install.sh
[root@localhost ~]# rsync -avz /opt/seafile-data root@10.20.29.38:/mnt/ root@10.20.29.38's password: 输入远端服务器密码 sending incremental file list seafile-data/ seafile-data/db/ seafile-data/db/aria_log.00000001 seafile-data/db/aria_log_control ...... ...... sent 1,809,853,100 bytes received 10,547 bytes 28,501,789.72 bytes/sec total size is 2,005,006,840 speedup is 1.11
当执行和remote server同步的动作时,你需要指定username,ip。也要指定远程服务器上的目的地目录,格式是: username@machineip:path
即格式为:
==rsync -avz 本地需要同步文件的绝对路径 远端用户名@远端ip:远端文件保存的绝对路径==
这个过程中,rsync会要求输入密码。但是如果你有一个脚本自动运行这个备份动作,你可能希望不要手动输入密码,即
[root@localhost ~]# rsync -avz root@10.20.29.38:/mnt/seafile-data /mnt/ root@10.20.29.38's password: 输入密码 receiving incremental file list seafile-data/ seafile-data/db/ seafile-data/db/aria_log.00000001 ...... ...... sent 4,698 bytes received 1,785,742,775 bytes 31,056,477.79 bytes/sec total size is 2,005,006,840 speedup is 1.12
如对本文有疑问, 点击进行留言回复!!
linux下文本编辑器vim的使用方法(复制、粘贴、替换、行号、撤销、多文件操作)
网友评论