原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://nolinux.blog.51cto.com/4824967/1431426
環境 CentOS6.5 x86_64位 采用最小化安裝,系統經過了基本優化 selinux為關閉狀態,iptables為無限制模式 源碼包存放位置:/root
[root@rsync-server ~]# rpm -qa|grep rsync rsync-3.0.6-9.el6_4.1.x86_64
[root@rsync-server ~]# yum install rsync -y
[root@rsync-server ~]# yum install -y kernel-headers glibc-headers glibc-devel libgomp gcc gcc-c++
[root@rsync-server ~]# wget http://rsync.samba.org/ftp/rsync/rsync-3.1.1.tar.gz [root@rsync-server ~]# tar zxvf rsync-3.1.1.tar.gz [root@rsync-server ~]# cd rsync-3.1.1 [root@rsync-server rsync-3.1.1]# ./configure --prefix=/usr/local/rsync[root@rsync-server rsync-3.1.1]# make && make install
[root@rsync-server /]# rsync /rsync drwxr-xr-x 4096 2014/06/04 00:45:06 rsync [root@rsync-server /]# rsync /rsync/ drwxr-xr-x 4096 2014/06/04 00:45:06 . -rw-r--r-- 0 2014/06/04 00:45:01 a -rw-r--r-- 0 2014/06/04 00:45:03 b -rw-r--r-- 0 2014/06/04 00:45:05 c -rw-r--r-- 0 2014/06/04 00:45:06 d [root@rsync-server /]# ls -dl /rsync drwxr-xr-x. 2 root root 4096 Jun 4 00:45 /rsync [root@rsync-server /]# ls -l /rsync total 0 -rw-r--r--. 1 root root 0 Jun 4 00:45 a -rw-r--r--. 1 root root 0 Jun 4 00:45 b -rw-r--r--. 1 root root 0 Jun 4 00:45 c -rw-r--r--. 1 root root 0 Jun 4 00:45 d
[root@rsync-server /]#
rsync [OPTION]... SRC [SRC]... DEST
rsync [OPTION]... SRC [SRC]... DEST 1)rsync為同步的命令 2)[OPTION]... 為同步時的參數選項 3)SRC為源,即需要拷貝的分區,文件或目錄等 4)[DEST]為目地分區,文件或目錄等
-n : 如果擔心命令執行不正確,一同步復制,可能這個復制的后果是致命的,那后果可就嚴重了,這里我們可以加-n先測試一下 -v : --verbose,詳細輸出模式,顯示詳細過程 -q : --quiet,靜默模式,盡可能輸出少的信息 -c : --checksum,可以對傳輸的文件進行較驗的,強制對文件傳輸進行校驗 -r : --recursive,遞歸復制 -p : --perms,保存文件的權限 -t : --times,保留文件的時間戳 -l : --links,保留文件的符號鏈接 -g : --group,保留文件的屬組 -o : --owner,保留文件的屬主 -D : --devices,保留設備文件 -A : --acls,保留acl訪問控制列表 -a : --archives歸檔,復制時可以保存原有的屬主屬主等屬性信息,甚至包括一點額外的屬性。等同于-rlptgoD -e SSH : 遠程復制時,表示使用ssh協議作承載。如果ssh端口不是22,可以用-e ‘ssh -p 22’ -z : 基于網絡時使用,對文件壓縮后傳輸 --progress : 顯示壓縮進度條的,簡寫-P --stats : 顯示如何執行壓縮和傳輸的,也就是顯示傳輸狀態的 --
[root@rsync-server /]# rsync -a /rsync /backup #會把目錄rsync直接同步至/backup目錄中去 [root@rsync-server /]# rsync -a /rsync/ /backup #會把目錄中的data/中的內容同步至/backups目錄中
[root@rsync-server ~]# mkdir /rsync #創建rsync測試目錄 [root@rsync-server ~]# touch /rsync/test #在rsync測試目錄下建立test.txt文件 [root@rsync-server ~]# chmod -R 700 /rsync #授權一下以查看同步后是不是保持了權限屬性 [root@rsync-server rsync]# ll -a /rsync/ total 8 drwx------. 2 root root 4096 Jun 4 01:01 . dr-xr-xr-x. 26 root root 4096 Jun 4 01:01 .. -rwx------. 1 root root 0 Jun 4 01:01 test [root@rsync-server ~]# rsync -avz /rsync/ /tmp sending incremental file list ./ test sent 78 bytes received 34 bytes 224.00 bytes/sec total size is 0 speedup is 0.00 [root@rsync-server ~]# ls -l /tmp/test -rwx------. 1 root root 0 Jun 4 01:01 /tmp/test
拉取pull :rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST 推送push:rsync [OPTION]... [USER@]HOST:SRC [DEST]
1)Rsync為同步的命令 2)[OPTION]... 為同步時的參數選項 3)[USER@]HOST... 為Rsync同步的遠程的連接用戶和主機地址 4)SRC為源,即需要拷貝的分區、文件或目錄等,和HOST之間用一個冒號連接 5)[DEST]為目地分區、文件或目錄等
[root@rsync-server ~]# rsync -vzrtopg –progress -e ‘ssh -p 22’ root@172.16.100.1:/etc /backup
語法說明:
1)-vzrtopg 相當于上文的-avz,表示同步時文件和目錄屬性不變。 2)--progress 顯示同步的過程,可以用-p替換 3)-e ‘ssh -p 22’,表示通過ssh的通道傳輸數據。如果ssh是默認的22號端口,此處的-p 22可省略。 4)root@192.168.1.111:/etc 遠程的主機用戶,地址,路徑 5)/backup本地(172.16.100.2)的路徑
[root@rsync-server ~]# rsync -avz --progress -e ‘shh -p 22’ /webrepo root@172.16.100.1:/data/web/html
1)上面兩個例子中的數據同步都是加密傳輸的,因為是通過ssh通道進行數據傳輸的。2)在傳輸前需要進行連接用戶(一般為系統用戶)密碼驗證,需要手工輸入。這里我們可以通過配置ssh互信來實現免驗證方式同步。3)rsync軟件必須安裝在本地及遠程服務器上
uid = rsync # 指定的模塊傳輸文件時所用的用戶身份 gid = rsync # 指定的site模塊傳輸文件時所用的用戶組身份 port=873 # 默認端口 use chroot = no # 默認為true,修改為no,增加對目錄文件軟連接的備份 max connections = 200 # 指定最大并發連接數以保護服務器,超過限制的連接請求將被告知隨后再試。默認值為 0,即沒有限制。 timeout = 300 # 服務器會話的超時時間 pid file = /var/run/rsyncd.pid # 進程啟動后,進程號存放路徑 lock file = /var/run/rsyncd.lock # 服務啟動、停止會用到鎖文件 log file = /var/log/rsyncd.log # 指定日志文件的路徑 Log fromat = %h %o %f %l %b # 指定日志記錄的格式 motd file = /etc/rsyncd.motd # 登陸消息提示文件,可以不配置。默認為空。 [test] # 模塊名稱,名稱可以任意寫,不過一般是以路徑命名,最好見名意 Comment = test # 模塊信息描述,該描述連同模塊名在客戶連接得到模塊列表時顯示給客戶。默認沒有描述定義 path=/test/ # 指定供備份的目錄路徑,必須指定該參數。該目錄路徑是rsync服務端的一個路徑,提供給客戶端上傳或下載 ignore erros # 忽略錯誤 read only = false # 設定是否允許客戶上載文件。若為 true 任何上載請求均會失敗,若為 false 且客戶端擁有服務器目錄讀寫權限則可以上載。默認值為 true 。 write only 設定是否允許客戶下載文件。若為 true 任何下載請求均會失敗,默認值為 false 。 list = false # 設定當客戶請求可以使用的模塊列表時,是否列出該模塊。若為 false,則創建隱藏的模塊。默認值為 true hosts allow=192.168.137.0/24 # 指定允許客戶連接的 IP 地址??梢詾閱蝹€ IP 地址或整個網段。多個 IP 或網段需要以空格隔開。默認是允許所有主機連接。 hosts deny=* # 指定拒絕連接的客戶IP地址,設置方式和hosts allow相同。 Dont commpress # 指定不進行壓縮處理即可傳輸的文件,默認值是 *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz auth users = rsync_backup # 該選項指定由空格或逗號分隔的用戶名列表,只有這些用戶才允許連接該模塊。這里的用戶不一定是客戶端存在的用戶。如果"auth users"被設置,那么客戶端發出對該模塊的連接請求時,需要對用戶身份進行驗證。用戶的名和密碼以明文方式存放在"secrets file"選項指定的文件中。默認情況下無需密碼就可以連接模塊 ( 也就是匿名方式 )。 secrets file = /etc/rsync.password # 虛擬用戶的密碼文件 exclude = common/ # 用來指定多個由空格隔開的多個文件或目錄 ( 相對路徑 ),并將其添加到 exclude 列表中。這等同于在客戶端命令中使用 --exclude 參數。一個模塊只能指定一個 exclude 選項。但是需要注意的一點是該選項有一定的安全性問題,客戶端很有可能繞過 exclude 列表,如果希望確保特定的文件不能被訪問,那就最好結合 uid/gid 選項一起使用。 pre-xfer exec, post-xfer exec # 設置可在文件傳輸前/后執行的命令。若在文件傳輸前執行的命令失敗,則取消本次傳輸操作。
環境 CentOS6.5 x86_64位 采用最小化安裝,系統經過了基本優化 selinux為關閉狀態,iptables為無限制模式 源碼包存放位置:/root 服務器架構: rsync-server:172.16.100.1 rsync-client:172.16.100.2
[root@rsync-server ~]# yum install rsync -y [root@rsync-server ~]# vim /etc/rsyncd.conf uid = root gid = root use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = 172.16.100.0/24 hosts deny = * auth users = rsync_backup secrets file = /etc/rsync.password [sunsky] path = /test
[root@rsync-server ~]# touch /etc/rsync.passord [root@rsync-server ~]# chmod 600 /etc/rsyncd.password [root@rsync-server ~]# vim rsyncd.password #里面記錄條目的格式為“用戶 : 密碼” rsync_backup:redhat
[root@rsync-server ~]# touch /etc/rsyncd.motd [root@rsync-server ~]# vim rsyncd.conf ============================ This is Critical Data Server =============================
[root@rsync-server ~]# mkdir /test [root@rsync-server ~]# chown root.root /test #更改目錄的權限為rsyncd.conf中uid和gid指定屬主和屬組
[root@rsync-server ~]# /usr/bin/rsync --daemon --config=/etc/rsyncd.conf #注意如果你的rsyncd.conf文件在/etc/目錄下,就不需要再用--config去指定了。--daemon是以守護進程的方式啟動
[root@rsync-server ~]# pkill rsync #關閉rsync服務 [root@rsync-server ~]# rsync --daemon #啟動rsync服務 [root@rsync-server ~]# ps -ef | grep rsync #檢查是否啟動
[root@rsync-server ~]# yum install xinetd #安裝xinetd [root@rsync-server ~]# vim /etc/xinetd.d/rsync disable= no # 默認為yes,需要將他設置為no,讓xinetd接管rsync服務。注意,如果你使用手動的方式,這里就修改為no,即拒絕xinetd接管rsync服務。 [root@rsync-server ~]# /etc/init.d/xinetd start # 啟動xinetd服務,則默認會以守護進程方式啟動rsync 服務
[root@rsync-server ~]# netstat -lntup |grep 873 [root@rsync-server test]# netstat -lntup |grep 873 tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 10234/rsync tcp 0 0 :::873 :::* LISTEN 10234/rsync
[root@rsync-server ~]# lsof -i tcp:873 [root@a test]# lsof -i tcp:873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 10234 root 3u IPv4 93239 0t0 TCP *:rsync (LISTEN) rsync 10234 root 5u IPv6 93240 0t0 TCP *:rsync (LISTEN)
[root@rsync-server ~]# rsync --daemon # daemon 表示以守護進程的方式啟動rsync服務 常用啟動參數: --daemon # 表示以守護進程的方式啟動rsync服務 --address # 綁定指定ip地址提供服務,即服務器是多網卡模式或多虛擬IP模式,需要指定某個固定ip提供服務 --config=FILE # 更改配置文件路徑,而不去使用默認的/etc/rsyncd.conf --port=PORT # 更改其它端口提供服務,而不去使用模式的873端口。
ot@rsync-server ~]# yum install rsync -y
[ro[root@rsync-server ~]# vim /etc/rsync.password # 該文件里面僅存放密碼即可 redhat [root@rsync-server ~]# cat /etc/rsync.password # 配置完畢之后檢查 [root@rsync-server ~]# ll /etc/rsync.password # 配置完畢之后檢查
拉?。孩賠sync [OPTION...] [USER@]HOST::SRC... [DEST] ②rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] 推送:①rsync [OPTION...] SRC... [USER@]HOST:DEST ②rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
rsync -avzP rsync_backup@172.16.100.1::sunsky /test/ --password-file=/etc/rsync.password
rsync -avzP /test rsync_backup@172.16.100.1::sunsky --password-file=/etc/rsync.password
rsync -avzP rsync://rsync_backup@172.16.100.1:/sunsky/ /test/ --password-file=/etc/rsync.password
rsync -avzP /test/ rsync://resync_backup@192.168.1.111:/sunsky/ --password-file=/etc/rsync.password
[root@rsync-server ~]# crontab -e # Automatic synchronization log files at 1 a.m * 1 * * * /usr/bin/rsync-avzP rsync_nobody@172.16.100.1::weblog /data/log/ --password-file=/etc/rsyncd.passwd > /dev/null 2>&1 # Automatic distribution site file points every second of every day * * * * * /usr/bin/rsync-avzP --delete rsync_nobody@172.16.100.1::webdata /data/web/ --password-file=/etc/rsyncd.passwd > /dev/null 2>&1
[root@rsync-server ~]# ls /test/ #查看rsync服務端,sunsky模塊下的/test目錄的文件列表 e [root@rsync-client ~]# ls /test/ #查看rsync客戶端本地/test目錄的文件列表 a b c d
[root@rsync-client ~]# rsync -avzP --delete rsync_backup@172.16.100.1::sunsky /test/ --password-file=/etc/rsync.passwordreceiving incremental file listdeleting ddeleting cdeleting bdeleting a./e 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2) sent 86 bytes received 143 bytes 458.00 bytes/sectotal size is 0 speedup is 0.00[root@rsync-client ~]# ls /test/e [root@rsync-server ~]# ls /test/e
[root@rsync-client ~]# rsync -avzP --delete /test/ rsync_backup@172.16.100.1::sunsky --password-file=/etc/rsync.passwordsending incremental file list./deleting ea 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=3/5)b 0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=2/5)c 0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=1/5)d 0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=0/5) sent 201 bytes received 87 bytes 576.00 bytes/sectotal size is 0 speedup is 0.00[root@rsync-client ~]# ls /test/a b c d[root@rsync-server ~]# ls /test/a b c d
[root@rsync-server ~]# tree /test//test/0 directories, 0 files[root@rsync-client ~]# tree /test//test/├── a├── b├── c.txt├── x│ └── xxx.txt└── x.txt2 directories, 4 files
[root@rsync-client ~]# rsync -avz --exclude=*.txt /test/ root@172.16.100.1:/test/root@172.16.100.1's password: sending incremental file list./abx/sent 154 bytes received 57 bytes 60.29 bytes/sectotal size is 0 speedup is 0.00
[root@rsync-server ~]# tree /test//test/├── a├── b└── x1 directory, 2 files
[root@rsync-client ~]# cat /tmp/exclude x *.txt [root@rsync-client ~]# rsync -avz --exclude-from=/tmp/exclude /test/ root@172.16.100.1:/test/ root@172.16.100.1's password: sending incremental file list ./ a b xx/ sent 151 bytes received 57 bytes 59.43 bytes/sec total size is 0 speedup is 0.00
[root@rsync-server ~]# tree /test/ /test/ ├── a ├── b └── xx 1 directory, 2 files
轉自:http://nolinux.blog.51cto.com/4824967/1431426
原創文章,作者:s19930811,如若轉載,請注明出處:http://www.www58058.com/1995