天天看點

Zhong__Linux下使用rsync實作兩台主機遠端備份檔案服務端:用戶端: 

主題:實作服務端檔案定時增量備份到用戶端

環境:服務端(CentOStart7.6) 用戶端(CentOStart7.6)  兩台皆為雲伺服器

目的:實作服務端檔案每天自動備份到用戶端,并删除超過30天的檔案!

時間:2019.08.12

工具:rsync

作者:Zhong

  對于MySQL遠端備份前面的文章有寫過,有需要的請前去檢視!

目前CentOS7.6系統自帶rsync工具,無需再安裝!需要配置的檔案建議做好備份再修改!

服務端:

配置rsyncd.conf:

vim rsyncd.conf
           

初始檔案:

# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
           

修改為如下:

# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = root
gid = root
use chroot = no
#最大連接配接
max connections = 4
# pid file = /var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
#日志檔案路徑
log file = /var/log/rsyncd.log     
transfer logging = yes
# exclude = lost+found/
# transfer logging = yes
#端口  預設為873
#port=873
#連接配接逾時時間
timeout = 900
#忽略不可讀檔案
ignore nonreadable = yes
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

#[]中為名字,可随意
[rsync_file]
path = /home/test
#comment 為描述說明
comment = test file
list=yes
ignore errors
list=yes
ignore errors
#認證使用者  和系統使用者無關 作為rsync工具認證使用
auth users = test
#密碼檔案
secrets file = /etc/rsync/screts.pas
           

 以上配置主要指定服務端要備份的檔案路徑,用戶端備份時隻需指定名為rsync_file的配置即可找到path對應的檔案!并配置了使用者及密碼檔案!

配置密碼檔案screts.pas(對應rsyncd.conf中指定的路徑、檔案名):

vim /etc/rsync/screts.pas  #如果沒有rsync目錄則建立
           

添加使用者名:密碼并儲存: 

Zhong__Linux下使用rsync實作兩台主機遠端備份檔案服務端:用戶端: 

此檔案權限必須設定其它組使用者不可通路(服務端和用戶端都要設定)

chmod 600 /etc/rsync/screts.pas
           

如果不做上面的配置會出現這種提示:

ERROR: password file must not be other-accessible

rsync error: syntax or usage error (code 1) at authenticate.c(196) [Receiver=3.1.2]

啟動服務:

rsync --daemon --config=/etc/rsyncd.conf
           

用戶端: 

建立密碼檔案(密碼和服務端定義的對應):

echo  "admin123" > /root/Desktop/test/passwd 
           

 修改權限:

chmod 600 passwd
           
  •  即時備份(192.192.192.192為舉例IP,實際IP對應服務端的IP):
rsync -avz --password-file=/root/Desktop/test/passwd  [email protected]::rsync_file
           

參數可根據自己目的調整,自行搜尋!這兒-a代表歸檔,保持源檔案屬性![email protected] 192.192.192.192::rsync_file  test為服務端定義的使用者,rsync_file是服務端定義的要備份的檔案的名字!

此時執行指令後,檔案就開始備份了!

  • 定時備份(本實驗的目的)

建立rsync.sh檔案: 

vim rsync.sh
           

添加上面的指令:

rsync -avz --password-file=/root/Desktop/test/passwd  [email protected]::rsync_file
           

 給rsync.sh檔案增加可執行的權限:

chmod +x rsync.sh
           

配置crontab定時任務:

crontab -e
           

添加如下配置(路徑對應rsybc.sh檔案路徑):

40  10 * * * /root/Desktop/test_backup/rsync.sh
           

 上面的配置為每天10:40執行定時任務,執行rsync.sh檔案。

備注:

如果為雲伺服器的話,請在背景配置相關政策,開放要用到的端口如873或者12000

如果服務端指定port,那麼用戶端也要指定:

例如:

服務端:

port=12000

用戶端:

rsync -avz --password-file=/root/Desktop/test/passwd --port=12000 [email protected]::rsync_file /root/Desktop/test/ 

可以增加定時删除超過規定時間的檔案:

crontab -e
           

添加一行指令:

30 22 * * * find /root/Desktop/backup_test/ -mtime +30 -name "*tar.gz" -exec rm -rf {} \;
           

上面的指令為每天22:30查找并删除/root/Desktop/backup_test/目錄中建立超過30天的以tar.gz結尾的檔案 !

 關注微信公衆号:

Zhong__Linux下使用rsync實作兩台主機遠端備份檔案服務端:用戶端: 

qq交流群:

 121160124

繼續閱讀