rsync的定义
rsync是什么呢,作为初学者,既然不了解的咱就搜一搜呗🔮🔮🔮
配置rsync源服务器
既然了解了是什么,那咱们就来配一配吧,看看咱到底配不配!
配置备份源的基本思路
配置文件-rsyncd.conf
rsync账号文件的作用
启用rsync服务
配置源的两种表示方法
备份工具-rsync
同步方式
增量备份
rsync命令
基本格式:
rsync [选项] 原始位置 目标位置
部署rsync远程同步
实验准备
主机 | ip地址 |
---|---|
rsync | 192.168.35.40 |
client | 192.168.35.10 |
下载rsync并启动
rpm -q rsync
yum -y install rsync ##安装rsync
vim /etc/rsyncd.conf ##修改/etc/rsyncd.conf配置文件
--------------->全局配置global<---------------------------
uid = nobody ##root
gid = nobody ##root
use chroot = yes ##禁锢在源目录;禁锢的对象:使用用户的权限/身份;禁锢的目录就是源目录;源目录就是共享模块下的path = /var/www/html(源目录会在每个共享模块下)
address = 192.168.35.40 ##监听地址
port 873 ##监听端口/tcp/udp 873, 可通过cat /etc/services | grep rsync查看
log file = /var/log/rsyncd.log ##日志文件位置
pid file = /var/run/rsyncd.pid ##存放进程ID的文件位置
hosts allow = 192.168.35.0/24 ##允许访问的客户机地址
--------------->共享模块<---------------------------
[ wwwroot ]
path = /var/www/html ##源目录的实际路径
comment = Document Root of www.1jm.com
read only = yes ##是否为只读
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z ##同步时不再压缩的文件类型,即rsync -avz
auth users = backuper ##授权账户,多个账号以空格分隔
secrets file = /etc/rsyncd_users.db ###存放账户信息的数据文件,即用户密码的地址
如采用匿名的方式,只要将其中的"auth users" 和"secrets file" 配置项去掉即可
vim /etc/rsyncd_users.db ##为备份账户创建数据文件,存放用户和用户密码
backuper: klj ##无需建立同名系统用户,
chmod 600 /etc/rsyncd_users.db ##保证所有用户对源目录/var/www/html 都有读的权限
mkdir -p /var/www/html
chmod +r /var/www/html/
ls -ld /var/www/html/
rsync --daemon ##启动rsync 服务程序
netstat -antp | grep rsync
测试远程同步
rsync -avz backuper@192.168.35.40::wwwroot /abc/ ##client进程远程共享
[root@client abc]# rm -rf 1.txt ##删除
[root@client abc]# ls
[root@client abc]# cd ..
[root@client /]# vim /etc/server.pass
[root@client /]# cat /etc/server.pass
klj
[root@client /]# chmod 600 /etc/server.pass
[root@client /]# rsync -avz backuper@192.168.35.40::wwwroot /abc/
Password:
receiving incremental file list
./
1.txt
sent 46 bytes received 113 bytes 63.60 bytes/sec
total size is 6 speedup is 0.04
[root@client /]# cd /abc
[root@client abc]# ls
1.txt
---------------------->rsync服务端<----------------------
[root@rsync ~]# cd /var/www/html
[root@rsync html]# ls
1.txt
[root@rsync html]# rm -rf 1.txt ##删除共享文件
[root@rsync html]# ls
[root@rsync html]#
client客户端操作共享发现仍存在1.txt
[root@client ~]# cd /abc
[root@client abc]# ls
1.txt
[root@client abc]# rsync -avz --password-file=/etc/server.pass backuper@192.168.35.40::wwwroot /abc
Password:
receiving incremental file list
./
sent 27 bytes received 47 bytes 29.60 bytes/sec
total size is 0 speedup is 0.00
[root@client abc]# ls
1.txt
我们想要实现的是同步应该是本地也删除1.txt,加上--delete即可实现
[root@client abc]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.35.40::wwwroot /abc
receiving incremental file list
deleting 1.txt/
sent 20 bytes received 40 bytes 40.00 bytes/sec
total size is 0 speedup is 0.00
rsync实时同步的不足
rsync结合inotify实时同步
inotify简介
/etc/sysctl.conf (内核参数配置文件) ##调整inotify内核参数(优化)
参数 | 含义 |
---|---|
inotifywait | 用于持续监控,实时输出结果 |
inotifywatch | 用于短期监控,任务完成后再输出结果 |
max_queue_events | 监控事件队列大小 |
max_user_instances | 最多监控实例数 |
max_user_watches | 每个实例最多监控文件数 |
持续监控并实时输出监控结果的命令-inotifywait
格式: inotifywait [参数]
常见参数 说明
-m 持续进行监控
-r 递归监控所有子对象
-q 简化输出信息
-e 指定要监控哪些事件类型
实验: rsync+inotify
vim /etc/rsyncd.conf
uid = root ##修改为root
gid = root ##修改为root
[wwwroot]
read only = no ##关闭只读,上行同步需要可写权限
kill 'cat /var/run/rsyncd.pid'
netstat -natp | grep rsync
rsync --daemon ##启动rsync
netstat -natp | grep rsync ##验证是否开启
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 32768 ##监控时间队列,默认为16384
fs.inotify.max_user_instances = 1024 ##最多监控实例数,默认为128
fs.inotify.max_user_watches = 1048576 ##每个实例最多监控文件数,默认为8192
当要监控的目录、文件数据量较多或者变化频繁时,建议加大参数值
sysctl -p ##刷新生效
yum -y install gcc gcc-c++
tar -zxvf inotify-tools-3.14.tar.gz -C /opt
cd /opt/inotify-tools-3.14
./configure
make&&make install