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"

相关文章
|
7月前
|
存储 安全 数据管理
服务器违规资源被删,数据定时备份OSS 云存储才是 “救命稻草”
在数字化时代,数据已成为企业与个人的核心资产。然而,服务器违规、硬件故障等问题频发,导致数据丢失、业务中断,甚至造成不可挽回的损失。为保障数据安全与业务连续性,定时备份至关重要。阿里云国际站OSS提供高效、可靠的云存储解决方案,支持自动定时备份,帮助用户轻松应对数据风险。本文详解OSS备份操作步骤与注意事项,助你为数据穿上“防护甲”,实现安全无忧存储。
|
10月前
|
弹性计算 NoSQL 数据库
阿里云服务器如何备份数据?
阿里云服务器数据备份有多种方法,用户可按需选择。主要方式包括:1)快照备份,创建云盘的时间点拷贝,支持定期备份与数据恢复;2)数据库备份DBS,适用于多种环境的数据库备份,涵盖本地及多云场景;3)云备份Cloud Backup,提供统一灾备平台,支持ECS整机、数据库、文件系统等全方位备份,保障数据安全。
|
11月前
|
关系型数据库 MySQL Linux
在Linux环境下备份Docker中的MySQL数据并传输到其他服务器以实现数据级别的容灾
以上就是在Linux环境下备份Docker中的MySQL数据并传输到其他服务器以实现数据级别的容灾的步骤。这个过程就像是一场接力赛,数据从MySQL数据库中接力棒一样传递到备份文件,再从备份文件传递到其他服务器,最后再传递回MySQL数据库。这样,即使在灾难发生时,我们也可以快速恢复数据,保证业务的正常运行。
509 28
|
12月前
|
存储 安全 Linux
CentOS 7.9系统备份:每日定期发送最新备份文件到另一台服务器。
注意,这个解决方案忽略了很多细节,例如错误处理和通知、备份版本控制、循环处理旧的备份文件等等。此外,你也应该尽量保持源服务器和目标服务器之间快速,稳定且安全的网络连接,并且目标服务器应该有足够的空间用于存放每天的备份文件。如果你需要更高级的备份解决方案,可能需要考虑一下使用专门的备份工具或者服务。
551 18
|
存储 Linux 网络安全
【VMware VCF】使用 SFTP 服务器备份 VCF 核心组件的配置文件
【10月更文挑战第6天】以下是使用 SFTP 服务器备份 VMware VCF 核心组件配置文件的步骤:首先,设置 SFTP 服务器并配置用户账号与权限;其次,确保 VCF 环境能与 SFTP 服务器建立网络连接,并检查防火墙规则;接着,识别核心组件配置文件的位置,并使用 SFTP 客户端工具(如 `lftp` 或 WinSCP)进行备份;最后,验证备份结果的完整性和正确性,并定期执行备份操作,设置备份文件保留策略以节省存储空间。
210 0
|
数据安全/隐私保护
服务器备份的常见方法包括完全备份、增量备份、差异备份和实时备份
服务器备份的常见方法包括完全备份、增量备份、差异备份和实时备份
1877 3
|
5月前
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
447 10
|
5月前
|
运维 安全 Ubuntu
阿里云渠道商:服务器操作系统怎么选?
阿里云提供丰富操作系统镜像,涵盖Windows与主流Linux发行版。选型需综合技术兼容性、运维成本、安全稳定等因素。推荐Alibaba Cloud Linux、Ubuntu等用于Web与容器场景,Windows Server支撑.NET应用。建议优先选用LTS版本并进行测试验证,通过标准化镜像管理提升部署效率与一致性。
|
5月前
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享
阿里云4核8G服务器价格因实例类型而异,经济型e实例约159元/月,计算型c9i约371元/月,按小时计费最低0.45元。实际购买享折扣,1年最高可省至1578元,附主流ECS实例及CPU型号参考。
602 8

热门文章

最新文章