1. 程式人生 > >rsync 同步命令

rsync 同步命令

.tar.gz zed 目標 keygen gen font tin copy ner

rsync同步命令

同步操作

rsync [選項..] 源目錄 目標目錄

同步與復制的差異

復制:完成拷貝源到目標

同步:增量拷貝,只傳輸變化過的數據

本地同步

rsync [選項...] 本地目錄1 本地目錄2 #同步整個文件夾

rsync [選項...] 本地目錄1/ 本地目錄2 #只同步目錄的數據

操作選項

-n : 測試同步過程,不做實際修改

--delete:刪除目標文件夾多余的文檔

-a :歸檔模式,相當於-rlptgoD

-v :顯示詳細操作信息

-z :傳輸過程中啟動壓縮/解壓




[root@test ]# mkdir /abc

[root@test ]# mkdir /test

[root@test ]# cp /etc/passwd /etc/fstab /etc/shadow /etc/group /abc

[root@test ]# rsync -avz /abc /test

sending incremental file list

abc/

abc/fstab

abc/group

abc/passwd

abc/shadow

sent 2596 bytes received 92 bytes 5376.00 bytes/sec

total size is 5551 speedup is 2.07

[root@test ]# ls /test

abc

[root@test ]# rsync -avz /abc/ /test

sending incremental file list

./

fstab

group

passwd

shadow

sent 2581 bytes received 91 bytes 5344.00 bytes/sec

total size is 5551 speedup is 2.08

[root@test ]# ls /test

abc fstab group passwd shadow

[root@test ]# echo 123 >> /abc/group

[root@test ]# rsync -avz /abc/ /test

sending incremental file list

group

sent 668 bytes received 31 bytes 1398.00 bytes/sec

total size is 5555 speedup is 7.95

[root@test ]# touch /test/test.txt

[root@test ]# rsync -avz --delete /abc/ /test

sending incremental file list

./

deleting abc/shadow

deleting abc/passwd

deleting abc/group

deleting abc/fstab

deleting abc/

deleting test.txt

sent 87 bytes received 15 bytes 204.00 bytes/sec

total size is 5555 speedup is 54.46



rsync+ssh同步

下行:rsync [...] user@host:遠程目錄

上行:rsync [...] 本地目錄 user@host:遠程目錄


[root@test ~]# ls /abc/

fstab group passwd shadow

[root@test ~]# rsync -avz --delete /abc/ [email protected]:/opt/

[email protected]'s password:

sending incremental file list

./

deleting rh/

fstab

group

passwd

shadow

sent 2590 bytes received 91 bytes 766.00 bytes/sec

[root@pc207 ~]# ls /opt/

fstab group passwd shadow


實時同步

1.密碼驗證取消,采用公鑰 私鑰 驗證


[root@test ~]# ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

45:3d:84:d0:ac:08:4b:9b:84:a5:b4:fc:47:eb:7f:98 [email protected]

The key's randomart image is:

+--[ RSA 2048]----+

| .o. .+.+. |

| o.o+ .+ o |

| +o =.. .. . |

| .+..... |

| . o S |

| o |

| . o |

| .E . |

| .. |

+-----------------+


[root@test ~]# ls /root/.ssh #公私鑰存放地址

id_rsa id_rsa.pub known_hosts

[root@test ~]# ssh-copy-id [email protected]

[root@pc207 ~]# ls /root/.ssh

authorized_keys

2.驗證

[root@test ~]# ssh [email protected]

Last login: Thu Nov 16 20:07:55 2017 from 192.168.4.254

[root@pc207 ~]# exit

3.inotify實時監控目錄,內容是否變化

inotify-tools-3.13.tar.gz

[root@test ~]# rm -rf /opt/*

[root@test ~]# tar -xf /tools/inotify-tools-3.13.tar.gz -C /opt/

[root@test ~]# ls /opt/

inotify-tools-3.13

[root@test ~]# cd /opt/inotify-tools-3.13/

[root@test inotify-tools-3.13]# ./configure

[root@test inotify-tools-3.13]# make

[root@test inotify-tools-3.13]# make install

[root@test inotify-tools-3.13]# inotifywait

No files specified to watch!

inotify基本用法

inotifywait [選項 ] 目標文件

常用選項

-m:

-r

-q

-e


[root@test /]# inotifywait -mrq /opt/

/opt/ CREATE 1.txt

/opt/ OPEN 1.txt

/opt/ ATTRIB 1.txt

/opt/ CLOSE_WRITE,CLOSE 1.txt

/opt/ CREATE 2.txt

/opt/ OPEN 2.txt

/opt/ ATTRIB 2.txt

/opt/ CLOSE_WRITE,CLOSE 2.txt

^C

[root@test /]#


Shell腳本

[root@test ~]# vim /root/look.sh


#!/bin/bash

while inotifywait -rqq /abc

do

rsync -az --delete /abc/ [email protected]:/opt/

done

[root@test ~]# chmod +x /root/look.sh


[root@test ~]# /root/look.sh &

[1] 19200


rsync 同步命令