rsync備份服務
rsync服務命令簡單應用(1v4的應用效果)
①. rsync == cp
[root@backup ~]# # rsync == cp效果 [root@backup ~]# cp -a /etc/hosts /tmp/ [root@backup ~]# ll /tmp/ total 4 -rw-r--r--. 1 root root 352 Jan 27 01:15 hosts [root@backup ~]# rsync -a /etc/sysconfig/network /tmp/ [root@backup ~]# ll /tmp/ total 8 rw-r--r--. 1 root root 352 Jan 27 01:15 hosts -rw-r--r-- 1 root root 31 Jan 26 18:16 network
②. rsync == scp
[root@backup ~]# scp -rp /tmp/ 172.16.1.31:/tmp/ The authenticity of host ‘172.16.1.31 (172.16.1.31)‘ can‘t be established. RSA key fingerprint is 5b:9b:e6:79:a9:95:4f:be:06:41:e3:bb:7a:12:ee:b4. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘172.16.1.31‘ (RSA) to the list of known hosts. [email protected]‘s password: network 100% 31 0.0KB/s 00:00 hosts 100% 352 0.3KB/s 00:00 [root@backup ~]# ll /tmp/ total 8 -rw-r--r--. 1 root root 352 Jan 27 01:15 hosts -rw-r--r-- 1 root root 31 Jan 26 18:16 network [root@backup ~]# rsync -rp /tmp/ 172.16.1.31:/tmp/ [email protected]‘s password:
說明:
同步數據時,/tmp/目錄後有/信息,表示將目錄下面的數據內容進行備份同步
同步數據時,/tmp目錄後沒有/信息,表示將目錄及目錄下面的數據內容進行備份同步
③ rsync == rm
說明:rsync實現刪除目錄中數據內容過程,就將一個空目錄和一個有數據的目錄進行同步
最終,會將有數據的目錄中的文件進行清空
[root@backup ~]# mkdir /null [root@backup ~]# rsync --delete /null/ /tmp/ rsync: --delete does not work without -r or -d. rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6] [root@backup ~]# [root@backup ~]# rsync -r --delete /null/ /tmp/ [root@backup ~]# ll /tmp/ total 0
④ rsync == ls
[root@backup ~]# ls /etc/hosts
/etc/hosts
[root@backup ~]# ls -l /etc/hosts
-rw-r--r--. 2 root root 352 Jan 27 01:15 /etc/hosts
[root@backup ~]# rsync /etc/hosts
-rw-r--r-- 352 2018/01/27 01:15:59 hosts
rsync軟件工作方式
①. 本地數據備份方式
Local: rsync [OPTION...] SRC... [DEST]
rsync --- 數據備份傳輸命令
option --- 可以輸入一下和rsync傳輸數據有關的參數
src --- 要進行備份的數據(文件/目錄)
dest --- 將數據信息備份到什麽位置(相應路徑中)
[root@backup ~]# rsync -a /etc/hosts /tmp/ok.txt
[root@backup ~]# ll /tmp/ok.txt
-rw-r--r-- 1 root root 352 Jan 27 01:15 /tmp/ok.txt
②. 遠程數據備份方式
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
pull方式語法說明:
rsync --- 數據備份傳輸命令
option --- 可以輸入一下和rsync傳輸數據有關的參數
[USER@]HOST: --- 需要指定以什麽用戶身份登錄到遠程主機,
如果省略USER信息,表示以當前用戶身份進行登錄
登錄主機地址或域名信息
SRC --- 指定遠程主機要傳輸過來到本地的數據信息
dest --- 將數據保存到本地的什麽路徑中
push方式語法說明:
rsync --- 數據備份傳輸命令
option --- 可以輸入一下和rsync傳輸數據有關的參數
[USER@]HOST: --- 需要指定以什麽用戶身份登錄到遠程主機,
如果省略USER信息,表示以當前用戶身份進行登錄
登錄主機地址或域名信息
SRC --- 指定本地主機要傳輸到遠程主機的數據
dest --- 將本地數據保存到遠端的什麽路徑中
③. 守護進程傳輸模式
Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
pull:rsync [OPTION...] [USER@]HOST::SRC... [DEST]
[USER@]HOST:: --- 指定遠程連接的認證用戶
SRC --- 指定相應的模塊信息
[DEST] --- 將遠程數據保存到本地的路徑信息
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
[USER@]HOST:: --- 指定遠程連接的認證用戶
SRC --- 指定本地要進行推送的數據信息
[DEST] --- 遠程進行保存數據的模塊信息
rsync守護進程部署流程
1)服務端部署流程
第一裏程:檢查軟件是否安裝
[root@backup ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
第二裏程:編寫配置文件
vim /etc/rsyncd.conf
#rsync_config
#created by HQ at 2017
##rsyncd.conf start##
uid = rsync
gid = rsync
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.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = "backup dir by oldboy"
path = /backup
read only = true
[nfs]
comment = "backup dir by oldboy"
path = /nfs
第三個裏程:創建備份目錄管理用戶useradd rsync -M -s /sbin/nologin
第四個裏程:創建備份目錄
mkdir /backup
chown -R rsync.rsync /backup
第五個裏程:創建認證文件
echo "rsync_backup:oldboy123" >>/etc/rsync.password
chmod 600 /etc/rsync.password
第六個裏程:啟動rsync服務rsync --daemon
2)客戶端部署流程
第一個裏程:確認軟件是否安裝
[root@backup ~]# rpm -qa rsync
rsync-3.0.6-12.el6.x86_64
第二個裏程:創建認證密碼文件
echo "oldboy123" >>/etc/rsync.password
chmod 600 /etc/rsync.password
第三個裏程:進行數據備份測試
[root@nfs01 tmp]# rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password
sending incremental file list
hosts
sent 189 bytes received 27 bytes 432.00 bytes/sec
total size is 352 speedup is 1.63
rsync備份服務