rsync遠程同步的基本使用
實驗一:通過rsync實現本機內文件或者文件夾的同步
實驗步驟:
[root@yeyue ~]# mkdir ceshi
[root@yeyue ~]# rsync -av 桌面/個人學習資料與報告/ ceshi/ --a代表歸檔模式,v顯示同步信息
注:rsync 參數 原目錄 目標目錄 (原目錄下帶/則代表同步目錄下的內容,不帶/則代表同步目錄本身)
[root@yeyue ~]# mkdir /1
[root@yeyue ~]# touch /1/123.txt
[root@yeyue ~]# rsync -a /boot/ /1/ --delete --添加--delete命令後將會刪除目標文件夾內有而源文件沒有的內容
實驗二:基於網絡的rsync
1.rsync+ssh安全同步
下行(下載):rsync user@ip:原目錄 本地目錄
上行(上傳):rsync 本地目錄 user@ip:目標目錄
[root@yeyue ~]# mkdir ceshi
[root@yeyue ~]# rsync -av [email protected]:/root/ ceshi/
[root@yeyue ~]# ls ceshi/
anaconda-ks.cfg install.log install.log.syslog
2.rsync+rsync同步
下行:rsync user@ip::共享名 本地目錄
上行:rsync 本地目錄 user@ip::共享名
實驗環境:兩臺擁有rsync服務的服務器,其中一臺配置rsync共享,一臺測試
實驗步驟:
1.創建可訪問用戶的用戶名,密碼存放文件
[root@dns01 ~]# cat /etc/user.db
abc:123
[root@dns01 ~]# chmod 600 /etc/user.db --修改文件權限,確保其他用戶沒有權限,提高安全性
[root@dns01 ~]# ll /etc/user.db
-rw------- 1 root root 8 06-25 20:03 /etc/user.db
2.創建並修改主配置文件
[root@dns01 etc]# touch rsyncd.conf --在etc下創建主配置文件
[root@dns01 etc]# cat rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
[gongxiang]--共享名
path = /usr/src --共享路徑
read only = yes --權限只讀
dont compress = *.zip *.bz2 *.tgz *.gz --不需要壓縮的格式
auth users = abc --授權登錄賬戶
secrets file = /etc/user.db--授權登錄賬戶信息位置
[root@dns01 etc]# chkconfig rsync on
[root@dns01 etc]# service xinetd restart
[root@dns01 etc]# netstat -ln | grep :873--查看服務端口是否監聽
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN
[root@dns01 etc]# ls /usr/src/ --查看共享內有的文件
debug kernels
3.在客戶端進行測試
[root@yeyue ~]# mkdir 123 --創建測試用文件夾
[root@yeyue ~]# rsync -avz [email protected]::gongxiang /root/123/
--同步192.168.13.221服務器內的共享 也可加參數--delete刪除本地目錄內源沒有讀文件
Password: --輸入在服務器授權的登錄用戶abc相對應的密碼
receiving incremental file list
./
debug/
kernels/
sent 69 bytes received 141 bytes 60.00 bytes/sec
total size is 0 speedup is 0.00
[root@yeyue ~]# ls 123/
debug kernels
容易出錯的地方
# iptables -L --防火牆未關
# getenforce --selinux未關
# chmod 600 /etc/rsyncd_users.db --權限沒有修改
# service xinetd restart --xinetd臨時服務監控未啓動
# netstat -ln | grep :873 --端口未監聽
# /etc/xinetd.d/rsync --xinetd服務不可用
disable = yes
注:此實驗可結合計劃任務來達成定時備份的目的
實驗三:通過安裝源碼包inotify,執行腳本,實現觸發式同步
實驗目的:實現源文件發生改變時,自動備份至其他備份服務器
實驗環境:一臺備份源服務器,一臺測試用待同步服務器
實驗步驟:
1.配置遠程目標主機
開啓nfs共享,創建備份目錄
[root@dns01 etc]# mkdir -p /beifeng
[root@dns01 etc]# cat /etc/exports
/beifeng *(rw,no_root_squash)--保留root用戶權限
[root@dns01 etc]# service portmap restart
[root@dns01 etc]# chkconfig portmap on
[root@dns01 etc]# service nfs restart
[root@dns01 etc]# chkconfig nfs on
2.配置備份源服務器
安裝源碼包inotify (需要gcc,gcc-c++,make)
[root@yeyue ~]# yum -y install gcc*
[root@yeyue ~]# tar -zxf 桌面/inotify-tools-3.14.tar.gz -C /usr/src/ --將源碼包解壓到指定文件夾
[root@yeyue ~]# cd /usr/src/inotify-tools-3.14/ --進入解壓後的文件夾
[root@yeyue inotify-tools-3.14]# ./configure--執行
[root@yeyue inotify-tools-3.14]# make --編譯
[root@yeyue inotify-tools-3.14]# make install --安裝
通過配置將遠程待備份服務器的nfs掛在到本機位置
[root@yeyue ~]# mkdir /data/beifeng--創建源備份服務器掛載點
[root@yeyue ~]# cat /etc/fstab |tail -1
192.168.13.221:/beifeng/data/beifengnfsdefaults0 0
[root@yeyue ~]# mount -a --自動掛在配置文件內有的掛在目錄
[root@yeyue ~]# mount |tail -1
192.168.13.221:/beifeng on /data/beifeng type nfs (rw,addr=192.168.13.221)
--將遠程備份服務器上的/beifeng掛在到本地的/data/beifeng上
[root@yeyue ~]# cat /usr/bin/in_rsync.sh --編輯in_rsync.sh文件,讓其執行內部腳本
#!/bin/bash
/usr/local/bin/inotifywait -mrq -e modify,create,move,delete /var/www/html | while read DIR EVENT FILE --當文件夾/var/www/html發生修改,創建,移動,刪除變化時執行下面操作
do --要做de
/usr/bin/rsync -aHvz --delete /var/www/html/ /data/beifeng --/var/www/html發生上述變化時進行本地同步,並且保留原有文件不變,刪除目標文件夾裏源內沒有的文件
done--結束
[root@yeyue ~]# chmod 700 /usr/bin/in_rsync.sh
[root@yeyue ~]# ll /usr/bin/in_rsync.sh
-rwx------ 1 root root 182 6月 25 20:56 /usr/bin/in_rsync.sh
[root@yeyue ~]# echo "/usr/bin/in_rsync.sh" >> /etc/rc.local
--將腳本配置追加到rc.local內,開機啓動
測試:在源服務器內的/var/www/html/創建ceshi.html
[root@yeyue html]# sending incremental file list
./
ceshi.html
sent 84 bytes received 34 bytes 236.00 bytes/sec
total size is 0 speedup is 0.00
[root@yeyue html]# ls /data/beifeng/ --查看rsync本地同步結果
ceshi.html
進入遠程備份服務器查看結果
[root@dns01 /]# ls /beifeng/
ceshi.html-- 成功
實驗總結:通過上述實驗,使用插件inotify-tools執行了腳本,在源目錄內的文件發生變化時自動同步到遠程服務器在本地的掛載點,實現了遠程觸發備份
NTP網絡時間配置與測試
1.安裝NTP服務
[root@yeyue ~]# yum -y install ntp
2.修改主配置文件
10 restrict 192.168.13.0 mask 255.255.255.0 nomodify --指定192.168.13.0網段用戶可同步服務器時間
22 server 0.centos.pool.ntp.org iburst
23 server 1.centos.pool.ntp.org iburst
24 server 2.centos.pool.ntp.org iburst
25 server 127.127.1.0--服務器同步時間鎖定本機硬件bios,直接應答客戶機請求。
測試:
服務器時間
[root@yeyue ~]# date
2014年 06月 25日 星期三 21:33:38 CST
本地時間
[root@dns01 /]# date
2014年 03月 23日 星期日 16:45:05 CST
[root@dns01 /]# ntpdate 192.168.13.1
25 Jun 21:39:26 ntpdate[6934]: step time server 192.168.13.1 offset 8138890.328443 sec
[root@dns01 /]# date
2014年 06月 25日 星期三 21:39:31 CST
[root@dns01 /]# hwclock --systohc --將本地時間鎖定到硬件BIOS