rsync上传和下载

您所在的位置:网站首页 sync网站 rsync上传和下载

rsync上传和下载

2023-08-16 02:40| 来源: 网络整理| 查看: 265

一.rsync同步简介

一款快速增量备份工具

一款快速增量备份工具 Remote Sync,远程同步 支持本地复制,或者与其他SSH、rsync主机同步 官方网站:http://rsync.samba.org

应用场景

“推” :即由客户端推送数据至服务器,比如个人电脑上传数据至云盘。 “拉” :即由客户端拉取服务器上的数据,比如利用个人电脑下载云盘文件。

rsync 包括如下的一些特性

1. 能更新整个目录树和文件系统; 2. 有选择性的保持符号链链、硬链接、文件属性、权限、设备以及时间等; 3. 传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。 4. 能用rsh、ssh 或直接端口做为传输端口; 5. 支持匿名rsync 同步文件,是理想的镜像工具;

Rsync可以通过rsh或ssh使用,也能以daemon模式去运行,在以daemon方式运行时Rsync server会打开一个873端口,等待客户端去连接。 在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客机的rsync同步操作的服务器称为备份源。

二.配置rsync源服务器 2.1、rsync同步源简介

指备份操作的远程服务器,也称为备份源 在这里插入图片描述

2.2、如何配置rsync源

1、基本思路

修改rsyncd.conf配置文件、独立的密码文件 启用rsync的--daemon模式

2、应用示例

用户backuper,允许下行同步 操作的目录为/var/www/html

3、配置文件rsyncd.conf

语法类似于Samba配置 认证配置auth users、secrets file,不加则为匿名(nobody)

4、rsync账号文件

采用“用户名:密码”的记录格式,每行一个用户记录 独立的账号数据,不依赖于系统账号(安全)

5、如何启动和关闭rsync

启动rsync:rsync --daemon 关闭rsync:kill $(cat /var/run/rsyncd.pid) 2.3、使用rsync备份工具 rsync命令的用法 rsync [选项] 原始位置 目标位置 常用选项 -a:归档模式,递归并保留对象属性,等同于 -rlptgoD -v:显示同步过程的详细(verbose)信息 -z:在传输文件时进行压缩(compress) -H:保留硬链接文件 -A:保留ACL属性信息 --delete:删除目录位置有而原始位置没有的文件 --checksum:根据对象的校验和来决定是否跳过文件 配置源的两种表示方法 格式1:用户名@主机地址::共享模块名 格式2:rsync://用户名@主机地址/共享模块名 示例 rsync -avz [email protected]::wwwroot /root rsync -avz rsync://[email protected]/wwwroot /root rsynx同步操作示例 下行rsync源:wwwroot共享--> /myweb rsync -avzH --delete [email protected]::wwwroot /myweb 然后输入密码,执行操作 2.4、rsync脚本的免交互处理 rsync源的免交互处理 使用 --password-file=密码文件 cat /etc/server.pass abc123 chmod 600 /etc/server.pass crontab -e 0 0 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /var/www/html systemctl restart crond systemctl enable crond 2.5、rsync实时同步 定期同步的不足 执行备份的时间固定,延迟明显、实时性差 当同步源长期不变化时,密集的定期任务是不必要的 实时同步的优点 一旦同步源出现变化,立即启动备份 只要同步源无变化,则不执行备份 2.6、关于inotify Linux内核的inotify机制 从版本2.6.13开始提供 可以监控文件系统的变动情况,并作出通知响应 辅助软件:inotify-tools 三、配置rsync远程同步 3.1、环境

在这里插入图片描述

3.2、步骤 1、rsync源站配置

1.确认rsync是否已经安装

[root@rsyncd ~]# rpm -q rsync rsync-3.0.9-18.el7.x86_64

2、修改配置文件

[root@rsync ~]# vim /etc/rsyncd.conf uid = nobody gid = nobody use chroot = yes #禁锢家目录 pid file = /var/run/rsyncd.pid address = 192.168.106.150 #提供同步服务的地址 port 873 log file = /var/log/rsyncd.log hosts allow = 192.168.106.0/24 #允许同步的网段 [wwwroot] #模块 path = /var/www/html #指定同步目录 comment = www.tat.cn #描述信息 read only = yes #描述权限 dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #这些结尾的文件同步时不再压缩 auth users = backuper #访问名单 secrets file = /etc/rsyncd_users.db #密码文件

3、创建backuper用户的密码文件

[root@rsync ~]# vim /etc/rsyncd_users.db backuper:abc123

4、服务端的密码文件要600权限

[root@Rsync ~]# chmod 600 /etc/rsyncd_users.db

5、启动服务并查看状态

[root@rsync ~]# rsync --daemon [root@rsync ~]# netstat -anpt | grep rsync tcp 0 0 192.168.106.150:873 0.0.0.0:* LISTEN 4023/rsync

6.安装httpd平且配置测试文件

[root@rsync ~]# yum install httpd -y [root@rsync html]# echo "web" > index.html [root@rsync html]# cd .. [root@rsync www]# chmod 777 html/ 2、客户端验证 [root@client ~]# yum install httpd -y [root@client html]# cd /var/www/ [root@client www]# chmod 777 html/

1、方法一:

[root@client www]# rsync -avz [email protected]::wwwroot /var/www/html/ Password: receiving incremental file list ./ index.html sent 83 bytes received 160 bytes 54.00 bytes/sec total size is 4 speedup is 0.02 [root@client www]# cd html [root@client html]# ls index.html

2、方法二

[root@client html]# rsync -avz rsync://[email protected]/wwwroot /var/www/html/ Password: receiving incremental file list ./ index.html sent 83 bytes received 160 bytes 69.43 bytes/sec total size is 4 speedup is 0.02 [root@client html]# ls index.html 常用选项: -r:递归模式,包含目录及子目录中的所有文件 -l:对于符号链接文件仍然复制为符号链接文件 -v:显示同步过程的详细信息 -a:归档模式,保留文件的权限、属性等信息,等同于组合选项“-rlptgoD” -z:在传输文件时进行压缩 -p:保留文件的权限标记 -t:保留文件的时间标记 -g:保留文件的属组标记(仅超级用户使用) -o:保留文件的属主标记(仅超级用户使用) -H:保留硬连接文件 -A:保留ACL属性信息 -D:保留设备文件及其他特殊文件 免密方式同步文件:

要先在客户端本地创建密码文件/etc/server.pass

[root@client html]# vim /etc/server.pass abc123 [root@client html]# chmod 600 /etc/server.pass [root@client html]# rsync -avz --delete --password-file=/etc/server.pass [email protected]::wwwroot /var/www/html/ receiving incremental file list ./ index.html sent 83 bytes received 160 bytes 486.00 bytes/sec total size is 4 speedup is 0.02 [root@client html]# ls index.html [root@client html]#crontab -e 0 0 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /var/www/html [root@client html]#systemctl restart crond [root@client html]#systemctl enable crond 四、rsync实时同步 4.1、为什么要实时同步

1、定期同步的不足 执行备份的时间固定,延迟明显,实时性差 当同步源长期不变化时,密集的定期任务是不必要的 2、实时同步的优点 一旦同步源出现变化,立即启动备份 只要同步源无变化,则不执行备份

4.2、rsync+inotify实时同步

在这里插入图片描述

1、调整inotify内核参数

[root@client ~]# vi /etc/sysctl.conf fs.inotify.max_queued_events = 16384 ###监控事件队列大小 fs.inotify.max_user_instances = 1024 ###最多监控实例数 fs.inotify.max_user_watches = 1048576 ###每个实例最多监控文件数 [root@client ~]# sysctl -p fs.inotify.max_queued_events = 16384 fs.inotify.max_user_instances = 1024 fs.inotify.max_user_watches = 1048576

2、安装 inotify-tools辅助工具

[root@client html]# cd [root@client ~]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/ [root@client ~]# yum -y install gcc gcc-c++ make [root@client ~]# cd /opt/inotify-tools-3.14/ [root@client inotify-tools-3.14]# ./configure [root@client inotify-tools-3.14]# make && make install [root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html

3指令说明

inotifywait:用于持续监控,实时输出结果 inotifywatch:用于短期监控,任务完成后再出结果 -m:持续进行监控 -r:递归监控所有子对象 -q:简化输出信息 -e:指定要监控哪些事件类型 modify:修改;create:创建;move:移动;delete:删除

4.在开个服务客户端连接

[root@client ~]# cd /var/www/html/ [root@client html]# touch abc [root@client html]# rm -rf abc

5.原来的监控端能看到

/var/www/html/ CREATE abc /var/www/html/ DELETE abc

6、客户端上编写脚本,将inotify监控和rsync远程同步结合起来

[root@client ~]# vi inotify.sh #!/bin/bash INOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html" RSYNC="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ [email protected]::wwwroot/" $INOTIFY | while read DIRECTORY EVENT FILE #逐条读取监控记录 do if [ $(pgrep rsync | wc -l) -le 0 ];then $RSYNC fi done ###同步两边目录权限都为777 发起端 [root@client opt]#chmod 777 /var/www/html/ [root@client opt]# [root@client opt]# ls -l /var/www/ 总用量 0 drwxr-xr-x. 2 root root 6 8月 4 2017 cgi-bin drwxrwxrwx. 2 root root 57 11月 30 18:45 html 源端 [root@Rsync ~]# vi /etc/rsyncd.conf read only = no [root@rsync www]# kill -9 rsync [root@rsync www]# rm -rf /var/run/rsyncd.pid [root@rsync www]# rsync --daemon [root@rsync www]# netstat -anpt | grep rsync tcp 0 0 192.168.106.150:873 0.0.0.0:* LISTEN 5941/rsync

7、运行脚本,在客户端/var/www/html目录下创建文件,查看源端/var/www/html目录是否同步到 客户端:

[root@client opt]# ./inotify.sh [root@client ~]# cd /var/www/html [root@client html]# touch test.txt

源站:

[root@rsync html]# cd /var/www/html/ [root@rsync html]# ll 总用量 0 -rw-r--r--. 1 nobody nobody 0 11月 30 19:59 test.txt 106.150:873 0.0.0.0:* LISTEN 5941/rsync

7、运行脚本,在客户端/var/www/html目录下创建文件,查看源端/var/www/html目录是否同步到 客户端:

[root@client opt]# ./inotify.sh [root@client ~]# cd /var/www/html [root@client html]# touch test.txt

源站:

[root@rsync html]# cd /var/www/html/ [root@rsync html]# ll 总用量 0 -rw-r--r--. 1 nobody nobody 0 11月 30 19:59 test.txt


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3