第1章 rsync 軟件介紹
1.1 什么是rsync
rsync 是一款開源的、快速的、多功能的、可實現全量及增量的本地或遠程數據同步備份的優秀工具。
1.1.1 全量及增量
全量:將全部數據,進行傳輸覆蓋
增量:只傳輸差異部分的數據
1.2 實現增量復制的原理
Rsync通過其獨特的“quick check”算法,實現增量傳輸數據
[root@iso-all ~]# man rsync
在同步備份數據時,默認情況下,Rsync通過其獨特的“quick check”算法,它僅同步大小或者最后修改時間發生變化的文件或目錄,當然也可根據權限,屬主等屬性的變化同步,但需要指定相應的參數,甚至可以實現只同步一個文件里有變化的內容部分,所以,可以實現快速的同步備份數據。
1.2.1 軟件版本
[root@iso-all ~]# rsync --version rsync version 3.1.2 protocol version 31 Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others. Web site: http://rsync.samba.org/ Capabilities: 64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints, socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace, append, ACLs, xattrs, iconv, symtimes, prealloc rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public Licence for details.
1.3 rsync 軟件功能介紹
類似於 cp 命令 -- 實現本地備份傳輸數據 類似於scp 命令 -- 遠程備份傳輸數據 類似於 rm 命令 -- 實現無差異同步備份 類似於 ls 命令 -- 本地文件信息查看
1.3.1 rsync == cp
[root@iso-all test]# cp -a 1.txt /tmp [root@iso-all test]# ls /tmp 1.txt a-test.sh b-test.sh c-test.sh test.sh [root@iso-all test]# rm /tmp/1.txt -f [root@iso-all test]# ls /tmp a-test.sh b-test.sh c-test.sh test.sh [root@iso-all test]# rsync 1.txt /tmp [root@iso-all test]# ls /tmp 1.txt a-test.sh b-test.sh c-test.sh test.sh
1.3.2 rsync == scp
#使用scp實現
#檢查對端服務器目標位置上是否有該文件
[root@10e0e0e17 ~]# ls -l total 4 -rw-------. 1 root root 1259 Mar 26 2019 anaconda-ks.cfg
#從本地拷貝到遠端服務器上
[root@iso-all ~]# scp -rp test 10.0.0.17:/root/ 1.txt 100% 0 0.0KB/s 00:00 2.txt 100% 0 0.0KB/s 00:00 3.txt 100% 0 0.0KB/s 00:00 4.txt 100% 0 0.0KB/s 00:00 5.txt 100% 0 0.0KB/s 00:00 6.txt 100% 0 0.0KB/s 00:00 7.txt 100% 0 0.0KB/s 00:00 8.txt 100% 0 0.0KB/s 00:00 9.txt 100% 0 0.0KB/s 00:00 10.txt 100% 0 0.0KB/s 00:00 [root@iso-all ~]#
#檢查遠端服務器上的結果
[root@10e0e0e17 ~]# ls -l total 4 -rw-------. 1 root root 1259 Mar 26 2019 anaconda-ks.cfg [root@10e0e0e17 ~]# ls -l total 8 -rw-------. 1 root root 1259 Mar 26 2019 anaconda-ks.cfg drwxr-xr-x. 19 root root 4096 Sep 24 06:12 test
#使用rsync 實現
[root@iso-all ~]# rsync -rp test 10.0.0.17:/tmp/ [root@iso-all ~]#
#檢查遠端服務器上的結果
[root@10e0e0e17 ~]# ls /tmp
test
[root@10e0e0e17 ~]#
1.3.3 rsync == rm
環境准備
[root@iso-all test]# rm -rf {a..d} [root@iso-all test]# [root@iso-all test]# rm -rf {1..5}.txt [root@iso-all test]# [root@iso-all test]# [root@iso-all test]# ls 10.txt 6.txt 7.txt 8.txt 9.txt [a..p] e f g h i j k l m n o p
與遠端進行同步
[root@iso-all ~]# rsync -a --delete test/ 10.0.0.17:/tmp/test/
查看遠端服務器目錄
[root@10e0e0e17 ~]# ls /tmp/test/ 10.txt 6.txt 7.txt 8.txt 9.txt [a..p] e f g h i j k l m n o p
1.3.4 rsync == ls -l
使用rsync 可以實現與 ls 類似的功能
[root@iso-all ~]# ls -l test 總用量 0 -rw-r--r-- 1 root root 0 9月 24 06:12 10.txt -rw-r--r-- 1 root root 0 9月 24 06:12 6.txt -rw-r--r-- 1 root root 0 9月 24 06:12 7.txt -rw-r--r-- 1 root root 0 9月 24 06:12 8.txt -rw-r--r-- 1 root root 0 9月 24 06:12 9.txt drwxr-xr-x 2 root root 6 9月 24 06:11 [a..p] drwxr-xr-x 2 root root 6 9月 24 06:11 e drwxr-xr-x 2 root root 6 9月 24 06:11 f drwxr-xr-x 2 root root 6 9月 24 06:11 g drwxr-xr-x 2 root root 6 9月 24 06:11 h drwxr-xr-x 2 root root 6 9月 24 06:11 i drwxr-xr-x 2 root root 6 9月 24 06:11 j drwxr-xr-x 2 root root 6 9月 24 06:11 k drwxr-xr-x 2 root root 6 9月 24 06:11 l drwxr-xr-x 2 root root 6 9月 24 06:11 m drwxr-xr-x 2 root root 6 9月 24 06:11 n drwxr-xr-x 2 root root 6 9月 24 06:11 o drwxr-xr-x 2 root root 6 9月 24 06:11 p [root@iso-all ~]# rsync test/ drwxr-xr-x 194 2020/09/24 06:29:44 . -rw-r--r-- 0 2020/09/24 06:12:12 10.txt -rw-r--r-- 0 2020/09/24 06:12:12 6.txt -rw-r--r-- 0 2020/09/24 06:12:12 7.txt -rw-r--r-- 0 2020/09/24 06:12:12 8.txt -rw-r--r-- 0 2020/09/24 06:12:12 9.txt drwxr-xr-x 6 2020/09/24 06:11:29 [a..p] drwxr-xr-x 6 2020/09/24 06:11:44 e drwxr-xr-x 6 2020/09/24 06:11:44 f drwxr-xr-x 6 2020/09/24 06:11:44 g drwxr-xr-x 6 2020/09/24 06:11:44 h drwxr-xr-x 6 2020/09/24 06:11:44 i drwxr-xr-x 6 2020/09/24 06:11:44 j drwxr-xr-x 6 2020/09/24 06:11:44 k drwxr-xr-x 6 2020/09/24 06:11:44 l drwxr-xr-x 6 2020/09/24 06:11:44 m drwxr-xr-x 6 2020/09/24 06:11:44 n drwxr-xr-x 6 2020/09/24 06:11:44 o drwxr-xr-x 6 2020/09/24 06:11:44 p
1.4 Rsync的特性總結(7個特性信息說明)
01. 支持拷貝普通文件與特殊文件如鏈接文件,設備等。 02. 可以有排除指定文件或目錄同步的功能,相當於打包命令tar的排除功能。 #tar zcvf backup_1.tar.gz /opt/data -exclude=clsn 說明:在打包/opt/data時就排除了clsn命名的目錄和文件。 03. 可以做到保持原文件或目錄的權限、時間、軟硬鏈接、屬主、組等所有屬性均不改變-p。 04. 可實現增量同步,既只同步發生變化的數據,因此數據傳輸效率很高(tar -N)。 # 將備份/home 目錄自 2008-01-29 以來修改過的文件 # tar -N 2008-01-29 -zcvf /backups/inc-backup_$(date +%F).tar.gz /home # 將備份 /home 目錄昨天以來修改過的文件 # tar -N $(date -d yesterday "+%F") -zcvf /backups/inc-backup_$(date +%F).tar.gz /home # 添加文件到已經打包的文件 # tar -rf all.tar *.gif 說明:這條命令是將所有.gif的文件增加到all.tar的包里面去。-r是表示增加文件的意思。 05. 可以使用rcp,rsh,ssh等方式來配合進行隧道加密傳輸文件(rsync本身不對數據加密) 06. 可以通過socket(進程方式)傳輸文件和數據(服務端和客戶端)*****。重點掌握 07. 支持匿名的或認證(無需系統用戶)的進程模式傳輸,可實現方便安全的進行數據備份及鏡像。
1.5 Rsync的企業工作場景說明
01. 兩台服務器之間數據同步(定時任務cron+rsync) 同步網站內部人員數據信息(定時任務最小周期為1分鍾) 02. 兩台服務器之間數據同步(實時任務inotify/sersync/lrsyncd+rsync) 同步網站用戶人員數據信息
第2章 rsync使用方式
2.1 rsync軟件工作方式
SYNOPSIS 本地數據同步方式 Local: rsync [OPTION...] SRC... [DEST] 遠程數據同步方式 Access via remote shell: Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST: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
2.1.1 本地數據同步方式(類似於cp)
Local: rsync [OPTION...] SRC... [DEST]
參數 |
含義 |
rsync |
數據同步命令 |
[OPTION...] |
rsync命令參數信息 |
SRC |
要同不得數據信息(文件或目錄) |
[DEST] |
將數據傳輸到什么位置 |
實例演示命令:
[root@iso-all test]# cp -a 1.txt /tmp [root@iso-all test]# ls /tmp 1.txt a-test.sh b-test.sh c-test.sh test.sh [root@iso-all test]# rm /tmp/1.txt -f [root@iso-all test]# ls /tmp a-test.sh b-test.sh c-test.sh test.sh [root@iso-all test]# rsync 1.txt /tmp [root@iso-all test]# ls /tmp 1.txt a-test.sh b-test.sh c-test.sh test.sh
2.1.2 遠程數據同步方式(類似scp)---又稱為隧道傳輸
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
說明:需要進行交互傳輸數據。如果想實現免交互傳輸數據,需要借助ssh+key方式實現
pull: 拉: |
|
[USER@] : |
以什么用戶身份傳輸數據信息 |
HOST: |
遠程主機信息(IP地址信息 主機名稱信息) |
SRC: |
遠端要恏過來的數據信息 |
[dest] |
恏到本地什么位置 |
push:推: |
|
SRC: |
本地要懟過去的數據信息 |
DEST |
懟到遠端什么位置 |
2.1.3 【實踐操作】pull 拉
從遠端拉文件到當前目錄
[root@iso-all test]# rm yhh.txt rm:是否刪除普通文件 "yhh.txt"?y [root@iso-all test]# [root@iso-all test]# [root@iso-all test]# [root@iso-all test]# rsync 10.0.0.17:/root/yhh.txt . [root@iso-all test]# ls 10.txt 6.txt 7.txt 8.txt 9.txt [a..p] e f g h i j k l m n o p yhh.txt
2.1.4 【實踐操作】push 推 (目錄)
使用push的格式 推整個目錄(包括目錄
[root@iso-all ~]# rsync -a /etc 10.0.0.17:/tmp/ [root@iso-all ~]#
遠端查看結果
[root@10e0e0e17 tmp]# ls
etc
推整個目錄下的文件(不包括目錄本身)
[root@iso-all ~]# rsync -a /etc/ 10.0.0.17:/tmp/ [root@iso-all ~]#
遠端查看
[root@10e0e0e17 tmp]# ls adjtime csh.login gnupg krb5.conf mtab profile rwtab.d sysctl.d aliases dbus-1 GREP_COLORS krb5.conf.d mtools.conf profile.d sasl2 systemd aliases.db default groff ld.so.cache my.cnf protocols securetty system-release alternatives depmod.d group ld.so.conf my.cnf.d python security system-release-cpe anacrontab dhcp group- ld.so.conf.d NetworkManager rc0.d selinux tcsd.conf asound.conf DIR_COLORS grub2.cfg libaudit.conf networks rc1.d services terminfo audisp DIR_COLORS.256color grub.d libnl nsswitch.conf rc2.d sestatus.conf tmpfiles.d audit DIR_COLORS.lightbgcolor gshadow libuser.conf nsswitch.conf.bak rc3.d shadow tuned bash_completion.d dracut.conf gshadow- locale.conf openldap rc4.d shadow- udev bashrc dracut.conf.d gss localtime opt rc5.d shells vconsole.conf binfmt.d e2fsck.conf host.conf login.defs os-release rc6.d skel vimrc centos-release environment hostname logrotate.conf pam.d rc.d ssh virc centos-release-upstream ethertypes hosts logrotate.d passwd rc.local ssl vsftpd chkconfig.d exports hosts.allow lvm passwd- redhat-release statetab wgetrc cobbler favicon.png hosts.deny machine-id pkcs11 resolv.conf statetab.d wpa_supplicant cron.d filesystems httpd magic pki resolv.conf.GMZVA0 subgid X11 cron.daily firewalld init.d mailcap plymouth resolv.conf.save subuid xdg cron.deny fonts inittab makedumpfile.conf.sample pm rpc subversion xinetd.conf cron.hourly fstab inputrc man_db.conf polkit-1 rpm sudo.conf xinetd.d cron.monthly gcrypt iproute2 mime.types popt.d rsyncd.conf sudoers yum crontab gdbinit issue mke2fs.conf postfix rsync.password sudoers.d yum.conf cron.weekly gdbinit.d issue.net modprobe.d ppp rsyslog.conf sudo-ldap.conf yum.repos.d crypttab GeoIP.conf kdump.conf modules-load.d prelink.conf.d rsyslog.d sysconfig csh.cshrc GeoIP.conf.default kernel motd printcap rwtab sysctl.conf
說明:
/tmp --表示將tmp目錄本身及目錄下的內容進行傳輸
/tmp/ --表示只傳輸tmp目錄下面的內容信息
2.2 守護進程方式同步數據
2.2.1 配置rsync守護進程方式(需要有服務端與客戶端)
規划:
1、iso-all服務器作為rsync服務端 2、以rysnc客戶端作為參照物,將數據推到rsync服務器上
2.2.2 配置rsync服務端(將服務端配置到 iso-all服務器上)
第一個里程碑: 軟件是否存在
[root@iso-all ~]# rpm -qa |grep rsync rsync-3.1.2-10.el7.x86_64
第二個里程碑: 進行軟件服務配置
[root@iso-all ~]# vim /etc/rsyncd.conf 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 = 10.0.0.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password [backup] comment = "backup file by yhh.txt" path = /test
第三個里程:創建rsync用戶
[root@iso-all ~]# id rsync id: rsync: no such user [root@iso-all ~]# useradd -s /sbin/nologin -M rsync [root@iso-all ~]# id rsync uid=1003(rsync) gid=1003(rsync) 組=1003(rsync)
第四個里程碑: 創建數據備份儲存目錄,目錄修改屬主
[root@iso-all ~]# chown -R rsync.rsync test
[root@iso-all ~]#
第五個里程碑: 創建認證用戶密碼文件
[root@iso-all ~]# echo "rsync_backup:123456" >>/etc/rsync.password [root@iso-all ~]# chmod 600 /etc/rsync.password [root@iso-all ~]#
第六個里程碑: 啟動rsync服
[root@iso-all ~]# rsync --daemon
至此服務端配置完成
[root@iso-all ~]# ps -ef |grep rsync root 7114 1 0 21:59 ? 00:00:00 rsync --daemon root 7117 7033 0 22:00 pts/0 00:00:00 grep --color=auto rsync [root@iso-all ~]# ss -lntp|grep rsync LISTEN 0 5 *:873 *:* users:(("rsync",pid=7114,fd=3)) LISTEN 0 5 :::873 :::* users:(("rsync",pid=7114,fd=5)) [root@iso-all ~]#
2.2.3 配置rsync客戶端(其他服務器為客戶端)
第一個里程碑: 軟件是否存在
[root@10e0e0e17 ~]# rpm -qa |grep rsync rsync-3.1.2-10.el7.x86_64
第二個里程碑: 創建認證文件
客戶端的認證文件只需要有密碼即可
echo "123456" >>/etc/rsync.password chmod 600 /etc/rsync.password
第三個里程碑: 實現數據傳輸
交互式
[root@10e0e0e17 ~]# rsync -avzP /root/yhh.txt rsync_backup@10.0.0.15::backup Password: sending incremental file list yhh.txt 33 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/1) rsync: chgrp ".yhh.txt.BQkcGt" (in backup) failed: Operation not permitted (1) sent 124 bytes received 126 bytes 71.43 bytes/sec total size is 33 speedup is 0.13 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
免交互式
[root@10e0e0e17 ~]# rsync -avzP /root/yhh.txt rsync_backup@10.0.0.15::backup --password-file=/etc/rsync.password sending incremental file list yhh.txt 33 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/1) rsync: chgrp ".yhh.txt.4cZIXI" (in backup) failed: Operation not permitted (1) sent 124 bytes received 126 bytes 500.00 bytes/sec total size is 33 speedup is 0.13 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
2.3 rsync 命令同步參數選項&特殊參數
目錄參數 |
參數說明 |
-v ,--verbose |
詳細模式輸出,傳輸時的信息 |
-z,--compress |
傳輸時進行壓縮以提高傳輸效率 --compress-level=NUM 可按級別壓縮 局域網可以不用壓縮 |
-a,--archive (主要) |
歸檔模式,表示以遞歸方式傳輸文件,並保持文件屬性。等於 -rtopgDl |
-r,--recursive 歸檔於-a |
對子目錄以遞歸模式,即目錄下的所有目錄都同樣傳輸。小寫r |
-t,--times 歸檔於-a |
保持文件時間信息 |
-o,--owner 歸檔於-a |
保持文件屬主信息 |
-p,--perms 歸檔於-a |
保持文件權限 |
-g,--group 歸檔於-a |
保持文件屬組信息 |
-P,--progress |
顯示同步的過程及傳輸時的進度等信息(大P) |
-D,--devices 歸檔於-a |
保持設備文件信息 |
-l,--links 歸檔於-a |
保留軟連接(小寫字母l) |
-e,--rsh=COMMAND |
使用的信道協議(remote shell),指定替代rsh的shell程序。 例如 ssh |
--exclude=PATTERN |
指定排除不需要傳輸的文件信息 |
--exclude-from=file |
文件名所在目錄文件,即可以實現排除多個文件 |
--bwlimit=RATE |
限速功能 |
--delete |
讓目標目錄SRC和源目錄數據DST一致,即無差異數據同步 |
保持同步目錄及文件屬性: 這里的-avzP相當於 -vzetopdDlP,生產環境常用的參數為 -avzP 在腳本中可以報-vP去掉 --progress可以用-P代替 |
|
daemon啟動擴展參數 |
|
--daemon |
daemon表示以守護進程的方式啟動rsync服務。 |
--address |
綁定指定IP地址提供服務。 |
--config=FILE |
更改配置文件路徑,而不是默認的/etc/rsyncd.conf |
--port=PORT |
更改其它端口提供服務,而不是缺省的873端口 |
2.3.1 特殊參數實踐
指定ip:
[root@iso-all ~]# rsync --daemon --address=10.0.0.15 [root@iso-all ~]# [root@iso-all ~]# [root@iso-all ~]# ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 5 10.0.0.15:873 *:* users:(("rsync",pid=8950,fd=3)) LISTEN 0 128 *:22 *:* users:(("sshd",pid=6770,fd=3)) LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=6921,fd=13)) LISTEN 0 128 :::22 :::* users:(("sshd",pid=6770,fd=4)) LISTEN 0 100 ::1:25 :::* users:(("master",pid=6921,fd=14))
指定配置文件路徑
[root@iso-all ~]# rsync --daemon --config=/etc/rsyncd.conf
服務端指定服務端口:
[root@iso-all ~]# rsync --daemon --port=5222 [root@iso-all ~]# ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users:(("sshd",pid=6770,fd=3)) LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=6921,fd=13)) LISTEN 0 5 *:5222 *:* users:(("rsync",pid=8960,fd=3)) LISTEN 0 128 :::22 :::* users:(("sshd",pid=6770,fd=4)) LISTEN 0 100 ::1:25 :::* users:(("master",pid=6921,fd=14)) LISTEN 0 5 :::5222 :::* users:(("rsync",pid=8960,fd=5))
第3章 rsycn配置文件詳解 rsyncd.conf
3.1 部分知識補充
3.1.1 配置文件內容參考資料
man rsyncd.conf
3.1.2 配置文件內容總結
模塊之上內容為全局變量信息
模塊之下內容為局部變量信息
說明:
無論是全局變量發生變化,還是局部變量發生變化,都建議重啟rsync服務使配置生效。
3.2 守護進程多模塊功能配置
第一個里程碑: 編寫配置信息創建多模塊
...... [test1] comment = "test1 dir by 10e0e0e12" path = /root/test1 [test2] comment = "test2 dir by 10e0e0e17" path = /root/test2
第二個里程碑: 創建多模塊指定的目錄
[root@iso-all ~]# mkdir test{1..3} [root@iso-all ~]# ls -l drwxr-xr-x 15 rsync rsync 209 9月 25 17:29 test drwxr-xr-x 2 root root 6 9月 25 17:59 test1 drwxr-xr-x 2 root root 6 9月 25 17:59 test2 drwxr-xr-x 2 root root 6 9月 25 17:59 test3 [root@iso-all ~]# chown -R rsync.rsync test1 [root@iso-all ~]# [root@iso-all ~]# chown -R rsync.rsync test2 [root@iso-all ~]# chown -R rsync.rsync test3 [root@iso-all ~]# ls -l drwxr-xr-x 15 rsync rsync 209 9月 25 17:29 test drwxr-xr-x 2 rsync rsync 6 9月 25 17:59 test1 drwxr-xr-x 2 rsync rsync 6 9月 25 17:59 test2 drwxr-xr-x 2 rsync rsync 6 9月 25 17:59 test3
說明:
rsyncd.conf配置文件中,添加多模塊信息,可以不用重啟rsync服務,即時生效~
全局變量參數針對所有模塊生效;局部變量參數只針對指定模塊生效
read only參數默認配置為ture,即為只讀模式
全局變量發生變化,不用重啟rsync服務;局部變量發生變化,需要重啟rsync服務
注意:修改配置文件就重啟
無論是全局變量發生變化,還是局部變量發生變化,都建議重啟rsync服務使配置生效
3.3 守護進程的排除功能實踐
3.3.1 排除的方式
a) --exclude=要配置的目錄或文件名稱
b) --exclude-from=要排除多個目錄或文件匯總文件名稱
c) 在配置文件中進行修改,指定要排除的信息
3.3.2 排除測試
第一個里程碑: 創建模擬測試環境
[root@10e0e0e17 test]# ls -l total 0 -rw-r--r--. 1 root root 0 Sep 24 06:12 10.txt -rw-r--r--. 1 root root 0 Sep 24 06:12 1.txt -rw-r--r--. 1 root root 0 Sep 24 06:12 2.txt -rw-r--r--. 1 root root 0 Sep 24 06:12 3.txt -rw-r--r--. 1 root root 0 Sep 24 06:12 4.txt -rw-r--r--. 1 root root 0 Sep 24 06:12 5.txt -rw-r--r--. 1 root root 0 Sep 24 06:12 6.txt -rw-r--r--. 1 root root 0 Sep 24 06:12 7.txt -rw-r--r--. 1 root root 0 Sep 24 06:12 8.txt -rw-r--r--. 1 root root 0 Sep 24 06:12 9.txt drwxr-xr-x. 2 root root 6 Sep 24 06:11 a drwxr-xr-x. 2 root root 6 Sep 24 06:11 [a..p] drwxr-xr-x. 2 root root 6 Sep 24 06:11 b drwxr-xr-x. 2 root root 6 Sep 24 06:11 c drwxr-xr-x. 2 root root 6 Sep 24 06:11 d drwxr-xr-x. 2 root root 6 Sep 24 06:11 e drwxr-xr-x. 2 root root 6 Sep 24 06:11 f drwxr-xr-x. 2 root root 6 Sep 24 06:11 g drwxr-xr-x. 2 root root 6 Sep 24 06:11 h drwxr-xr-x. 2 root root 6 Sep 24 06:11 i drwxr-xr-x. 2 root root 6 Sep 24 06:11 j drwxr-xr-x. 2 root root 6 Sep 24 06:11 k drwxr-xr-x. 2 root root 6 Sep 24 06:11 l drwxr-xr-x. 2 root root 6 Sep 24 06:11 m drwxr-xr-x. 2 root root 6 Sep 24 06:11 n drwxr-xr-x. 2 root root 6 Sep 24 06:11 o drwxr-xr-x. 2 root root 6 Sep 24 06:11 p
第二個里程碑 利用 --exclude參數測試排除功能
[root@10e0e0e17 test]# rsync -avzP /root/test --exclude=3.txt --exclude=5.txt rsync_backup@10.0.0.15::test1 --password-file=/etc/rsync.password sending incremental file list rsync: chgrp "test" (in test1) failed: Operation not permitted (1) test/ test/1.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=24/26) test/10.txt 0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=23/26) test/2.txt 0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=22/26) test/4.txt 0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=21/26) test/6.txt 0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=20/26) test/7.txt 0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=19/26) test/8.txt 0 100% 0.00kB/s 0:00:00 (xfr#7, to-chk=18/26) test/9.txt 0 100% 0.00kB/s 0:00:00 (xfr#8, to-chk=17/26) rsync: chgrp "test/[a..p]" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/a" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/b" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/c" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/d" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/e" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/f" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/g" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/h" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/i" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/j" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/k" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/l" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/m" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/n" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/o" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/p" (in test1) failed: Operation not permitted (1) test/[a..p]/ test/a/ test/b/ test/c/ test/d/ test/e/ test/f/ test/g/ test/h/ test/i/ test/j/ test/k/ test/l/ test/m/ test/n/ test/o/ test/p/ rsync: chgrp "test/.1.txt.ZUDib0" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.10.txt.AihsiP" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.2.txt.gmtCpE" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.4.txt.Fn5Mwt" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.6.txt.5n6XDi" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.7.txt.58taL7" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.8.txt.j8fnSW" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.9.txt.MyKAZL" (in test1) failed: Operation not permitted (1) sent 693 bytes received 2,250 bytes 5,886.00 bytes/sec total size is 0 speedup is 0.00 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
[root@iso-all test]# ls 10.txt 1.txt 2.txt 4.txt 6.txt 7.txt 8.txt 9.txt a [a..p] b c d e f g h i j k l m n o p
3.3.3 利用--exclude-from 方式進行排除
第一個里程碑: 創建模擬測試環境
[root@iso-all test1]# rm test/ -rf [root@iso-all test1]# [root@iso-all test1]# [root@iso-all test1]# ls -l 總用量 0
第二個里程碑:利用--exlude-from參數,測試排除功能
[root@10e0e0e17 ~]# vim /tmp/paichu.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt a b
第三個里程碑:進行排除
[root@10e0e0e17 ~]# rsync -avzP /root/test --exclude-from=/tmp/paichu.txt rsync_backup@10.0.0.15::test1 --password-file=/etc/rsync.password sending incremental file list rsync: chgrp "test" (in test1) failed: Operation not permitted (1) test/ test/1.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=18/20) test/10.txt 0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=17/20) test/2.txt 0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=16/20) rsync: chgrp "test/[a..p]" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/b" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/c" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/d" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/e" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/f" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/g" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/h" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/i" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/j" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/k" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/l" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/m" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/n" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/o" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/p" (in test1) failed: Operation not permitted (1) test/[a..p]/ test/b/ test/c/ test/d/ test/e/ test/f/ test/g/ test/h/ test/i/ test/j/ test/k/ test/l/ test/m/ test/n/ test/o/ test/p/ rsync: chgrp "test/.1.txt.xmOHk0" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.10.txt.WBMC1X" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.2.txt.8rQyIV" (in test1) failed: Operation not permitted (1) sent 448 bytes received 1,653 bytes 4,202.00 bytes/sec total size is 0 speedup is 0.00 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
[root@iso-all test]# ll 總用量 0 -rw------- 1 rsync rsync 0 9月 25 18:21 10.txt -rw------- 1 rsync rsync 0 9月 25 18:21 1.txt -rw------- 1 rsync rsync 0 9月 25 18:21 2.txt drwx------ 2 rsync rsync 6 9月 24 06:11 [a..p] drwx------ 2 rsync rsync 6 9月 24 06:11 b drwx------ 2 rsync rsync 6 9月 24 06:11 c drwx------ 2 rsync rsync 6 9月 24 06:11 d drwx------ 2 rsync rsync 6 9月 24 06:11 e drwx------ 2 rsync rsync 6 9月 24 06:11 f drwx------ 2 rsync rsync 6 9月 24 06:11 g drwx------ 2 rsync rsync 6 9月 24 06:11 h drwx------ 2 rsync rsync 6 9月 24 06:11 i drwx------ 2 rsync rsync 6 9月 24 06:11 j drwx------ 2 rsync rsync 6 9月 24 06:11 k drwx------ 2 rsync rsync 6 9月 24 06:11 l drwx------ 2 rsync rsync 6 9月 24 06:11 m drwx------ 2 rsync rsync 6 9月 24 06:11 n drwx------ 2 rsync rsync 6 9月 24 06:11 o drwx------ 2 rsync rsync 6 9月 24 06:11 p
說明:
01. 排除文件中,需要利用相對路徑指定排除信息(不能利用絕對路徑) 02. 相對路徑指的是相對同步的目錄信息而言,是對rsync -avz /data/ 后面的data目錄進行相對
3.4 守護進程來創建備份目錄
通過客戶端命令創建服務端備份目錄中子目錄
[root@10e0e0e17 ~]# rsync -avzP /etc/hosts rsync_backup@10.0.0.15::test1/10e0e0e17/ --password-file=/etc/rsync.password sending incremental file list created directory 10e0e0e17 rsync: change_dir#2 "/10e0e0e17" (in test1) failed: Permission denied (13) rsync error: errors selecting input/output files, dirs (code 3) at main.c(675) [Receiver=3.1.2]
檢查結果:
[root@iso-all test1]# ls
10e0e0e17 test
說明:
a 目標目錄名稱后要加上 "/", 表示創建目錄,否則變為修改傳輸文件名稱了 b 利用客戶端創建服務備份子目錄時,只能創建一級子目錄。
3.5 守護進程的訪問控制配置
第一個里程碑:在服務端配置文件,編寫白名單策略或黑名單策略(只能取其一)
vim /etc/rsyncd.conf hosts allow = 10.0.0.12/32 #hosts deny = 0.0.0.0/32
關於訪問控制的說明:
01. 白名單和黑名單同時存在時,默認控制策略為不匹配的傳輸數據信息全部放行 02. 白名單單一存在時,默認控制策略為不匹配的傳輸數據信息全部禁止 03. 黑名單單一存在時,默認控制策略為不匹配的傳輸數據信息全部放行
全局變量修改控制策略信息,rsync服務必須重啟
第二個里程碑:客戶端進行測試
#10e0e0e12 [root@10e0e0e12 ~]# rsync -avzP /etc/hosts rsync_backup@10.0.0.15::test1/10e0e0e12/ Password: sending incremental file list created directory 10e0e0e12 rsync: change_dir#2 "/10e0e0e12" (in test1) failed: Permission denied (13) rsync error: errors selecting input/output files, dirs (code 3) at main.c(675) [Receiver=3.1.2]
#10e0e0e17 [root@10e0e0e17 ~]# rsync -avzP /etc/hosts rsync_backup@10.0.0.15::test1/10e0e0e17/ --password-file=/etc/rsync.password @ERROR: Unknown module 'test1' rsync error: error starting client-server protocol (code 5) at main.c(1649) [sender=3.1.2]
3.6 守護進程無差異同步配置
3.6.1 什么是無差異:
推模式:我有什么,你就有什么;我沒有,你也不能有
拉模式:你有什么,我就有什么;你沒有,我也不能有
總結:服務端客戶端數據完全一致(一模一樣)
3.6.2 實現無差異同步方法
第一個里程碑: 創建實驗環境
[root@10e0e0e17 ~]# rsync -avzP /root/test --exclude=3.txt --exclude=5.txt rsync_backup@10.0.0.15::test1 --password-file=/etc/rsync.password
第二個里程:刪除指定目錄,並添加指定文件,測試無差異功能
[root@10e0e0e17 test]# rm 4.txt -f [root@10e0e0e17 test]# [root@10e0e0e17 test]# [root@10e0e0e17 test]# rm a -rf [root@10e0e0e17 test]# [root@10e0e0e17 test]# rm c -rf [root@10e0e0e17 test]# [root@10e0e0e17 test]# [root@10e0e0e17 test]# rsync -avzP /root/test --delete rsync_backup@10.0.0.15::test1 --password-file=/etc/rsync.password sending incremental file list rsync: chgrp "test" (in test1) failed: Operation not permitted (1) deleting test/c/ deleting test/a/ deleting test/4.txt test/ test/1.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=23/25) test/10.txt 0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=22/25) test/2.txt 0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=21/25) test/3.txt 0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=20/25) test/5.txt 0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=19/25) test/6.txt 0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=18/25) test/7.txt 0 100% 0.00kB/s 0:00:00 (xfr#7, to-chk=17/25) test/8.txt 0 100% 0.00kB/s 0:00:00 (xfr#8, to-chk=16/25) test/9.txt 0 100% 0.00kB/s 0:00:00 (xfr#9, to-chk=15/25) rsync: chgrp "test/[a..p]" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/b" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/d" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/e" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/f" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/g" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/h" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/i" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/j" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/k" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/l" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/m" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/n" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/o" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/p" (in test1) failed: Operation not permitted (1) test/[a..p]/ test/b/ test/d/ test/e/ test/f/ test/g/ test/h/ test/i/ test/j/ test/k/ test/l/ test/m/ test/n/ test/o/ test/p/ rsync: chgrp "test/.1.txt.quqP4m" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.10.txt.C1PuVi" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.2.txt.C2AbMe" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.3.txt.zuBTCa" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.5.txt.vr5Ct6" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.6.txt.q4Qnk2" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.7.txt.8Ap9aY" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.8.txt.YsIV1T" (in test1) failed: Operation not permitted (1) rsync: chgrp "test/.9.txt.Q9NISP" (in test1) failed: Operation not permitted (1) sent 726 bytes received 2,236 bytes 1,184.80 bytes/sec total size is 0 speedup is 0.00 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
查看結果:
[root@iso-all test]# ls 10.txt 1.txt 2.txt 3.txt 5.txt 6.txt 7.txt 8.txt 9.txt [a..p] b d e f g h i j k l m n o p
第4章 常見問題(本人配置遇到問題)
4.1 客戶端的錯誤現象:No route to host
rsync服務端開啟的iptables防火牆
[root@iso-all ~]# systemctl status iptables ● iptables.service - IPv4 firewall with iptables Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled) Active: active (exited) since 六 2020-09-26 19:41:24 CST; 2s ago Process: 10062 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS) Main PID: 10062 (code=exited, status=0/SUCCESS) 9月 26 19:41:24 iso-all systemd[1]: Starting IPv4 firewall with iptables... 9月 26 19:41:24 iso-all iptables.init[10062]: iptables: Applying firewall rules: [ 確定 ] 9月 26 19:41:24 iso-all systemd[1]: Started IPv4 firewall with iptables.
關閉防火牆
[root@iso-all ~]# systemctl stop iptables [root@iso-all ~]# systemctl status iptables ● iptables.service - IPv4 firewall with iptables Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled) Active: inactive (dead) since 六 2020-09-26 19:42:16 CST; 5s ago Process: 10108 ExecStop=/usr/libexec/iptables/iptables.init stop (code=exited, status=0/SUCCESS) Process: 10062 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS) Main PID: 10062 (code=exited, status=0/SUCCESS) 9月 26 19:41:24 iso-all systemd[1]: Starting IPv4 firewall with iptables... 9月 26 19:41:24 iso-all iptables.init[10062]: iptables: Applying firewall rules: [ 確定 ] 9月 26 19:41:24 iso-all systemd[1]: Started IPv4 firewall with iptables. 9月 26 19:42:16 iso-all systemd[1]: Stopping IPv4 firewall with iptables... 9月 26 19:42:16 iso-all iptables.init[10108]: iptables: Setting chains to policy ACCEPT: filter [ 確定 ] 9月 26 19:42:16 iso-all iptables.init[10108]: iptables: Flushing firewall rules: [ 確定 ] 9月 26 19:42:16 iso-all systemd[1]: Stopped IPv4 firewall with iptables.
4.2 ERROR: The remote path must start with a module name not a /
rsync客戶端執行rsync命令錯誤:
客戶端的錯誤現象:
[root@10e0e0e17 ~]#rsync -avz /etc/hosts rsync_backup@10.0.0.15::/backup ERROR: The remote path must start with a module name not a / rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
異常問題解決:
rsync命令語法理解錯誤,::/backup是錯誤的語法,應該為::backup(rsync模塊)
4.3 @ERROR: auth failed on module backup
3. @ERROR: auth failed on module oldboy
客戶端的錯誤現象:
[root@10e0e0e17 ~]#rsync -avz /etc/hosts rsync_backup@10.0.0.15::/backup Password: @ERROR: auth failed on module backup rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
異常問題解決:
1. 密碼真的輸入錯誤,用戶名真的錯誤
2. secrets file = /etc/rsync.password指定的密碼文件和實際密碼文件名稱不一致
3. /etc/rsync.password文件權限不是600
4. rsync_backup:123456密碼配置文件后面注意不要有空格
5. rsync客戶端密碼文件中只輸入密碼信息即可,不要輸入虛擬認證用戶名稱