目录
一:关于rsync
1.1:一款快速增量备份的工具
- Remote Sync,远程同步
- 支持本地复制,或者与其他SSH、rsync主机同步
- 官方网址:http://rsync.samba.org
1.2:rsync同步源
- 指备份操作的远程服务器,也称为备份源
1.3:配置rsync源
- 配置文件rsyncd.conf
- 需手动建立,语法类似于Samba
- 认证配置auth users、secrets file,不加则为匿名
- rsync帐号文件
- 采用“用户名:密码”的记录格式,每行一个用户记录
- 独立的账号数据,不依赖于系统账号
- 启用rsync服务
- 通过–daemon独自提供服务
1.4:rsync命令的用法
语法结构:
rsync [选项] 原始位置 目标位置
- 常用选项
- -a:归档模式,递归并保留对象属性,等同于 -rlptfoD
- -v:显示同步过程的详细(verbose)信息
- -z:在传输文件时进行压缩(compress)
- -H:保留硬连接文件
- -A:保留ACL属性信息
- –delete:删除目标位置有二原始位置没有的文件
- –checksum:根据对象的校验和来决定是否跳过文件
二:部署过程
2.1:实验需求
- 两台虚拟机:
服务器端IP:192.168.100.42
客户端IP:192.168.00.43
2.2:实验环境部署
- 安装httpd作为数据环境
[root@rsyncd ~]# yum install -y httpd
- 检查是否安装rsync服务
[root@rsyncd ~]# rpm -qa rsync
###没有进行安装
[root@rsyncd ~]# yum -y install rsync
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package rsync.x86_64 0:3.1.2-4.el7 will be installed
...省略内容
[root@rsyncd ~]# rpm -qa rsync
rsync-3.1.2-4.el7.x86_64
- 关闭防火墙和核心防护
[root@rsyncd ~]# systemctl stop firewalld
[root@rsyncd ~]# systemctl disable firewalld
[root@rsyncd ~]# setenforce ?
setenforce: SELinux is disabled
2.3:修改rsync等配置文件
- 对/etc/rsyncd.conf文件进行修改
[root@rsyncd ~]# vim /etc/rsyncd.conf
//下面是原有文件
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
# [ftp]
# path = /home/ftp
# comment = ftp export area
以下是修改过后的
uid = nobody
gid = nobody
use chroot = yes ###禁锢在源目录
address = 192.168.100.42 ###监听地址
port 873 ###监听端口号
log file = /var/log/rsyncd.log ###日志文件位置
pid file = /var/run/rsyncd.pid ###存放进程ID的位置
hosts allow = 192.168.100.0/24 ###允许客户端访问的地址段
[wwwroot] ###共享模块的名称
path = /var/www/html ###源目录的实际路径
comment = Document Root of www.abc.com
read only = yes ###设置为只读
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z ###同步时不再进行压缩的文件类型
auth users = backuper ###授权的账户
secrets file = /etc/rsyncd_users.db ###存放账户信息的文件
- 对/etc/rsyncd_users.db文件进行修改
[root@rsyncd ~]# vim /etc/rsyncd_users.db
backuper:123456
[root@rsyncd ~]# chmod 600 /etc/rsyncd_users.db ###给此文件600的权限
2.4:启动rsync服务
通过–daemon独自提供服务
如果要停止这个服务 kill $(cat /var/run/rsyncd.pid)
[root@rsyncd ~]# rsync --daemon
[root@rsyncd ~]# netstat -anpt |grep 873
tcp 0 0 192.168.100.42:873 0.0.0.0:* LISTEN 9851/rsync
二:实验测试
1.1:在/var/www.httml/下创建文件
[root@rsyncd ~]# cd /var/www/html/
[root@rsyncd html]#echo "abc" > 1.txt
[root@rsyncd html]# ls
-rw-r--r-- 1 root root 4 Oct 23 07:26 1.txt
1.2:在客户端进行测试
- 方法一:
[root@rsyncd ~]# rsync -avz backuper@192.168.100.42::wwwroot /opt/
Password:
receiving incremental file list
./
1.txt
sent 46 bytes received 111 bytes 1.90 bytes/sec
total size is 4 speedup is 0.03
[root@rsyncd ~]# cd /opt
[root@rsyncd opt]# ll
total 37204
-rw-r--r-- 1 root root 4 Oct 23 07:26 1.txt
[root@rsyncd opt]# cat 1.txt
abc ###同步成功
方法二: rsync -avz rsync://backuper@192.168.100.42/wwwroot/opt/
方法三:免交互
rsync -az --delete --password-file=/etc/server.pass backuper@192.168.100.42::wwwroot /opt