sync同步工具使用

2023-03-07,,

sync详解

sync概述:

rsync是一个提供快速增量文件传输的开源工具。rsync在GNU通用公共许可证下免费提供,目前由Wayne Davison维护。传输前进行压缩,适合做备份使用。

命令格式

rsync [-参数] 源目录 目的目录

-r 递归模式,包含目录及子目录中的所有文件

-l(小写L) 对于符号链接文件仍然复制为符号链接文件

-v 显示同步过程中的详细(verbose)信息

-z 在传输文件时进行压缩(compress)

-a 归档模式,保留文件的权限,属性等信息,等同于组合选项"-rlptgoD"

-p 保留文件的权限标记

-t 保留文件的时间标记

-g 保留文件的属组标记(仅超级用户使用)

-o 保留文件的属主标记(仅超级用户使用)

-H 保留硬链接文件

-A 保留ACL属性信息

-D 保留设备文件及其他特殊文件

-S(--sparce) 处理稀疏文件时使用

--delete 删除目录位置有,而原始位置没有的文件。

--checksum 更具校验和(而不是文件大小,修改时间)来决定是否跳过文件.

--password-flie= 指定密码文件

本地传输文件 目录后如果没有加/(/test/)是代表传输整个目录 如果带/(/text)是传输目录下所有文件

systemctl  stop  firewalld
[root@shell-test ~]# rsync -a ./ /opt/
[root@shell-test ~]# cd /opt/
[root@shell-test opt]# ll
total 16
-rw------- 1 root root 1244 Feb 20 01:50 anaconda-ks.cfg
-rw-r--r-- 1 root root 3671 Feb 20 03:20 instalization.sh
drwxr-xr-x 4 root root 92 Aug 1 10:42 script
-rw-r--r-- 1 root root 100 Jul 1 05:24 test.sh
-rw-r--r-- 1 root root 16 Aug 10 11:31 test.txt
[root@shell-test opt]# rsync -a /opt /opt/
[root@shell-test opt]# cd /opt/
[root@shell-test opt]# ll
total 4
dr-xr-x--- 6 root root 198 Aug 10 22:19 opt
drwxr-xr-x 4 root root 92 Aug 1 10:42 script
-rw-r--r-- 1 root root 16 Aug 10 11:31 test.txt

配置rsync同步服务器

rsync+ssh同步数据

systemctl  stop  firewalld
setenforce 0

服务端配置

修改配置文件rsync.conf

uid = nobody
gid = nobody
use chroot = yes #禁锢在源目录
address = 192.168.23.104 #监听地址
port 873 #监听端口(UDP/TCP 873端口)
log file = /var/run/rsyncd.log #日志文件位置
pid file = /var/run/rsyncd.pid #存放进程pid文件的位置
hosts allow = 192.168.23.0/24 #允许访问的客户机
[wwwroot] #模块名称
path = /var/www/html #源目录的实际路径
comment = Document Root of web server #描述信息
read only = yes #设置为只读
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #同步时不再压缩的文件类型
auth users = backuper #授权用户,多个账户用空格分隔
secrets file = /etc/rsyncd_users.db #设置存放账户信息的数据文件

创建账户

vim /etc/rsyncd_users.db
#rsync 的认证用户和密码。不用创建同名的系统用户
backuper:abc123
chomod 600 rsync_users.db

启动服务

rsync --daemon
ss -tnap|grep rsync
[root@shell-test opt]# ss -natp |grep rsync
LISTEN 0 5 192.168.211.30:873 *:* users:(("rsync",pid=5540,fd=3))

客户端验证

rsync -azv backuper@192.168.211.30::wwwroot #IP地址后面是双冒号之后跟共享模块名称
将指定文件目录文件同步,注意客户端要有相对应的目录
[root@ansible-1 ~]# rsync -azv backuper@192.168.211.30::wwwroot /var/www/html
Password:
receiving incremental file list
sent 20 bytes received 34 bytes 15.43 bytes/sec
total size is 0 speedup is 0.00

免交互定时同步数据

#创建文件,将密码写入文件中
echo "abc123" > /etc/server.pass
#设置权限,防止密码文件被其他用户篡改
chmod 600 /etc/server.pass
crontab -e
*/1 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass backuper@192.168.211.30::wwwroot /var/www/html
#每天1:30 j进行备份,将源服务器的wwwroot模块下文件备份到本地的 /opt/目录下
#--deleete 删除源服务器没有,而本地有的文件
#--password-file= 指定保存rsync 用户backuper密码的文件,以实现免交互
[root@host103 opt]# systemctl restart crond
[root@host103 opt]# systemctl enable crond

rsync + inotify 机制实现实时同步

将inotify机制与rsync工具相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作,否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。

[root@host104 ~]# vim /etc/rsyncd.conf
........
#uid和gid 修改问目录/var/www/html 属组和属主
uid = root
gid = root
#关闭只读,上行同步需要可以写。
read only = no
...... #关闭rsync 服务
kill $(cat /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid #启动rsync服务
rsync --daemon
netstat -natp | grep rsync
tcp 0 0 192.168.211.30:873 0.0.0.0:* LISTEN 83003/rsync #修改目录权限
chmod 777 /var/www/html/

发起内核参数调整

在Linux内核中,默认的inotify机制提供了三个调控参数∶max_queue_events(监控事件队列,默认值为16384)、max_user_instances(最多监控实例数,默认值为128)、max_user_watches(每个实例最多监控文件数,默认值为8192)。当要监控的目录、文件数量较多或者变化较频繁时,建议加大这三个参数的值。
cat /proc/sys/fs/inotify/max_queued_events
16384
cat /proc/sys/fs/inotify/max_user_instances
128
cat /proc/sys/fs/inotify/max_user_watches
8192 vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576 sysctl -p

发起端需要安装 inotify-tools

用 inotify 机制还需要安装inotify-tools,以便提供 inotifywait、inotifywatch 辅助工具程序,用来监控、汇总改动情况。

inotifywait∶可监控modify(修改)、create(创建)、move(移动)、delete(删除)、attrib(属性更改)等各种事件,一有变动立即输出结果。

inotifywatch∶可用来收集文件系统变动情况,并在运行结束后输出汇总的变化情况

cd /opt/
tar xf inotify-tools-3.14.tar.gz -C /opt/ #wget  http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
cd /opt/inotify-tools-3.14/
./configure
make && make install

测试执行结果

inotifywait -mrq -e modify,create,move,delete /var/www/html
再开一个终端 [root@ansible-1 ~]# cd /var/www/html/
You have new mail in /var/spool/mail/root
[root@ansible-1 html]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 11 22:24 index1.html
[root@ansible-1 html]# touch a.txt
[root@ansible-1 html]# touch b.txt
[root@ansible-1 html]# mv a.txt /opt
[root@ansible-1 html]# echo "abcd" b.txt
abcd b.txt
You have new mail in /var/spool/mail/root
[root@ansible-1 html]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 15 01:39 b.txt
-rw-r--r-- 1 root root 0 Aug 11 22:24 index1.html

客户端脚本编写

[root@host103 opt]# vim /opt/inotify.sh
Path=/var/www/html/
backup_Server=192.168.211.30
inotifywait -mrq --format '%w%f' -e create,close_write,delete /var/www/html/ | while read line
do
if [ -f $line ];then
rsync -az $line --delete backuper@$backup_Server::wwwroot --password-file=/etc/server.pass
else
cd $Path &&\
rsync -az --delete backuper@$backup_Server::wwwroot --password-file=/etc/server.pass
fi
done
[root@host103 opt]# chmod +x /opt/inotify.sh
[root@host103 opt]# chmod 777 /var/www/html/
[root@host103 opt]# chmod +x /etc/rc.d/rc.local
[root@host103 opt]# echo '/opt/inotify.sh' >> /etc/rc.d/rc.local

创建文件进行测试如果同步成功则可以使用了

sync同步工具使用的相关教程结束。

《sync同步工具使用.doc》

下载本文的Word格式文档,以方便收藏与打印。