linux——rsync简介

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux——rsync简介相关的知识,希望对你有一定的参考价值。

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 [[email protected]]HOST:DEST
rsync [OPTION]... [[email protected]]HOST:SRC DEST

//对应以上三种命令格式,rsync有三种不同的工作模式:
1.拷贝本地文件
[[email protected] ~]# rsync -a nfs.sh a.sh

2.使用远程shell程序(rsh,ssh)来实现将本地机器的内容拷贝到远程机器
[[email protected] ~]# rsync -avz nfs.sh [email protected]:/root/b.sh
[[email protected] ~]# ssh [email protected] ‘ls -l /root‘

3.使用一个远程shell程序(如rsh,ssh)来实现将远程机器的内容拷贝到本地机器
[[email protected] ~]# 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机,方便免密码登陆

[[email protected] ~]# ssh-keygen -t rsa //密钥生成
[[email protected] ~]# ls .ssh/
id_rsa id_rsa.pub
[[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] //A机将生成的公钥交给B机
[[email protected] ~]# ssh [email protected] //尝试登陆,发现无需密码
[[email protected] ~]# exit //退出B机

rsync+inotify
需求:把源服务器上/etc目录实时同步到目标服务器的/tmp/下

安装环境 A B 先关闭防火墙

[[email protected] ~]# systemctl status firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# setenforce 0
AB机安装rsync


[[email protected] ~]# yum -y install rsync A机
[[email protected] ~]# yum -y install rsync B机
B机设置rsyncd.conf配置文件

[[email protected] ~]# touch /etc/rsync.pass
[[email protected] ~]# 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服务并设置开机自启动

[[email protected] ~]# echo ‘admin:123456‘ > /etc/rsync.pass

[[email protected] ~]# chmod 600 /etc/rsync
[[email protected] ~]# 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


[[email protected] ~]# systemctl start 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 -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 :873 :
LISTEN 0 5 :::873 :::


A机

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


//安装rsync服务端软件,只需要安装,不要启动,不需配置
[[email protected] ~]# yum -y install rsync
[[email protected] ~]# echo ‘123456‘ > /etc/rsync.pass
[[email protected] ~]# cat /etc/rsync.pass
123456

//设置文件权限,只设置文件所有者具有读取,写入权限即可
[[email protected] ~]# chmod 600 /etc/rsync.pass
[[email protected] ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 8月 13 17:51 /etc/rsync.pass

[[email protected] ~]# ls //创建测试目录
anaconda-ks.cfg

[[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]::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机

[[email protected] ~]# ls /tmp
test
[[email protected] ~]# 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
[[email protected] ~]# yum -y install make gcc gcc-c++
[[email protected] ~]# yum -y install inotify-tools


A机


//写同步脚本,最重要的一步,让脚本自动去检测我们制定的目录下
//文件发生的变化,然后执行rsunc的命令把它同步到服务器端
[[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 8月 13 18:15 /scripts/inotify.sh


[[email protected] ~]# 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 [email protected]$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done


//检查脚本
[[email protected] bin]# bash -x /scripts/inotify.sh

//启动脚本
[[email protected] ~]# nohup bash /scripts/inotify.sh &
[1] 58297

[[email protected] 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


//在源服务器上生成一个新文件
[[email protected] ~]# mkdir -p /etc/httpd24
[[email protected] ~]# ls /etc/httpd24
[[email protected] ~]# echo ‘hello world‘ > /etc/httpd24/test


//查看inotify生成的日志
[[email protected] ~]# 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 //同步修改


设置开机自启动

[[email protected] ~]# chmod +x /etc/rc.d/rc.local
[[email protected] ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 4月 11 15:36 /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


到目标服务器上查看是否把新生成的文件自动传上去了:

[[email protected] tmp]# pwd
/tmp
[[email protected] tmp]# ls
etc test

[[email protected] tmp]# ls etc/httpd24/
test
//以将源服务器的/etc目录整个同步到了目标服务器,新增的test文件也自动同步了

以上是关于linux——rsync简介的主要内容,如果未能解决你的问题,请参考以下文章

Rsync服务简介部署使用及原理详解

rsync

rsync

rsync

rsync

rsync