1. 程式人生 > >Linux文件同步工具-rsync

Linux文件同步工具-rsync

監聽端口 屬於 auth delete ret 日誌 roo delet sta

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 [email protected]:/tmp/2.txt
將/etc/passwd文件同步備份到192.168.188.2機器的/tmp/2.txt

[root@shu-test ~]# 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
[root@shu-test ~]# ls /tmp/
1.txt
2.txt
[root@shu-test ~]#

備份到遠程計算機(ssh被修改端口情況下)

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

[root@shu-test 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
[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]#

同步到其他機器方法

通過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