服務管理之rsync
目錄
- rsync
- 1. rsync簡介
- 2. rsync特性
- 4. rsync命令
- 5. rsync+inotify
rsync
1. rsync簡介
rsync是linux系統下的數據鏡像備份工具。使用快速增量備份工具Remote Sync可以遠程同步,支持本地復制,或者與其他SSH、rsync主機同步。
2. rsync特性
rsync支持很多特性:
- 可以鏡像保存整個目錄樹和文件系統
- 可以很容易做到保持原來文件的權限、時間、軟硬鏈接等等
- 無須特殊權限即可安裝
- 快速:第一次同步時rsync會復制全部內容,但在下一次只傳輸修改過的文件。rsync在傳輸數據的過程中可以實行壓縮及解壓縮操作,因此可以使用更少的帶寬
- 安全:可以使用scp、ssh等方式來傳輸文件,當然也可以通過直接的socket連接
支持匿名傳輸,以方便進行網站鏡象
3.rsync的ssh認證協議
rsync命令來同步系統文件之前要先登錄remote主機認證,認證過程中用到的協議有2種:- ssh協議
rsync協議
rsync server端不用啟動rsync的daemon進程,只要獲取remote host的用戶名和密碼就可以直接rsync同步文件
rsync server端因為不用啟動daemon進程,所以也不用配置文件/etc/rsyncd.conf
ssh認證協議跟scp的原理是一樣的,如果在同步過程中不想輸入密碼就用ssh-keygen -t rsa打通通道
//這種方式默認是省略了 -e ssh 的,與下面等價:
rsync -avz /SRC -e ssh [email protected]:/DEST
-a //文件宿主變化,時間戳不變
-z //壓縮數據傳輸
//當遇到要修改端口的時候,我們可以:
rsync -avz /SRC -e "ssh -p2222" [email protected]:/DEST
//修改了ssh 協議的端口,默認是22
4. rsync命令
//Rsync的命令格式常用的有以下三種: rsync [OPTION]... SRC DEST rsync [OPTION]... SRC [[email protected]]HOST:DEST rsync [OPTION]... [[email protected]]HOST:SRC DEST //對應於以上三種命令格式,rsync有三種不同的工作模式: 1)拷貝本地文件。當SRC和DES路徑信息都不包含有單個冒號":"分隔符時就啟動這種工作模式。如: [[email protected] ~]# ls anaconda-ks.cfg azhttpd.sh test tphttpd.sh [[email protected] ~]# rsync -a anaconda-ks.cfg ba [[email protected] ~]# ls anaconda-ks.cfg azhttpd.sh ba test tphttpd.sh [[email protected] ~]# ll 總用量 16 -rw-------. 1 root root 1585 3月 20 03:06 anaconda-ks.cfg -rw-r--r--. 1 root root 1773 4月 25 04:10 azhttpd.sh -rw-------. 1 root root 1585 3月 20 03:06 ba drwxr-xr-x. 2 root root 29 4月 25 21:35 test -rw-r--r--. 1 root root 1248 4月 25 16:39 tphttpd.sh 2)使用一個遠程shell程序(如rsh、ssh)來實現將本地機器的內容拷貝到遠程機器。當DST路徑地址包 含單個冒號":"分隔符時啟動該模式。如: [[email protected] ~]# rsync -avz ba [email protected]:/root/aa [email protected]'s password: sending incremental file list ba sent 867 bytes received 35 bytes 200.44 bytes/sec total size is 1,585 speedup is 1.76 客戶端驗證: [[email protected] ~]# ls aa CentOS7-Base-163.repo anaconda-ks.cfg httpd-2.4.39.tar.bz2 apr-1.6.5.tar.bz2 mysql57-community-release-el7-10.noarch.rpm apr-util-1.6.1.tar.bz2 test [[email protected] ~]# 3)使用一個遠程shell程序(如rsh、ssh)來實現將遠程機器的內容拷貝到本地機器。當SRC地址路徑 包含單個冒號":"分隔符時啟動該模式。如: [[email protected] ~]# rsync -aqz ba 'ssh' [email protected]:/root [email protected]'s password: rsync: link_stat "/root/ssh" failed: No such file or directory (2) rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2] 客戶端驗證: [[email protected] ~]# ls aa anaconda-ks.cfg ba test //rsync常用選項: -a, --archive //歸檔 -v, --verbose //啰嗦模式 -q, --quiet //靜默模式 -r, --recursive //遞歸 -p, --perms //保持原有的權限屬性 -z, --compress //在傳輸時壓縮,節省帶寬,加快傳輸速度 --delete //在源服務器上做的刪除操作也會在目標服務器上同步 delete的用法 [[email protected] ~]# rsync -aqz --delete test 'ssh' [email protected]:/root [email protected]'s password: rsync: link_stat "/root/ssh" failed: No such file or directory (2) rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2] 客戶端驗證: [[email protected] ~]# ls test/ aa anaconda-ks.cfg
5. rsync+inotify
rsync與傳統的cp、tar備份方式相比,rsync具有安全性高、備份迅速、支持增量備份等優點,通過rsync可以解決對實時性要求不高的數據備份需求,例如定期的備份文件服務器數據到遠端服務器,對本地磁盤定期做數據鏡像等。
隨著應用系統規模的不斷擴大,對數據的安全性和可靠性也提出的更好的要求,rsync在高端業務系統中也逐漸暴露出了很多不足,首先,rsync同步數據時,需要掃描所有文件後進行比對,進行差量傳輸。如果文件數量達到了百萬甚至千萬量級,掃描所有文件將是非常耗時的。而且正在發生變化的往往是其中很少的一部分,這是非常低效的方式。其次,rsync不能實時的去監測、同步數據,雖然它可以通過linux守護進程的方式進行觸發同步,但是兩次觸發動作一定會有時間差,這樣就導致了服務端和客戶端數據可能出現不一致,無法在應用故障時完全的恢復數據。基於以上原因,rsync+inotify組合出現了!
Inotify是一種強大的、細粒度的、異步的文件系統事件監控機制,linux內核從2.6.13起,加入了Inotify支持,通過Inotify可以監控文件系統中添加、刪除,修改、移動等各種細微事件,利用這個內核接口,第三方軟件就可以監控文件系統下文件的各種變化情況,而inotify-tools就是這樣的一個第三方軟件。
在前面有講到,rsync可以實現觸發式的文件同步,但是通過crontab守護進程方式進行觸發,同步的數據和實際數據會有差異,而inotify可以監控文件系統的各種變化,當文件有任何變動時,就觸發rsync同步,這樣剛好解決了同步數據的實時性問題。
環境說明:
服務器類型 | IP地址 | 應用 | 操作系統 |
---|---|---|---|
源服務器 | 192.168.153.153 | rsync inotify-tools 腳本 |
centos7/redhat7 |
目標服務器 | 192.168.153.152 | rsync | centos7/redhat7 |
需求:
- 把源服務器上/etc目錄實時同步到目標服務器的/lcr/下
在目標服務器上做以下操作:
//關閉防火墻與SELINUX
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[[email protected] ~]# getenforce
Enforcing
[[email protected] ~]# setenforce 0
[[email protected] ~]# getenforce
Permissive
//安裝rsync服務端軟件
[[email protected] ~]# yum -y install rsync
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
myrepo | 4.1 kB 00:00
(1/2): myrepo/group_gz | 137 kB 00:00
(2/2): myrepo/primary_db | 4.0 MB 00:00
Resolving Dependencies
--> Running transaction check
---> Package rsync.x86_64 0:3.0.9-18.el7 will be installed
......
myrepo/productid | 1.6 kB 00:00
Verifying : rsync-3.0.9-18.el7.x86_64 1/1
Installed:
rsync.x86_64 0:3.0.9-18.el7
Complete!
//設置rsyncd.conf配置文件
[[email protected] ~]# cat >> /etc/rsyncd.conf <<EOF
log file = /var/log/rsyncd.log //日誌文件位置,啟動rsync後自動產生這個文件,無需提前創建
pidfile = /var/run/rsyncd.pid //pid文件的存放位置
lock file = /var/run/rsync.lock //支持max connections參數的鎖文件
secrets file = /etc/rsync.pass //用戶認證配置文件,裏面保存用戶名稱和密碼,必須手動創建這個文件
[etc_from_client] //自定義同步名稱
path = /lcr/ //rsync服務端數據存放路徑,客戶端的數據將同步至此目錄
comment = sync test from client
uid = root //設置rsync運行權限為root
gid = root //設置rsync運行權限為root
port = 873 //默認端口
ignore errors //表示出現錯誤忽略錯誤
use chroot = no //默認為true,修改為no,增加對目錄文件軟連接的備份
read only = no //設置rsync服務端為讀寫權限
list = no //不顯示rsync服務端資源列表
max connections = 200 //最大連接數
timeout = 600 //設置超時時間
auth users = admin //執行數據同步的用戶名,可以設置多個,用英文狀態下逗號隔開
hosts allow = 172.16.12.128 //允許進行數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開
hosts deny = 192.168.1.1 //禁止數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開
> EOF
[[email protected] ~]# echo 'admin:123456' > /etc/rsync.pass
[[email protected] ~]# cat /etc/rsync.pass
admin:123456
[[email protected] ~]# chmod 600 /etc/rsync.pass
[[email protected] ~]# systemctl restart rsyncd
[[email protected] ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[[email protected] ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 5 *:873 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 5 :::873 :::*
[[email protected] ~]#
在源服務器上做以下操作:
/關閉防火墻與SELINUX
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# getenforce
Enforcing
[[email protected] ~]# setenforce 0
//配置yum源
[[email protected] ~]# cd /etc/yum.repos.d/
[[email protected] yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
--2018-08-10 12:07:17-- http://mirrors.163.com/.help/CentOS7-Base-163.repo
Resolving mirrors.163.com (mirrors.163.com)... 59.111.0.251
Connecting to mirrors.163.com (mirrors.163.com)|59.111.0.251|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1572 (1.5K) [application/octet-stream]
Saving to: ‘CentOS7-Base-163.repo’
100%[=================================>] 1,572 --.-K/s in 0s
2018-08-10 12:07:17 (191 MB/s) - ‘CentOS7-Base-163.repo’ saved [1572/1572]
[[email protected] ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[[email protected] ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[[email protected] ~]# yum -y install epel-release
安裝過程略。。。。
//安裝rsync服務端軟件,只需要安裝,不要啟動,不需要配置
[[email protected] ~]# yum -y install rsync
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
myrepo | 4.1 kB 00:00
(1/2): myrepo/group_gz | 137 kB 00:00
(2/2): myrepo/primary_db | 4.0 MB 00:00
Resolving Dependencies
--> Running transaction check
---> Package rsync.x86_64 0:3.0.9-18.el7 will be installed
......
myrepo/productid | 1.6 kB 00:00
Verifying : rsync-3.0.9-18.el7.x86_64 1/1
Installed:
rsync.x86_64 0:3.0.9-18.el7
Complete!
//創建認證密碼文件
[[email protected] ~]# echo '123456' > /etc/rsync.pass
//設置文件權限,只設置文件所有者具有讀取、寫入權限即可
[[email protected] ~]# chmod 600 /etc/rsync.pass
[[email protected] ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 4月 26 03:33 /etc/rsync.pass
//在源服務器上創建測試目錄,然後在源服務器運行以下命令
[[email protected] ~]# mkdir -pv /root/etc/test
mkdir: 已創建目錄 "/root/etc"
mkdir: 已創建目錄 "/root/etc/test"
[[email protected] ~]# rsync -avH --port 873 --progress --delete /root/etc/ [email protected]::test_from_153 --password-file=/etc/rsync.pass
sending incremental file list
./
test/
sent 81 bytes received 23 bytes 208.00 bytes/sec
total size is 0 speedup is 0.00
[[email protected] ~]#
//運行完成後,在目標服務器上查看,在/lcr目錄下有test目錄,說明數據同步成功
驗證:
[[email protected] ~]# cd /lcr/
[[email protected] lcr]# ls
test
[[email protected] lcr]#
//安裝inotify-tools工具,實時觸發rsync進行同步
//查看服務器內核是否支持inotify
[[email protected] ~]# ll /proc/sys/fs/inotify/
總用量 0
-rw-r--r--. 1 root root 0 4月 26 03:49 max_queued_events
-rw-r--r--. 1 root root 0 4月 26 03:49 max_user_instances
-rw-r--r--. 1 root root 0 4月 26 03:49 max_user_watches
//如果有這三個max開頭的文件則表示服務器內核支持inotify
//安裝inotify-tools
[[email protected] ~]# yum -y install make gcc gcc-c++
安裝過程略....
[[email protected] ~]# yum -y install inotify-tools
安裝過程略....
//寫同步腳本,此步乃最最重要的一步,請慎之又慎。讓腳本自動去檢測我們制定的目錄下 //文件發生的變化,然後再執行rsync的命令把它同步到我們的服務器端去
[[email protected] ~]# mkdir /scripts
[[email protected] ~]# touch /scripts/inotify.sh
[[email protected] ~]# chmod 755 /scripts/inotify.sh
[[email protected] ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 0 4月 26 03:52 /scripts/inotify.sh
[email protected] ~]# vim /scripts/inotify.sh
host=192.168.153.152 //目標服務器的ip(備份服務器)
src=/etc //在源服務器上所要監控的備份目錄(此處可以自定義,但是要保證存在)
des=test_from_153 //自定義的模塊名,需要與目標服務器上定義的同步名稱一致
password=/etc/rsync.pass //執行數據同步的密碼文件
user=admin //執行數據同步的用戶名
inotifywait=/usr/bin/inotifywait
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files;do
rsync -avzP --delete --timeout=100 --password-file=${password} $src [email protected]$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
//啟動腳本
[[email protected] ~]# nohup bash /scripts/inotify.sh &
[1] 12221
[[email protected] ~]# nohup: 忽略輸入並把輸出追加到"nohup.out"
[[email protected] ~]# ps -ef|grep inotify
root 12221 1380 0 04:01 pts/0 00:00:00 bash /scripts/inotify.sh
root 12222 12221 0 04:01 pts/0 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
root 12223 12221 0 04:01 pts/0 00:00:00 bash /scripts/inotify.sh
root 12225 1380 0 04:01 pts/0 00:00:00 grep --color=auto inotify
//在源服務器上生成一個新文件
[[email protected] ~]# touch /etc/abc
驗證:
[[email protected] ~]# ls /lcr
test
[[email protected] ~]# ls /lcr
etc test
//查看inotify生成的日誌
[[email protected] ~]# tail /tmp/rsync.log
20190426 04:02 /etc/abcCREATE was rsynced
20190426 04:02 /etc/abcATTRIB was rsynced
設置腳本開機自動啟動:
[[email protected] ~]# chmod +x /etc/rc.d/rc.local
[[email protected] ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x 1 root root 473 Aug 10 23:23 /etc/rc.d/rc.local
[[email protected] ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[[email protected] ~]# tail /etc/rc.d/rc.local
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
nohup /bin/bash /scripts/inotify.sh
到目標服務器上去查看是否把新生成的文件自動傳上去了:
etc test
[[email protected] ~]# ls /lcr/etc/
abc httpd rc2.d
adjtime init.d rc3.d
aliases inittab rc4.d
aliases.db inputrc rc5.d
alternatives iproute2 rc6.d
anacrontab issue rc.d
asound.conf issue.net rc.local
audisp kdump.conf redhat-release
audit kernel resolv.conf
bash_completion.d krb5.conf rhsm
bashrc krb5.conf.d rpc
bbb ld.so.cache rpm
binfmt.d ld.so.conf rsyncd.conf
chkconfig.d ld.so.conf.d rsync.pass
cron.d libaudit.conf rsyslog.conf
cron.daily libnl rsyslog.d
cron.deny libuser.conf rwtab
cron.hourly locale.conf rwtab.d
cron.monthly localtime sasl2
crontab login.defs securetty
cron.weekly logrotate.conf security
crypttab logrotate.d selinux
csh.cshrc lvm services
csh.login machine-id sestatus.conf
dbus-1 magic shadow
default mailcap shadow-
depmod.d makedumpfile.conf.sample shells
dhcp man_db.conf skel
DIR_COLORS mime.types ssh
DIR_COLORS.256color mke2fs.conf ssl
DIR_COLORS.lightbgcolor modprobe.d statetab
dracut.conf modules-load.d statetab.d
dracut.conf.d motd subgid
e2fsck.conf mtab subuid
environment my.cnf subversion
ethertypes my.cnf.d sudo.conf
exports NetworkManager sudoers
favicon.png networks sudoers.d
filesystems nsswitch.conf sudo-ldap.conf
firewalld nsswitch.conf.bak sysconfig
fstab openldap sysctl.conf
fuse.conf opt sysctl.d
gcrypt os-release systemd
gdbinit pam.d system-release
gdbinit.d passwd system-release-cpe
GeoIP.conf passwd- tcsd.conf
GeoIP.conf.default pkcs11 terminfo
gnupg pki tmpfiles.d
GREP_COLORS plymouth tuned
groff pm udev
group polkit-1 vconsole.conf
group- popt.d vimrc
grub2.cfg postfix virc
grub.d ppp vmware-tools
gshadow prelink.conf.d wgetrc
gshadow- printcap wpa_supplicant
gss profile X11
host.conf profile.d xdg
hostname protocols xinetd.d
hosts python yum
hosts.allow rc0.d yum.conf
hosts.deny rc1.d yum.rep
服務管理之rsync