一、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:873
2.在文件源端A 安裝
安裝
yum install rsync -y
卸載
yum remove rsync -y
免密鑰登錄配置
第一步,在文件源端 生成密鑰
[root@izwz93hm3gyjyjwx5yyxuhz ~]# ssh-keygen -t rsa //指定算法為rsa
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:
SHA256:uuMJxNrH7HYKoKK4iqfX/Bxc3KgyvHx0glaKoygOdjA root@izwz93hm3gyjyjwx5yyxuhz
The key's randomart image is:
+---[RSA 2048]----+
| |
| |
| |
| . .. o |
| E..o+ S . |
| .=*++o+. |
|=o.*B.Oo |
|@ +.oX++. |
|@* o=O+ |
+----[SHA256]-----+
[root@izwz93hm3gyjyjwx5yyxuhz ~]#
此時會在/root/.ssh目錄下生成密鑰對
[root@izwz93hm3gyjyjwx5yyxuhz .ssh]# ls -la
total 20
drwx------ 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 root@120.77.250.120
[root@izwz93hm3gyjyjwx5yyxuhz .ssh]# ssh-copy-id root@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
root@120.77.250.120's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@120.77.250.120'"
and check to make sure that only the key(s) you wanted were added.
第三步,測試同步文件
在/www/wwwroot/file 里面新建文件
rsync -avzP /www/wwwroot/file/ root@120.77.250.120:/www/wwwroot/file/
//使用了免密方式,就不會提示輸入密碼
同步結果:( success)
sending incremental file list
plus-yixueqing/
plus-yixueqing/222.php
sent 90,389 bytes received 756 bytes 60,763.33 bytes/sec
total size is 132,583,590 speedup is 1,454.64
[root@izwz93hm3gyjyjwx5yyxuhz file]# rsync -avzP /www/wwwroot/file/ root@120.77.250.120:/www/wwwroot/file/
sending incremental file list
sent 90,342 bytes received 726 bytes 60,712.00 bytes/sec
total size is 132,583,590 speedup is 1,455.87
三、rsync 守護進程 (推薦)
1.在目標端
是要接收文件的,需要配置兩個文件
安裝
yum install rsync -y
卸載
yum remove rsync -y
第一、配置文件/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
[faceke]
path = /www/wwwroot/file/
ignore errors
read only = no
list = false
auth users = anson
secrets file = /etc/rsync.password
配置說明:
vim /etc/rsyncd.conf
uid = rsync #用戶id
gid = rsync
use chroot = no #安全性,內網一般不考慮,設為no
max 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 --daemon
2.安裝與配置客戶端 (文件源端)
客戶端只需要密碼文件,文件里只存放密碼:
安裝
yum install rsync -y
僅寫入和服務端虛擬用戶對應的密碼
修改/etc/rsync.password
echo "hao123456" > /etc/rsync.password
chmod 600 /etc/rsync.password
寫給目錄,開始同步操作
創建目錄,創建測試文件
cd /www/wwwroot/
mkdir file
vim aaa.php
vim bbb.php ...
開始同步
rsync -arvzp --delete /www/wwwroot/file/ anson@120.77.250.120::faceke --password-file=/etc/rsync.password
或者使用rsync協議
rsync -avz rsync://anson@120.77.250.120/test /www/wwwroot/file/ --password-file=/etc/rsync.password
也可以將要排除的文件和目錄寫入一個文件,一行一個: vim excefile.conf
test.py
*.log
dir1
使用—exclude-from
rsync -avz --exclude-from=excefile.conf /data/ rsync_backup@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.sh
vim rsync.sh
#/bin/sh
rsync -arvzp --delete /www/wwwroot/file/ anson@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/