标签:添加文件 属性 1.0 let 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文件中
[root@shu-test ~]# 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
[root@shu-test ~]# ls /tmp/
1.txt
rsync -av /etc/passwd root@192.168.188.2:/tmp/2.txt
将/etc/passwd文件同步备份到192.168.188.2机器的/tmp/2.txt
[root@shu-test ~]# rsync -av /etc/passwd root@192.168.188.2:/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.
root@192.168.188.2‘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
[root@shu-test ~]# ls /tmp/
1.txt
2.txt
[root@shu-test ~]#
rsync -avP -e "ssh -p 22" /root/abc/ root@192.168.188.3:/tmp/test2/
[root@shu-test tmp]# rsync -avP -e "ssh -p 22" /root/abc/ root@192.168.188.3:/tmp/test2/
root@192.168.188.3‘s password:
sending incremental file list
sent 365 bytes received 19 bytes 109.71 bytes/sec
total size is 255434 speedup is 665.19
[root@shu-test tmp]#
注意:备份到远程服务器上,双方服务器都必须安装rsync包,才能备份;
rsync -av /root/abc/ /tmp/111_dest/
[root@shu-test ~]# 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
[root@shu-test ~]# cd /tmp/
[root@shu-test 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
[root@shu-test tmp]#
rsync -av --delete /root/abc/ /tmp/111_dest/
将/root/abc/ 文件夹中的文件备份到/tmp/111_dest/目录下,
并删除/tmp/111_dest/中不属于/root/abc/的文件;
[root@shu-test 111_dest]# touch new.txt
[root@shu-test 111_dest]# ls
1.txt 2.txt 3.txt a ip.txt new.txt test
[root@shu-test 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
[root@shu-test 111_dest]# ls
1.txt 2.txt 3.txt a ip.txt test
[root@shu-test 111_dest]#
rsync -av --exclude ".123" /root/abc/ /tmp/111_dest/
备份/root/abc/到/tmp/111_dest/,但是不把.123的文件备份过去;
[root@shu-test abc]# ls
1.txt 2.txt 3.txt a a.123 ip.txt test
[root@shu-test abc]# cat a.123
[root@shu-test 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
[root@shu-test abc]# cd /tmp/111_dest/
[root@shu-test 111_dest]# ls
1.txt 2.txt 3.txt a ip.txt test
[root@shu-test 111_dest]#
[root@shu-test 111_dest]# ls /root/abc/
1.txt 2.txt 3.txt a a.123 ip.txt test
[root@shu-test 111_dest]#
rsync -avu /root/abc/ /tmp/111_dest/
如果/tmp/111_dest/中有比源文件新的文件,则保护新文件不被同步干掉;
并将/root/abc/ 同步到/tmp/111_dest/;
[root@shu-test abc]# cd /tmp/111_dest/
[root@shu-test 111_dest]# touch new.new
[root@shu-test 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
[root@shu-test 111_dest]# ls
1.txt 2.txt 3.txt a a.123 ip.txt new.new test
[root@shu-test 111_dest]# ls /root/abc/
1.txt 2.txt 3.txt a a.123 ip.txt test
[root@shu-test 111_dest]#
需要输入密码
rsync -avL /root/abc/ root@192.168.188.3:/tmp/test2/
或者
rsync -avL root@192.168.188.3:/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
未完、明日待续
标签:添加文件 属性 1.0 let rsync常用选项 默认 同步工具 监听端口 密码
原文地址:http://blog.51cto.com/shuzonglu/2067054