標籤:統計 輸入密碼 incr 路徑 filter rom god command .bashrc
一、rsync特性可以鏡像儲存整個分類樹和檔案系統。可以很容易做到保持原來檔案的許可權、時間、軟永久連結等等。無須特殊許可權即可安裝。快速:第一次同步時rsync會複製全部內容,但在下一次只傳輸修改過的檔案。rsync在傳輸資料的過程中可以實行壓縮及解壓縮操作,因此可以使用更少的頻寬。安全:可以使用scp、ssh等方式來傳輸檔案,當然也可以通過直接的socket串連。支援匿名傳輸,以方便進行網站鏡象。rsync常用選項 -a 包含-rtplgoD (平時用-a就夠啦) -r 同步目錄時要加上,類似cp時的-r選項 -v 可視化,告訴你拷貝了什麼檔案,統計用了多久,拷貝了多少位元組,速度等,同步時顯示一些資訊,讓我們知道同步的過程 -l 保留軟串連,拷貝的語言目錄有軟串連檔案,用到另外一個目錄下,加了-l 會把軟串連本身拷貝到目錄裡 -L 加上該選項後,同步軟連結時會把源檔案給同步 -p 保持檔案的許可權屬性 -o 保持檔案的屬主,這邊是www,拷貝另外一邊也是www,如果你不沒有,就顯示數字 -g 保持檔案的屬組,root屬組,到另外一個地方也是一樣 -D 保持裝置檔案資訊 -t 保持檔案的時間屬性 --delete 刪除DEST(目標目錄)中SRC(來源目錄)沒有的檔案 --exclude 過濾指定檔案,如--exclude (可以排查一些目錄或者檔案,如記錄檔,節省空間的) “logs”會把檔案名稱包含logs的檔案或者目錄過濾掉,不同步 -P 顯示同步過程,比如速率,比-v更加詳細 -u 是update的簡寫,加上該選項後,如果DEST(目標)中的檔案比SRC(源檔案)新,則不同步 -z 傳輸時壓縮(傳輸前自動壓縮,傳輸後,自動解壓)二、方式一:使用SSH通道同步說明: 1、檔案源:A (服務端) 2、目標端:B (用戶端)、C、D..... (接收檔案)1.在目標端B 安裝rsync安裝yum install rsync -y卸載yum remove rsync -y啟動rsync --daemon殺死進程pkill rsync查看連接埠873檢查是否開啟了對873連接埠的監聽lsof -i:8732.在檔案源端A 安裝安裝yum install rsync -y卸載yum remove rsync -y免密鑰登入配置第一步,在檔案源端 產生密鑰[[email protected] ~]# ssh-keygen -t rsa //指定演算法為rsaGenerating 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:SHA256:uuMJxNrH7HYKoKK4iqfX/Bxc3KgyvHx0glaKoygOdjA [email protected]The key‘s randomart image is:+---[RSA 2048]----+| || || || . .. o || E..o+ S . || .=*++o+. ||=o.*B.Oo ||@ +.oX++. ||@* o=O+ |+----[SHA256]-----+[[email protected] ~]# 此時會在/root/.ssh目錄下產生金鑰組[[email protected] .ssh]# ls -latotal 20drwx------ 2 root root 4096 Jul 4 08:40 .dr-xr-x---. 7 root root 4096 Jul 4 08:44 ..-rw------- 1 root root 0 Jul 3 08:24 authorized_keys-rw------- 1 root root 1675 Jul 4 08:40 id_rsa-rw-r--r-- 1 root root 410 Jul 4 08:40 id_rsa.pub-rw-r--r-- 1 root root 176 Jul 3 09:50 known_hosts第二步,在目標端把密鑰上傳到檔案原始伺服器ssh-copy-id [email protected]120.77.250.120[[email protected] .ssh]# ssh-copy-id [email protected]120.77.250.120/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"The authenticity of host ‘120.77.250.120 (120.77.250.120)‘ can‘t be established.ECDSA key fingerprint is SHA256:okdQRMFZve4+bVy0aCrDiKpNXe3E20bac2G7gG0VkdQ.ECDSA key fingerprint is MD5:88:a4:7a:72:db:10:a5:db:8d:47:bc:23:cb:4f:a9:eb.Are you sure you want to continue connecting (yes/no)? yes/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys[email protected]120.77.250.120‘s password: Number of key(s) added: 1Now try logging into the machine, with: "ssh ‘[email protected]‘"and check to make sure that only the key(s) you wanted were added.第三步,測試同步檔案在/www/wwwroot/file 裡面建立檔案rsync -avzP /www/wwwroot/file/ [email protected]120.77.250.120:/www/wwwroot/file/ //使用了免密方式,就不會提示輸入密碼同步結果:( success)sending incremental file listplus-yixueqing/plus-yixueqing/222.phpsent 90,389 bytes received 756 bytes 60,763.33 bytes/sectotal size is 132,583,590 speedup is 1,454.64[[email protected] file]# rsync -avzP /www/wwwroot/file/ [email protected]120.77.250.120:/www/wwwroot/file/ sending incremental file listsent 90,342 bytes received 726 bytes 60,712.00 bytes/sectotal size is 132,583,590 speedup is 1,455.87三、rsync 守護進程 (推薦)1.在目標端是要接收檔案的,需要配置兩個檔案安裝yum install rsync -y卸載yum remove rsync -y第一、設定檔/etc/rsyncd.conf寫模組,分配虛擬使用者名稱和密碼等資訊uid = rootgid = rootuse chroot = nomax connections = 200timeout = 300pid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locklog file = /var/log/rsyncd.log[faceke] path = /www/wwwroot/file/ ignore errors read only = no list = false auth users = anson secrets file = /etc/rsync.password配置說明:vim /etc/rsyncd.confuid = rsync #使用者idgid = rsyncuse chroot = no #安全性,內網一般不考慮,設為nomax connections = 200 #最多有多少個用戶端串連我timeout = 300 #逾時時間,秒pid file = /var/run/rsyncd.pid #pid檔案lock file = /var/run/rsync.lock #傳輸時會給檔案加鎖log file = /var/log/rsyncd.log #記錄檔[test] #模組path = /test/ #用戶端來同步,就是同步該目錄ignore errors #傳輸過程中遇到錯誤,自動忽略read only = false #可讀可寫list = false #不允許清單hosts allow = 10.0.0.0/24 #允許的IP段hosts deny = 0.0.0.0/32 #拒絕auth users = rsync_backup #這是個虛擬使用者(自訂)secrets file = /etc/rsync.password #虛擬使用者對應的密碼檔案第二、配置密碼檔案 /etc/rsync.password(寫入虛擬使用者名稱和密碼)#僅寫入和服務端虛擬使用者對應的密碼echo "anson:hao123456" > /etc/rsync.password#修改密碼檔案的許可權(必須)chmod 600 /etc/rsync.password查看端扣873檢查是否開啟了對873連接埠的監聽lsof -i:873殺死進程pkill rsync啟動服務rsync --daemon2.安裝與配置用戶端 (檔案源端)用戶端只要求輸入密碼檔案,檔案裡只存放密碼:安裝yum install rsync -y僅寫入和服務端虛擬使用者對應的密碼修改/etc/rsync.passwordecho "hao123456" > /etc/rsync.passwordchmod 600 /etc/rsync.password寫給目錄,開始同步操作建立目錄,建立測試檔案cd /www/wwwroot/mkdir filevim aaa.phpvim bbb.php ...開始同步rsync -arvzp --delete /www/wwwroot/file/ [email protected]120.77.250.120::faceke --password-file=/etc/rsync.password或者使用rsync協議rsync -avz rsync://[email protected]/test /www/wwwroot/file/ --password-file=/etc/rsync.password也可以將要排除的檔案和目錄寫入一個檔案,一行一個: vim excefile.conftest.py*.logdir1使用—exclude-fromrsync -avz --exclude-from=excefile.conf /data/ [email protected]10.0.07::/test --password-file=/etc/rsync.password一些問題排查https://yq.aliyun.com/articles/111512?t=t1//軟串連/rsyncd-munged/ 報錯解決方案http://blog.sina.com.cn/s/blog_4da051a60102wo5v.html寫定時任務crontab -e#此時會進入vi的編輯介面讓你編輯工作。注意到,每項工作都是一行。0 12 * * * mail dmtsai -s "at 12:00" < /home/dmtsai/.bashrc#分 時 日 月 周 |《==============命令列=======================》|
代表意義 分鐘 小時 日期 月份 周 命令數字範圍 0~59 0~23 1~31 1~12 0~7 就命令啊周的數字為0或7時,都代表“星期天”的意思。另外,還有一些輔助的字元,大概有下面這些:*(星號)代表任何時刻都接受的意思。舉例來說,範例一內那個日、月、周都是*,就代表著不論何月、何日的禮拜幾的12:00都執行後續命令的意思。,(逗號)代表分隔時段的意思。舉例來說,如果要執行的工作是3:00與6:00時,就會是:0 3,6 * * * command時間還是有五列,不過第二列是 3,6 ,代表3與6都適用-(減號)代表一段時間範圍內,舉例來說,8點到12點之間的每小時的20分都進行一項工作:20 8-12 * * * command仔細看到第二列變成8-12.代表 8,9,10,11,12 都適用的意思/n(斜線)那個n代表數字,即是每隔n單位間隔的意思,例如每五分鐘進行一次,則:*/5 * * * * command用*與/5來搭配,也可以寫成0-59/5,意思相同寫同步指令碼可手動執行,也可以定時執行在目錄www/wwwroot/file/建立指令碼rsync.shvim rsync.sh#/bin/shrsync -arvzp --delete /www/wwwroot/file/ [email protected]120.77.250.120::faceke --password-file=/etc/rsync.password//如果有多台伺服器,就在這裡設定多台done配置定時任務在這裡是定時1分鐘執行一次指令碼crontab -e*/1 * * * * sh /www/wwwroot/file/rsync.sh同步檔案大小du -sh ./*ls -alh壓縮命令tar -zcvf plus-yixueqing-h5.zip plus-yixueqing-h5/
多台伺服器檔案同步實現