1. 程式人生 > >linux——rsync簡介

linux——rsync簡介

服務 base help 無需 send 重要 ks.cfg 生成 嘗試

rsync簡介
rsync是Linux系統下的數據鏡像備份工具,使用快速增量備份工具 Remote sync 可以遠程同步,支持本地復制,或者與其他SSH,rsync主機同步


rsync

-a 歸檔模式
-v 詳細輸出
-q 靜默輸出
-r 對子目錄遞歸模式處理
-p 保持原有的權限屬性
-z 在傳輸時壓縮
--delete 在源服務器上做刪除操作也會在目標服務器上同步

-c 打開效驗開關
-R 使用相對路徑
-b 創建備份


rsync命令
//Rsync的三種命令格式

rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST

//對應以上三種命令格式,rsync有三種不同的工作模式:
1.拷貝本地文件
[root@localhost ~]# rsync -a nfs.sh a.sh

2.使用遠程shell程序(rsh,ssh)來實現將本地機器的內容拷貝到遠程機器
[root@localhost ~]# rsync -avz nfs.sh [email protected]:/root/b.sh
[root@localhost ~]# ssh [email protected] ‘ls -l /root‘

3.使用一個遠程shell程序(如rsh,ssh)來實現將遠程機器的內容拷貝到本地機器
[root@localhost ~]# rsync -avz [email protected]:/etc/yum.repos.d /root/


環境說明
A機——源服務器——IP192.168.56.11——應用(rsync,inotify-tools,腳本 )——centos7系統
B機——目標服務器——IP192.168.56.138——應用(rsync)——centos7系統

使用ssh,傳輸密鑰給B機,方便免密碼登陸

[root@localhost ~]# ssh-keygen -t rsa //密鑰生成
[root@localhost ~]# ls .ssh/
id_rsa id_rsa.pub
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] //A機將生成的公鑰交給B機

[root@localhost ~]# ssh [email protected] //嘗試登陸,發現無需密碼
[root@localhost ~]# exit //退出B機

rsync+inotify
需求:把源服務器上/etc目錄實時同步到目標服務器的/tmp/下

安裝環境 A B 先關閉防火墻

[root@localhost ~]# systemctl status firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
AB機安裝rsync


[root@localhost ~]# yum -y install rsync A機
[root@localhost ~]# yum -y install rsync B機
B機設置rsyncd.conf配置文件

[root@localhost ~]# touch /etc/rsync.pass
[root@localhost ~]# cat >> /etc/rsyncd.conf <<EOF

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
hosts allow = 192.168.56.11
hosts deny = 192.168.1.1
EOF
//創建用戶認證文件,設置文件權限, 啟動rsync服務並設置開機自啟動

[root@localhost ~]# echo ‘admin:123456‘ > /etc/rsync.pass

[root@localhost ~]# chmod 600 /etc/rsync
[root@localhost ~]# ll /etc/rsync

-rw-------. 1 root root 841 8月 14 01:14 /etc/rsyncd.conf
-rw-------. 1 root root 13 8月 14 01:18 /etc/rsync.pass


[root@localhost ~]# systemctl start rsyncd
[root@localhost ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 :873 :
LISTEN 0 5 :::873 :::


A機

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri ‘s/^(SELINUX=)./\1disabled/g‘ /etc/sysconfig/selinux
//配置yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@localhost yum.repos.d]# sed -i ‘s/\$releasever/7/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost yum.repos.d]# sed -i ‘s/^enabled=.
/enabled=1/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost yum.repos.d]# yum -y install epel-release
[root@localhost ~]# yum -y update --skip-broken


//安裝rsync服務端軟件,只需要安裝,不要啟動,不需配置
[root@localhost ~]# yum -y install rsync
[root@localhost ~]# echo ‘123456‘ > /etc/rsync.pass
[root@localhost ~]# cat /etc/rsync.pass
123456

//設置文件權限,只設置文件所有者具有讀取,寫入權限即可
[root@localhost ~]# chmod 600 /etc/rsync.pass
[root@localhost ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 8月 13 17:51 /etc/rsync.pass

[root@localhost ~]# ls //創建測試目錄
anaconda-ks.cfg

[root@localhost ~]# mkdir -pv /root/etc/test
mkdir: 已創建目錄 "/root/etc"
mkdir: 已創建目錄 "/root/etc/test"


[root@localhost ~]# rsync -avH --port 873 --progress --delete /root/etc/ [email protected]::etc_from_client --password-file=/etc/rsync.pass

sending incremental file list
deleting systemd-private-5434e220d18940898b6d9672af036026-vmtoolsd.service-MRNEKi/tmp/vmware-root/
deleting systemd-private-5434e220d18940898b6d9672af036026-vmtoolsd.service-MRNEKi/tmp/
deleting systemd-private-5434e220d18940898b6d9672af036026-vmtoolsd.service-MRNEKi/
deleting systemd-private-5434e220d18940898b6d9672af036026-vgauthd.service-EHAGL9/tmp/
deleting systemd-private-5434e220d18940898b6d9672af036026-vgauthd.service-EHAGL9/
deleting systemd-private-5434e220d18940898b6d9672af036026-cups.service-cQXF9F/tmp/
deleting systemd-private-5434e220d18940898b6d9672af036026-cups.service-cQXF9F/
deleting .font-unix/
deleting .esd-1000/
deleting .XIM-unix/
deleting .X11-unix/
deleting .Test-unix/
deleting .ICE-unix/
./
test/

sent 75 bytes received 670 bytes 1,490.00 bytes/sec
total size is 0 speedup is 0.00


B機

[root@localhost ~]# ls /tmp
test
[root@localhost ~]# ll /proc/sys/fs/inotify/ 查看服務器是否支持inotify,有三max則支持
總用量 0
-rw-r--r--. 1 root root 0 8月 14 02:04 max_queued_events
-rw-r--r--. 1 root root 0 8月 14 02:04 max_user_instances
-rw-r--r--. 1 root root 0 8月 14 02:04 max_user_watches


//安裝inotify-tools
[root@localhost ~]# yum -y install make gcc gcc-c++
[root@localhost ~]# yum -y install inotify-tools


A機


//寫同步腳本,最重要的一步,讓腳本自動去檢測我們制定的目錄下 \
//文件發生的變化,然後執行rsunc的命令把它同步到服務器端
[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 8月 13 18:15 /scripts/inotify.sh


[root@localhost ~]# vim /scripts/inotify.sh
host=192.168.56.138
src=/etc
des=etc_from_client
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 bin]# bash -x /scripts/inotify.sh

//啟動腳本
[root@localhost ~]# nohup bash /scripts/inotify.sh &
[1] 58297

[root@localhost bin]# ps -ef|grep inotify
root 74599 1 0 17: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 74621 2316 0 17:03 pts/0 00:00:00 bash /scripts/inotify.sh
root 74622 74621 0 17:03 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 74623 74621 0 17:03 pts/0 00:00:00 bash /scripts/inotify.sh
root 74625 2316 0 17:03 pts/0 00:00:00 grep --color=auto inotify


//在源服務器上生成一個新文件
[root@localhost ~]# mkdir -p /etc/httpd24
[root@localhost ~]# ls /etc/httpd24
[root@localhost ~]# echo ‘hello world‘ > /etc/httpd24/test


//查看inotify生成的日誌
[root@localhost ~]# tail /tmp/rsync.log
20180816 17:05 /etc/httpd24CREATE,ISDIR was rsynced //創建,ISDIR是同步的
20180816 17:05 /etc/httpd24/testCREATE was rsynced //同步創建
20180816 17:05 /etc/httpd24/testMODIFY 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 4月 11 15:36 /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


到目標服務器上查看是否把新生成的文件自動傳上去了:

[root@localhost tmp]# pwd
/tmp
[root@localhost tmp]# ls
etc test

[root@localhost tmp]# ls etc/httpd24/
test
//以將源服務器的/etc目錄整個同步到了目標服務器,新增的test文件也自動同步了

linux——rsync簡介