rsync 远程备份的功能
rsync常用选项:
-v: 详细信息输出
-z: 传输时进行压缩 --compress-level=NUM可按级别压缩
-a: 归档模式传输并保持文件属性相当于-rtopgDL
-r : 递归模式
-t : 保持文件的时间属性
-o: 保持文件属主属性
-p: 保持文件权限属性
-g: 保持文件属组属性
-D: 保持设备文件信息
-l : 保持文件软链接
-e: 使用指定协议
–include=PATTERN: 指定排除不需要传输的文件
–exclude-from=file: 从文件中读取需要排除的内容
-bwlimit=KBPS: 限速限制I / O带宽; 每秒KBytes
–delete: 删除源目录中不存在的文件使目标目录和源目录一致,慎用
crontab 周期计划任务
* * * * *
分 时 日 月 周
准备两台虚拟机测试
192.168.27.137 备份源
192.168.27.138 备份端
关掉防火墙 setenforce
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
备份源操作
安装rsync
[root@localhost ~]# yum -y install rsync
修改配置文件
[root@localhost ~]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.27.0/24
[wwwroot]
path = /opt/aaa
comment = Document Root of www.51xit.top
read only =no
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users =tom
secrets file = /etc/rsyncd_users.db
[root@localhost ~]# mkdir /opt/aaa
设置账户 密码
[root@localhost ~]# vim /etc/rsyncd_users.db
tom:123
加权限
[root@localhost ~]# chmod 600 /etc/rsyncd_users.db
启动rsync 查看端口
root@localhost ~]# rsync --daemon
[root@localhost ~]# netstat -nlput |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 13360/rsync
tcp6 0 0 :::873 :::* LISTEN 13360/rsync
备份端操作
安装rsync
[root@localhost ~]# yum -y install rsync
备份端只需设置用户密码
[root@localhost ~]# vim /etc/server.pass
123
加权限
[root@localhost ~]# chmod 600 /etc/server.pass
设置定时任务 每分钟执行一次
[root@localhost ~]# crontab -e
* * * * * rsync -az --password-file=/etc/server.pass tom@192.168.27.137::wwwroot /root/
[root@localhost ~]# crontab -l
* * * * * rsync -az --password-file=/etc/server.pass tom@192.168.27.137::wwwroot /root/
测试
在备份端的创建个测试
[root@localhost aaa]# cd /opt/aaa/
[root@localhost aaa]# touch a
在备份源等1分钟查看 是否同步
[root@localhost ~]# ls
a