目錄python
rsync是linux系統下的數據鏡像備份工具。使用快速增量備份工具Remote Sync能夠遠程同步,支持本地複製,或者與其餘SSH、rsync主機同步。mysql
rsync支持不少特性:linux
支持匿名傳輸,以方便進行網站鏡象c++
rsync協議sql
rsync server端不用啓動rsync的daemon進程,只要獲取remote host的用戶名和密碼就能夠直接rsync同步文件shell
rsync server端由於不用啓動daemon進程,因此也不用配置文件/etc/rsyncd.confvim
ssh認證協議跟scp的原理是同樣的,若是在同步過程當中不想輸入密碼就用ssh-keygen -t rsa打統統道centos
//這種方式默認是省略了 -e ssh 的,與下面等價: rsync -avz /SRC -e ssh root@172.16.12.129:/DEST -a //文件宿主變化,時間戳不變 -z //壓縮數據傳輸 //當遇到要修改端口的時候,咱們能夠: rsync -avz /SRC -e "ssh -p2222" root@192.168.153.153:/DEST //修改了ssh 協議的端口,默認是22
//Rsync的命令格式經常使用的有如下三種: rsync [OPTION]... SRC DEST rsync [OPTION]... SRC [USER@]HOST:DEST rsync [OPTION]... [USER@]HOST:SRC DEST //對應於以上三種命令格式,rsync有三種不一樣的工做模式: 1)拷貝本地文件。當SRC和DES路徑信息都不包含有單個冒號":"分隔符時就啓動這種工做模式。如: [root@localhost ~]# ls anaconda-ks.cfg azhttpd.sh test tphttpd.sh [root@localhost ~]# rsync -a anaconda-ks.cfg ba [root@localhost ~]# ls anaconda-ks.cfg azhttpd.sh ba test tphttpd.sh [root@localhost ~]# 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路徑地址包 \ 含單個冒號":"分隔符時啓動該模式。如: [root@localhost ~]# rsync -avz ba root@192.168.153.152:/root/aa root@192.168.153.152'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 客戶端驗證: [root@liuyi ~]# 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 [root@liuyi ~]# 3)使用一個遠程shell程序(如rsh、ssh)來實現將遠程機器的內容拷貝到本地機器。當SRC地址路徑 \ 包含單個冒號":"分隔符時啓動該模式。如: [root@localhost ~]# rsync -aqz ba 'ssh' root@192.168.153.152:/root root@192.168.153.152'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] 客戶端驗證: [root@liuyi ~]# ls aa anaconda-ks.cfg ba test //rsync經常使用選項: -a, --archive //歸檔 -v, --verbose //囉嗦模式 -q, --quiet //靜默模式 -r, --recursive //遞歸 -p, --perms //保持原有的權限屬性 -z, --compress //在傳輸時壓縮,節省帶寬,加快傳輸速度 --delete //在源服務器上作的刪除操做也會在目標服務器上同步 delete的用法 [root@localhost ~]# rsync -aqz --delete test 'ssh' root@192.168.153.152:/root root@192.168.153.152'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] 客戶端驗證: [root@liuyi ~]# ls test/ aa anaconda-ks.cfg
rsync與傳統的cp、tar備份方式相比,rsync具備安全性高、備份迅速、支持增量備份等優勢,經過rsync能夠解決對實時性要求不高的數據備份需求,例如按期的備份文件服務器數據到遠端服務器,對本地磁盤按期作數據鏡像等。安全
隨着應用系統規模的不斷擴大,對數據的安全性和可靠性也提出的更好的要求,rsync在高端業務系統中也逐漸暴露出了不少不足,首先,rsync同步數據時,須要掃描全部文件後進行比對,進行差量傳輸。若是文件數量達到了百萬甚至千萬量級,掃描全部文件將是很是耗時的。並且正在發生變化的每每是其中不多的一部分,這是很是低效的方式。其次,rsync不能實時的去監測、同步數據,雖然它能夠經過linux守護進程的方式進行觸發同步,可是兩次觸發動做必定會有時間差,這樣就致使了服務端和客戶端數據可能出現不一致,沒法在應用故障時徹底的恢復數據。基於以上緣由,rsync+inotify組合出現了!bash
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 |
需求:
在目標服務器上作如下操做:
//關閉防火牆與SELINUX [root@liuyi ~]# systemctl stop firewalld.service [root@liuyi ~]# 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. [root@liuyi ~]# getenforce Enforcing [root@liuyi ~]# setenforce 0 [root@liuyi ~]# getenforce Permissive //安裝rsync服務端軟件 [root@liuyi ~]# 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配置文件 [root@localhost ~]# 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 [root@liuyi ~]# echo 'admin:123456' > /etc/rsync.pass [root@liuyi ~]# cat /etc/rsync.pass admin:123456 [root@liuyi ~]# chmod 600 /etc/rsync.pass [root@liuyi ~]# systemctl restart rsyncd [root@liuyi ~]# systemctl enable rsyncd Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service. [root@liuyi ~]# 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 :::* [root@liuyi ~]#
在源服務器上作如下操做:
/關閉防火牆與SELINUX [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld [root@localhost ~]# getenforce Enforcing [root@localhost ~]# setenforce 0 //配置yum源 [root@localhost ~]# cd /etc/yum.repos.d/ [root@localhost 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] [root@localhost ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo [root@localhost ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo [root@localhost ~]# yum -y install epel-release 安裝過程略。。。。 //安裝rsync服務端軟件,只須要安裝,不要啓動,不須要配置 [root@localhost ~]# 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! //建立認證密碼文件 [root@localhost ~]# echo '123456' > /etc/rsync.pass //設置文件權限,只設置文件全部者具備讀取、寫入權限便可 [root@localhost ~]# chmod 600 /etc/rsync.pass [root@localhost ~]# ll /etc/rsync.pass -rw-------. 1 root root 7 4月 26 03:33 /etc/rsync.pass //在源服務器上建立測試目錄,而後在源服務器運行如下命令 [root@localhost ~]# mkdir -pv /root/etc/test mkdir: 已建立目錄 "/root/etc" mkdir: 已建立目錄 "/root/etc/test" [root@localhost ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.153.152::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 [root@localhost ~]# //運行完成後,在目標服務器上查看,在/lcr目錄下有test目錄,說明數據同步成功 驗證: [root@liuyi ~]# cd /lcr/ [root@liuyi lcr]# ls test [root@liuyi lcr]# //安裝inotify-tools工具,實時觸發rsync進行同步 //查看服務器內核是否支持inotify [root@localhost ~]# 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 [root@localhost ~]# yum -y install make gcc gcc-c++ 安裝過程略.... [root@localhost ~]# yum -y install inotify-tools 安裝過程略.... //寫同步腳本,此步乃最最重要的一步,請慎之又慎。讓腳本自動去檢測咱們制定的目錄下 \ //文件發生的變化,而後再執行rsync的命令把它同步到咱們的服務器端去 [root@localhost ~]# mkdir /scripts [root@localhost ~]# touch /scripts/inotify.sh [root@localhost ~]# chmod 755 /scripts/inotify.sh [root@localhost ~]# ll /scripts/inotify.sh -rwxr-xr-x. 1 root root 0 4月 26 03:52 /scripts/inotify.sh root@localhost ~]# 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 $user@$host::$des echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done //啓動腳本 [root@localhost ~]# nohup bash /scripts/inotify.sh & [1] 12221 [root@localhost ~]# nohup: 忽略輸入並把輸出追加到"nohup.out" [root@localhost ~]# 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 //在源服務器上生成一個新文件 [root@localhost ~]# touch /etc/abc 驗證: [root@liuyi ~]# ls /lcr test [root@liuyi ~]# ls /lcr etc test //查看inotify生成的日誌 [root@localhost ~]# tail /tmp/rsync.log 20190426 04:02 /etc/abcCREATE was rsynced 20190426 04:02 /etc/abcATTRIB was rsynced
設置腳本開機自動啓動:
[root@localhost ~]# chmod +x /etc/rc.d/rc.local [root@localhost ~]# ll /etc/rc.d/rc.local -rwxr-xr-x 1 root root 473 Aug 10 23:23 /etc/rc.d/rc.local [root@localhost ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local [root@localhost ~]# 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 [root@liuyi ~]# 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