Rsync备份服务器

简介:

第一章、备份服务器介绍

rsync服务简介

Rsync同步原理:通过独特的quick check算法,根据文件大小、最后修改时间、权限、属主等属性的变化进行同步,

1、Rsync备份软件7大特性

①、支持拷贝普通文件与特殊文件,如连接文件,设备等

②、支持排除指定文件或目录同步的功能,类似tar命令排除功能

③、支持保持原文件或目录的所有属性信息不变

④、支持增量同步,提升数据传输效率

⑤、支持使用rcp、rsh、ssh等方式来配合进行隧道加密传输文件

⑥、支持使用socket(守护进程方式)传输文件或目录数据信息

⑦、支持用户任务方式传输数据,提升数据同步安全性。

2、Rsync命令参数:

a 归档模式,表示递归方式传输文件,并宝石所有文件属性 = rtopgDi

v 详细模块输出

z传输时进行压缩以提高效率

r 对子目录递归传输

t 保持文件时间属性信息

o 保持文件属主信息 g 保持文件属组信息 p 保持文件权限

D 保持设备文件信息 I (小写)保持软连接 P (大写)显示同步的过程及传输时的进度等

e 指定使用加密隧道传输,如ssh, (-e “ssh -p22”)

--exclude 排除不需要传输的文件 (排除单个)

--exclude-from 排除目录,可实现排除多个文件 (排除多个)

--bwlimit 限速传输,速率单位为kbytes(按照字节限速)

第二章、Rsync详解

1、服务特性:

是一款实现全量及增量、本地或远程数据同步备份的优秀工具

Rsync等价于cp、scp、rm、ls四种命令的集合

①、测试cp命令

[root@backup ~]# cp -a /etc/hosts /tmp

[root@backup ~]# ll /tmp/

-rw-r--r--. 1 root root 158 Jun 7 2013 hosts

[root@backup ~]# rsync -a /etc/hostname /tmp/

[root@backup ~]# ll /tmp

total 8

-rw-r--r-- 1 root root 7 Mar 10 13:12 hostname

-rw-r--r--. 1 root root 158 Jun 7 2013 hosts

②、测试scp命令

[root@backup ~]# scp -rp /tmp/ 10.14.21.2:/opt

root@10.14.21.2's password:

[root@backup ~]# rsync -rp /tmp/ 10.14.21.2:/opt #只同步目录下的内容

root@10.14.21.2's password:

[root@backup ~]# rsync -rp /tmp 10.14.21.2:/opt #同步目录及目录下的内容

root@10.14.21.2's password:

注意用rsync同步目录时,

/tmp 表示将目录及目录下面的内容进行同步

/tmp/ 表示将目录下面的内容进行同步,不包括目录本身

③、测试rm命令,

说明rsync实现删除目录中的数据时,原理是将一个空目录和目标目录进行同步

最终会将目录中的数据进行删除

[root@backup ~]# mkdir /null #创建空目录

[root@backup ~]# rsync -r --delete /null/ /tmp/ #以同步的方式实现删除

[root@backup ~]# ll /tmp

total 0

④、测试ls查看命令

[root@backup ~]# ll /etc/hosts

-rw-r--r-- 1 root root 182 Mar 22 19:12 /etc/hosts

[root@backup ~]# rsync /etc/hosts

-rw-r--r-- 182 2020/03/22 19:12:03 hosts

  • rsync工作方式

①、本地传输模式 = cp

[root@backup ~]# rsync -a /etc/hosts /tmp/123.txt #备份并重命名

[root@backup ~]# ll /tmp

total 4

-rw-r--r-- 1 root root 182 Mar 22 19:12 123.txt

②、远程数据传输模式 = scp

[root@backup ~]# rsync -a root@10.14.21.2:/etc/hostname /tmp/pull01.txt #拉取并重命名

root@10.14.21.2's password:

[root@backup ~]# ll /tmp/

total 8

-rw-r--r-- 1 root root 182 Mar 22 19:12 123.txt

-rw-r--r-- 1 root root 7 Mar 10 13:12 pull01.txt

[root@backup ~]# rsync -a 1.txt 10.14.21.2:/opt #推送过去

root@10.14.21.2's password:

③、守护进程模式:可以免交互进行备份

3、守护进程配置

1、服务端部署

[root@backup ~]# rpm -qa rsync #检查软件是否安装

rsync-3.1.2-6.el7_6.1.x86_64

[root@backup ~]# vim /etc/rsyncd.conf #cenos6默认没有配置文件,需要自己手动创建

#Author quss

#Time 2020/5/24

#Des rsync的配置文件

uid = rsync #向磁盘进行读写操作的 操作者

gid = rsync #向磁盘进行读写操作的 操作者

use chroot = no #安全相关参数,默认进行内网传输同步,可关闭

max connections = 200 #并发连接数

timeout = 300 #多长时间没有数据传输是,就释放连接,单位秒

pid file = /var/run/rsyncd.pid #运行时,会将进程pid存到一直指定pid文件

lock file = /var/run/rsync.lock #主要配合max connection,达到最大连接数则禁止访问

log file = /var/log/rsyncd.log #日志文件路径

ignore errors #传输时,忽略一些I/O产生的传输错误

read only = false #设置备份目录具有读写权限,即将只读模式关闭

list = false #是否将服务端配置的模块信息 在客户端查看(不安全)

hosts allow = 10.14.21.0/24 #白名单

#hosts deny = 0.0.0.0/32 # 黑名单

auth users = rsync_backup #备份目录的认证用户,虚拟定义的用户 不需要创建

secrets file = /etc/rsync.password #备份目录用户的密码文件

fake super = yes #直接使用rsync用户即可,不需要root就可以存储

#以上为全局配置,对所有模块都生效

[backup] #备份目录的模块名称

comment = "backup dir by cnnc" #注释说明

path = /backup #进行备份的目录信息

#read only = true #如不需要再传输时,可关闭写入的操作

[nfs]

comment = "backup dir by cnnc"

path = /nfs

[root@backup ~]# useradd rsync -M -s /sbin/nologin #创建备份目录管理用户,专门管理

[root@backup ~]# mkdir /backup #创建备份目录

[root@backup ~]# chown -R rsync.rsync /backup #授权

[root@backup ~]# echo "rsync_backup:123123" >>/etc/rsync.password #创建认证文件

[root@backup ~]# chmod 600 /etc/rsync.password #只有root及相应用户才能操作,安全

[root@backup ~]# rsync --daemon #启动rsync守护进程

[root@backup ~]##rsync --daemon --port 8730 #扩展参数,指定rsync端口,可忽略

[root@backup ~]## rsync --daemon --config=/etc/xx.conf #扩展参数,指定配置文件-可忽略

[root@backup ~]# ps -ef|grep rsync #检查进程

root 10828 1 0 10:21 ? 00:00:00 rsync --daemon

root 10830 2041 0 10:21 pts/0 00:00:00 grep --color=auto rsync

[root@backup ~]# netstat -lntup #检查端口

tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 10828/rsync

2、客户端部署

[root@test02 ~]# rpm -qa rsync #检查软件是否安装

rsync-3.1.2-6.el7_6.1.x86_64

[root@test02 ~]# echo "123123" >>/etc/rsync.password

#创建认证密码文件 只有密码信息,因为用户信息会在命令行中写入

[root@test02 ~]# chmod 600 /etc/rsync.password #授权,为了安全

3、传输测试

[root@backup ~]# ps -ef|grep rsync |grep -v grep | awk '{print $2}'|xargs kill -9 #杀死进程

[root@backup ~]# which rsync

/usr/bin/rsync

[root@backup ~]# /usr/bin/rsync --daemon #启动rsync

failed to create pid file /var/run/rsyncd.pid: File exists

[root@backup ~]# rm -rf /var/run/rsyncd.pid #如报错,则删除pid文件即可

[root@backup ~]# rsync --daemon

[root@test02 ~]# rsync -avz /etc/hosts rsync_backup@10.14.21.25::backup --password-file=/etc/rsync.password #免交互认证。且传输测试成功

sending incremental file list

hosts

sent 140 bytes received 43 bytes 366.00 bytes/sec

total size is 158 speedup is 0.86

[root@backup ~]# cd /backup/

[root@backup backup]# ll #注意数据属主、属组均为rsync

total 4

-rw-r--r-- 1 rsync rsync 158 Jun 7 2013 hosts

3、扩展应用

①、多模块配置,单模块不好分类管理

[root@backup ~]# vim /etc/rsyncd.conf #修改配置文件,增加多个备份 即分类目录

[backup]

comment = "backup backup by cnnc"

path = /backup

#read only = false

[backup02]

comment = "backup backup02 by cnnc"

path = /backup02

[root@backup ~]# mkdir /backup02 #创建备份目录

[root@backup ~]# chown -R rsync.rsync /backup02/ #授权

[root@backup ~]# ps -ef|grep rsync

root 2943 1 0 11:41 ? 00:00:00 rsync --daemon

root 7633 2470 0 12:24 pts/0 00:00:00 grep --color=auto rsync

[root@backup ~]# ps -ef|grep rsync |grep -v grep | awk '{print $2}'|xargs kill -9

[root@backup ~]# rsync --daemon #启动rsync

failed to create pid file /var/run/rsyncd.pid: File exists

[root@backup ~]# rm -rf /var/run/rsyncd.pid #删除pid文件

[root@backup ~]# rsync --daemon #启动rsync

[root@test02 ~]# rsync -avz /etc/hosts rsync_backup@10.14.21.25::backup02 --password-file=/etc/rsync.password #传输正常,且实现分类管理

sending incremental file list

hosts

sent 140 bytes received 43 bytes 366.00 bytes/sec

total size is 158 speedup is 0.86

②、排除功能

1、排除单个文件

[root@test02 ~]# mkdir /test_dir #创建测试文件夹

[root@test02 ~]# touch /test_dir/{a..d} #创建测试文件

[root@test02 ~]# ll /test_dir/

total 0

-rw-r--r-- 1 root root 0 May 24 12:30 a

-rw-r--r-- 1 root root 0 May 24 12:30 b

-rw-r--r-- 1 root root 0 May 24 12:30 c

-rw-r--r-- 1 root root 0 May 24 12:30 d

[root@test02 ~]# rsync -avz /test_dir/ --exclude=b --exclude=d rsync_backup@10.14.21.25::backup02 --password-file=/etc/rsync.password #排除b和d文件

sending incremental file list

./

a

c

sent 151 bytes received 65 bytes 144.00 bytes/sec

total size is 0 speedup is 0.00

[root@test02 ~]# rsync -avz /test_dir/ --exclude={b,d} rsync_backup@10.14.21.25::backup02 --password-file=/etc/rsync.password #这样写也可以,如排除文件太多则不好书写

sending incremental file list

sent 72 bytes received 20 bytes 184.00 bytes/sec

total size is 0 speedup is 0.00

2、排除多个文件

[root@test02 ~]# cd /test_dir/

[root@test02 test_dir]# vim excllude_file.txt #编写排除文件

b

d

#以上配置文件,必须是每个文件一行,且为相对路径

[root@test02 test_dir]# rsync -avz /test_dir/ --exclude-from=./exclude_file.txt rsync_backup@10.14.21.25::backup02 --password-file=/etc/rsync.password

sending incremental file list

./

a

c

exclude_file.txt #可看到连排除文件也传输了,可在排除文件中把自己也排除掉

sent 231 bytes received 90 bytes 642.00 bytes/sec

total size is 5 speedup is 0.02

[root@test02 test_dir]# vim excllude_file.txt #编写排除文件

b

d

excllude_file.txt

③、用守护进程创建备份目录: 用命令行创建较为方便

#备份到rsync下面的ops目录中,如果没有则创建,如果有ops目录则不变

[root@test02 test_dir]# rsync -avz /test_dir/ --exclude-from=./exclude_file.txt rsync_backup@10.14.21.25::backup02/ops/ --password-file=/etc/rsync.password

sending incremental file list

created directory ops

./

a

c

sent 231 bytes received 110 bytes 682.00 bytes/sec

total size is 5 speedup is 0.01

#备份到开发人员目录中

[root@test02 test_dir]# rsync -avz /test_dir/ --exclude-from=./exclude_file.txt rsync_backup@10.14.21.25::backup02/dev/ --password-file=/etc/rsync.password

sending incremental file list

created directory dev

./

a

c

sent 231 bytes received 110 bytes 682.00 bytes/sec

total size is 5 speedup is 0.01

[root@backup backup02]# tree # 验证是否备份

├── dev

│ ├── a

│ ├── c

│ └── exclude_file.txt

├── exclude_file.txt

├── hosts

└── ops

├── a

├── c

└── exclude_file.txt

2 directories, 8 files

④、访问控制管理

[root@backup ~]# vim /etc/rsyncd.conf

hosts allow = 10.14.21.0/24

#hosts deny = 0.0.0.0/32

#只有allow时, 兜底的默认规则是阻止

#只有deny时, 兜底的默认规则是允许

#既有allow又有deny时,兜底的默认规则是允许,尽量不选择这种规则。

#建议只选择allow即可。

⑤、无差异同步配置(重点)我有的你也有,我没有的你也不能有。

注:小心使用,否则可能不小心清空所有数据

[root@test02 test_dir]# rsync -avz /test_dir/ --delete rsync_backup@10.14.21.25::backup02/dev/ --password-file=/etc/rsync.password

sending incremental file list

b

d

sent 211 bytes received 62 bytes 546.00 bytes/sec

total size is 5 speedup is 0.02

[root@backup dev]# tree # 验证是否无差异同步

├── a

├── b

├── c

├── d

└── exclude_file.txt

0 directories, 5 files

⑥、列表功能配置:建议关闭

[root@backup ~]# vim /etc/rsyncd.conf

list = true #如果改为true,则可在客户端查看服务端 模块信息

[root@backup ~]# ps -ef|grep rsync |grep -v grep | awk '{print $2}'|xargs kill -9 #关闭进程

[root@backup ~]# rm -rf /var/run/rsyncd.pid #删除pid

[root@backup ~]# rsync --daemon #启动rsync

[root@test02 test_dir]# rsync rsync_backup@10.14.21.25:: # 都显示了会不安全,太透明

backup "backup backup by cnnc"

backup02 "backup backup02 by cnnc"

nfs "backup nfs by cnnc"

相关文章
|
3月前
|
运维 Linux Windows
【计算巢】幻兽帕鲁服务器如何设置定时备份存档
计算巢针对幻兽帕鲁服务器,提供给了定时备份存档的功能,会在设定的频率下,定时将存档文件备份到目标文件夹下,有助于解决存档丢失和坏档的问题。
3147 1
|
3月前
|
监控 测试技术 网络安全
基于阿里云计算巢部署的幻兽帕鲁服务器我该如何设置计划任务定时备份和重启,以及存档导入导出
基于阿里云计算巢部署的幻兽帕鲁服务器我该如何设置计划任务定时备份和重启,以及存档导入导出
|
5月前
|
SQL 弹性计算 运维
云备份(Cloud Backup)ECS备份基础版——超好用的ECS数据保护方案
ECS备份基础版是阿里云云备份(Cloud Backup,原混合云备份HBR)全新推出的ECS数据保护方案,配置简单、预算可控,同时支持ECS文件、自建数据库和整机的保护。ECS基础版属于预付费商品,可为每台ECS保护最多500G数据。
83674 0
|
2月前
|
存储 弹性计算 对象存储
ECS快照问题之备份ECS快照失败如何解决
阿里云ECS用户可以创建的一个虚拟机实例或硬盘的数据备份,用于数据恢复和克隆新实例;本合集将指导用户如何有效地创建和管理ECS快照,以及解决快照过程中可能遇到的问题,确保数据的安全性和可靠性。
|
23天前
|
SQL 存储 弹性计算
ECS备份问题之添加批量备份如何解决
ECS(Elastic Compute Service,弹性计算服务)是云计算服务提供商提供的一种基础云服务,允许用户在云端获取和配置虚拟服务器。以下是ECS服务使用中的一些常见问题及其解答的合集:
|
23天前
|
存储 域名解析 弹性计算
ECS备份问题之HBR混合云备份如何解决
ECS(Elastic Compute Service,弹性计算服务)是云计算服务提供商提供的一种基础云服务,允许用户在云端获取和配置虚拟服务器。以下是ECS服务使用中的一些常见问题及其解答的合集:
|
3月前
|
JSON NoSQL 网络安全
业务服务器免装插件,使用rsync+nxlog同步+采集应用日志并接入到GrayLog5.1
业务服务器免装插件,使用rsync+nxlog同步+采集应用日志并接入到GrayLog5.1
40 0
|
4月前
|
监控 网络安全 开发工具
Linxu服务器文件双向同步-rsync+sersync
Linxu服务器文件双向同步-rsync+sersync
53 0
|
4月前
|
存储 弹性计算 监控
ecs实例备份
ecs实例备份ecs实例备份
89 1
|
5月前
|
缓存 jenkins Java
3分钟教你linux服务器无损迁移备份Jenkins
3分钟教你linux服务器无损迁移备份Jenkins一台服务器到期,jenkins正好部署在这台服务器,这时候如果在新服务器重新安装然后配置jenkins的每个服务时间两天起步,于是考虑将原服务器jenkins无损迁移到新服务器上,时间只需3分钟,nice
148 0

热门文章

最新文章