Linux文件同步工具-rsync

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux文件同步工具-rsync相关的知识,希望对你有一定的参考价值。

Linux文件同步工具-rsync

安装包


yum install -y rsync

rsync常用选项

-a:归档模式,表示递归方式传输文件,并保持所有属性;通-rlptgoD;
-r:同步目录时要加上,类似cp时加R;
-v:同步时显示一些信息,让我们知道同步国创;
-l:保留软链接;
-L:同步软链接时会把源文件给同步;
-p:保持文件权限属性;
-o:保持文件的属主;
-g:保持文件的属组;
-D:保持设备文件信息;
-t:保持文件的时间属性;
--delte:删除DEST中SRC没有的文件;
--exclude:过滤指定文件,如--exclude “logs”会把文件log过滤掉,不同步;
-P:显示同步过程,比如速率,比-v更加详细;
-u:如果DEST中的文件比SRC新,则不同步;
-z:测试时压缩;

备份

rsync -av /etc/passwd /tmp/1.txt
将/etc/passwd备份到tmp/1.txt文件中

[[email protected] ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd
sent 1229 bytes  received 31 bytes  2520.00 bytes/sec
total size is 1155  speedup is 0.92
[[email protected] ~]# ls /tmp/
1.txt

备份到远程计算机

rsync -av /etc/passwd [email protected]:/tmp/2.txt
将/etc/passwd文件同步备份到192.168.188.2机器的/tmp/2.txt

[[email protected] ~]# rsync -av /etc/passwd [email protected]:/tmp/2.txt
The authenticity of host ‘192.168.188.2 (192.168.188.2)‘ can‘t be established.
ECDSA key fingerprint is SHA256:vl9780juEAW/PspOqj/J4E4yHiQtIG6ZQ2R9S1OAdb8.
ECDSA key fingerprint is MD5:f8:71:01:1c:c1:71:88:97:d8:ca:25:88:79:45:17:1b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.188.2‘ (ECDSA) to the list of known hosts.
[email protected]‘s password:
sending incremental file list
passwd
sent 1229 bytes  received 31 bytes  168.00 bytes/sec
total size is 1155  speedup is 0.92
[[email protected] ~]# ls /tmp/
1.txt
2.txt
[[email protected] ~]#

备份到远程计算机(ssh被修改端口情况下)

rsync -avP -e "ssh -p 22" /root/abc/ [email protected]:/tmp/test2/

[[email protected] tmp]# rsync -avP -e "ssh -p 22" /root/abc/ [email protected]:/tmp/test2/
[email protected]‘s password:
sending incremental file list
sent 365 bytes  received 19 bytes  109.71 bytes/sec
total size is 255434  speedup is 665.19
[[email protected] tmp]#

注意:备份到远程服务器上,双方服务器都必须安装rsync包,才能备份;

同步文件夹

rsync -av /root/abc/ /tmp/111_dest/

[[email protected] ~]# rsync -av /root/abc/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
1.txt
2.txt
3.txt
ip.txt
a/
a/1.txt
a/2.txt
a/b/
a/b/2.txt
a/b/c/
a/k/
a/k11/
a/s/
test/
test/1.txt
test/2.txt
test/a.txt
sent 256259 bytes  received 233 bytes  512984.00 bytes/sec
total size is 255434  speedup is 1.00
[[email protected] ~]# cd /tmp/
[[email protected] tmp]# ls
111_dest  systemd-private-603500798f13486a90f57d6e2dd53912-chronyd.service-UCHrHd
1.txt     systemd-private-603500798f13486a90f57d6e2dd53912-vgauthd.service-UNh9hx
2.txt     systemd-private-603500798f13486a90f57d6e2dd53912-vmtoolsd.service-JJIguS
[[email protected] tmp]#

删除备份目录中不属于源的文件

rsync -av --delete /root/abc/ /tmp/111_dest/
将/root/abc/ 文件夹中的文件备份到/tmp/111_dest/目录下,
并删除/tmp/111_dest/中不属于/root/abc/的文件;

[[email protected] 111_dest]# touch new.txt
[[email protected] 111_dest]# ls
1.txt  2.txt  3.txt  a  ip.txt  new.txt  test
[[email protected] 111_dest]# rsync -av --delete /root/abc/ /tmp/111_dest/
sending incremental file list
./
deleting new.txt
sent 354 bytes  received 22 bytes  752.00 bytes/sec
total size is 255434  speedup is 679.35
[[email protected] 111_dest]# ls
1.txt  2.txt  3.txt  a  ip.txt  test
[[email protected] 111_dest]#

过滤指定文件不同步

rsync -av --exclude ".123" /root/abc/ /tmp/111_dest/
备份/root/abc/到/tmp/111_dest/,但是不把
.123的文件备份过去;

[[email protected] abc]# ls
1.txt  2.txt  3.txt  a  a.123  ip.txt  test
[[email protected] abc]# cat a.123
[[email protected] abc]# rsync -av --exclude "*.123" /root/abc/ /tmp/111_dest/
sending incremental file list
./
sent 354 bytes  received 22 bytes  752.00 bytes/sec
total size is 255434  speedup is 679.35
[[email protected] abc]# cd /tmp/111_dest/
[[email protected] 111_dest]# ls
1.txt  2.txt  3.txt  a  ip.txt  test
[[email protected] 111_dest]#
[[email protected] 111_dest]# ls /root/abc/
1.txt  2.txt  3.txt  a  a.123  ip.txt  test
[[email protected] 111_dest]#

保护备份后的文件中的新文件

rsync -avu /root/abc/ /tmp/111_dest/
如果/tmp/111_dest/中有比源文件新的文件,则保护新文件不被同步干掉;
并将/root/abc/ 同步到/tmp/111_dest/;

[[email protected] abc]# cd /tmp/111_dest/
[[email protected] 111_dest]# touch new.new
[[email protected] 111_dest]# rsync -avu /root/abc/ /tmp/111_dest/
sending incremental file list
./
sent 368 bytes  received 22 bytes  780.00 bytes/sec
total size is 255434  speedup is 654.96
[[email protected] 111_dest]# ls
1.txt  2.txt  3.txt  a  a.123  ip.txt  new.new  test
[[email protected] 111_dest]# ls /root/abc/
1.txt  2.txt  3.txt  a  a.123  ip.txt  test
[[email protected] 111_dest]#

同步到其他机器方法

通过ssh的方法

需要输入密码
rsync -avL /root/abc/ [email protected]:/tmp/test2/
或者
rsync -avL [email protected]:/tmp/test2/ /root/abc/

通过后台服务的方法

编辑配置文件

/etc/rsyncd.conf

添加文件内容

port=873        //监听端口873,默认为873端口;
log file=/var/log/rsync.log        //指定日志文件
pid file=/var/run/rsyncd.pid        //指定pid文件
address=192.168.188.2        //指定启动rsyncd服务的ip,默认为全部ip上启动;
[test]        //指定模块名,自定义;
path=/root/rsync        //指定数据存放路径
use chroot=true        //安全仿佛,默认为true,如果有软链接文件建议设置为false;
max connections=4        //指定最大连接数,默认为0,没有限制;
read only=no        //如果为true,则不能上传到该模块指定目录
list=true        //
uid=root        //指定传输时使用哪个用户名进行
gid=root        //指定传输时使用哪个用户组
auth users=test        //指定传输时要使用哪个用户名
secrets file=/etc/rsyncd.passwd    //指定密码文件,文件权限为600,格式:用户名:密码
hosts allow=192.168.133.132 1.1.1.1 2.2.2.2  192.168.133.0/24        //

启动服务

rsync --daemon

同步格式:

未完、明日待续

以上是关于Linux文件同步工具-rsync的主要内容,如果未能解决你的问题,请参考以下文章

Linux文件同步工具-rsync

Linux的文件同步工具rsync实战

Linux的rsync远程数据同步工具

linux-日常运维-文件同步工具-rsync

Linux centos7 rsync工具介绍rsync常用选项rsync通过ssh同步

Linux 学习总结(90)—— Linux 远程数据同步工具 Rsync(remote synchronize)详解