rsync
目錄- rsync
- rsync是什么
- rsync特性
- rsync的ssh認證協議
- rsync命令
- rsync實時同步
- 目標服務器上做以下操作:
- 在源服務器上做以下操作:
- 測驗腳本
rsync是什么
rsync是linux系統下的資料鏡像備份工具,使用快速增量備份工具Remote Sync可以遠程同步,支持本地復制,或者與其他SSH、rsync主機同步,
rsync特性
1)可以鏡像保存整個目錄樹和檔案系統,
2)可以很容易做到保持原來檔案的權限、時間、軟硬連接等,
3)無需特殊權限即可安裝,
4)快速:第一次同步時rsync復制全部內容,但在下一次值傳輸修改過的內容
5)壓縮傳輸:rysnc在傳輸的程序中可以實行壓縮及解壓縮操作,可以使用更少的帶寬
6)安全:可以使用scp、ssh等方式來進行檔案傳輸
7)支持匿名傳輸,以方便進行網站鏡像
8)rsync不僅可以遠程同步資料(類似于scp),而且可以本地同步資料(類似于cp),做差異同步
9)openssh 8.0已經把scp標記為過時不建議使用了,建議用sftp或者rsync替代scp
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打通通道
rsync命令
Rsync的命令格式常用的有以下三種:
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
rsync常用選項:
-a, --archive //歸檔
-v, --verbose //詳細模式輸出
-q, --quiet //精簡輸出模式
-r, --recursive //遞回
-p, --perms //保持原有的權限屬性
-z, --compress //在傳輸時壓縮,節省帶寬,加快傳輸速度
--delete //在源服務器上做的洗掉操作也會在目標服務器上同步
rsync實時同步
環境說明:
| 服務器型別 | IP地址 | 應用 | 作業系統 |
|---|---|---|---|
| 源服務器 | 192.168.111.135 | rsync inotify-tools 腳本 | centos8 |
| 目標服務器 | 192.168.111.137 | rsync | centos8 |
需求:
- 把源服務器上/etc目錄實時同步到目標服務器的/tmp/下
兩臺虛擬機都需要關閉防火墻
[root@localhost ~]# systemctl disable --now firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
目標服務器上做以下操作:
[root@localhost ~]# hostnamectl set-hostname 137
[root@localhost ~]# bash
安裝rsync
[root@137 ~]# dnf -y install rsync
[root@137 ~]# yum -y install rsync-daemon
#修改組態檔
[root@137 ~]# vim /etc/rsyncd.conf
log file = /var/log/rsyncd.log #添加內容
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
[etc_from_client]
path = /tmp/
comment = sync etc from client
uid = root
gid = root
port = 873
ignore errors
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = admin
#創建用戶認證檔案
[root@137 ~]# echo 'admin:123456' > /etc/rsync.pass
[root@137 ~]# cat /etc/rsync.pass
admin:123456
#設定檔案權限
[root@137 ~]# chmod 600 /etc/rsync*
[root@137 ~]# ll /etc/rsync*
-rw-------. 1 root root 13 Sep 22 18:41 /etc/rsync.pass
-rw-------. 1 root root 790 Sep 22 18:39 /etc/rsyncd.conf
#啟動rsync服務并設定開機自啟
[root@137 ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@137 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 0.0.0.0:873 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::]:873 [::]:*
在源服務器上做以下操作:
[root@localhost ~]# hostnamectl set-hostname 135
[root@localhost ~]# bash
#安裝rsync服務端軟體,只需要安裝,不要啟動,不需要配置
[root@135 ~]# yum -y install rsync
#創建認密碼檔案
[root@135 ~]# echo '123456' > /etc/rsync.pass
[root@135 ~]# cat /etc/rsync.pass
123456
#設定檔案權限,只設定檔案所有者具有讀取、寫入權限即可
[root@135 ~]# chmod 660 /etc/rsync.pass
[root@135 ~]# ll /etc/rsync.pass
-rw-rw----. 1 root root 7 Sep 22 18:50 /etc/rsync.pass
#在源服務器上創建測驗目錄,然后在源服務器運行以下命令
[root@135 ~]# mkdir -pv /root/etc/test
mkdir: created directory '/root/etc'
mkdir: created directory '/root/etc/test'
[root@135 ~]# rsync -avH --port 873 --progress --delete /root/etc/ [email protected]::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
deleting vmware-root_933-3988752732/
./
test/
sent 77 bytes received 58 bytes 270.00 bytes/sec
total size is 0 speedup is 0.00
#安裝inotify-tools工具,實時觸發rsync進行同步
[root@135 ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 Sep 22 18:53 max_queued_events
-rw-r--r--. 1 root root 0 Sep 22 18:53 max_user_instances
-rw-r--r--. 1 root root 0 Sep 22 18:53 max_user_watches
如果有這三個max開頭的檔案則表示服務器內核支持inotify
#安裝inotify-tools
[root@135 ~]# yum -y install make gcc gcc-c++ --allowerasing
[root@135 ~]# yum -y install inotify-tools
#撰寫腳本
[root@135 ~]# mkdir /scripts
[root@135 ~]# touch /scripts/inotify.sh
[root@135 ~]# chmod 755 /scripts/inotify.sh
[root@135 ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 0 Sep 22 19:03 /scripts/inotify.sh
[root@135 ~]# vim /scripts/inotify.sh
host=192.168.111.137
src=https://www.cnblogs.com/etc
des=etc_from_clien
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@135 ~]# nohup bash /scripts/inotify.sh &
[1] 198988
[root@135 ~]# nohup: ignoring input and appending output to 'nohup.out'
[root@135 ~]# ps -ef|grep inotify
root 198988 24963 0 19:11 pts/0 00:00:00 bash /scripts/inotify.sh
root 198989 198988 0 19:11 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 198990 198988 0 19:11 pts/0 00:00:00 bash /scripts/inotify.sh
root 200024 199782 0 19:11 pts/2 00:00:00 grep --color=auto inotify
測驗腳本
//在源服務器上生成一個新檔案
[root@135 ~]# touch /etc/123
#查看inotify生成的日志
[root@135 ~]# tail /tmp/rsync.log
20220922 19:15 /etc/123CREATE was rsynced
20220922 19:15 /etc/123ATTRIB was rsynced
#在目標服務器中也可以看見創建的123檔案
[root@137 ~]# ls
etc test
[root@137 ~]# ls etc/123
etc/123
設定腳本開機自動啟動:
[root@135 ~]# chmod +x /etc/rc.d/rc.local
[root@135 ~]# vim /etc/rc.d/rc.local
#
# 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 & //添加此行
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/509296.html
標籤:其他
上一篇:Shell命令
