5

使用 rsync 异地备份服务器数据

 3 years ago
source link: https://blog.fxcdev.com/archives/216.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用 rsync 异地备份服务器数据

Published on May 31, 2020 in Daily with 2 comments

折腾 rsync

如果有一天,你突然发现自己的服务出现了异常。登陆到服务器上后才发现数据库里已经空了,只剩下了一条记录,上面写着:

To recover your lost Database and avoid leaking it: Send us 0.045 Bitcoin (BTC) to our Bitcoin address 1McksxpysJGSG9a9zHvan5f8Y1nfpDbVYF and contact us by Email with your Server IP or Domain name and a Proof of Payment. Your Database is downloaded and backed up on our servers. Backups that we have right now: *. Any email without your server IP Address or Domain Name and a Proof of Payment together will be ignored. If we dont receive your payment in the next 10 Days, we will make your database public or use them otherwise.

恭喜你,你被勒索了。如果你平时没有备份数据的习惯,就跟你的数据说拜拜吧。更加不幸的是你的服务还有用户在使用,那你的项目可以宣告破产了。

为了保证自己的数据安全,不要让惨剧发生到自己身上,一定要经常备份服务器数据。但是对于数据,应该备份多少份?备份的文件又该存储在哪里,才能最大限度地提高恢复数据的可能性呢?

3-2-1 原则

什么是 3-2-1 原则?这个原则是指在进行文件备份的时候:

  • 3:存储 3 份完整的文件,一份原件加上两份拷贝
  • 2:将文件起码保持在两种不同的介质上
  • 1:将一份文件保存在异地

分享一下我是如何有效践行 3-2-1 原则的:因为我的数据不仅仅在数据库中,还有一些以单独文件的形式存在,所以我每天会从服务器上增量备份最新的数据文件到本地 NAS,然后每周会从 NAS 备份数据到 OneDrive,这样数据有三个地方保存,如果真的被入侵,也可以快速恢复数据。

这些操作,如果纯靠人工来做,真的要烦死。还好有现成的工具可以用:rsync

什么是 rsync

rsync 是 Unix 下一款开源的程序,可以提供增量文件传输。常见的类 Unix 系统中都会自带 rsync,一般不需要额外安装。结合 Cron,就可以实现定时定期增量的文件备份。

配置 rsync

虽然 rsync 可以通过 ssh 的方式进行传输,但是为了安全性,还是使用 rsync 服务端和客户端配合的方式,这样即使某一端被攻陷了,也无法获取其他端的 ssh 密钥。

rsync 服务端

首先需要创建 rsync 服务的配置文件 rsyncd.conf

# 传输文件使用的用户和用户组,如果是从服务器=>客户端,要保证www用户对文件有读取的权限;如果是从客户端=>服务端,要保证www对文件有写权限。
uid = admin
gid = administrators
pid file = /var/run/rsyncd.pid
read only = false
hosts allow = *
port = 8733
SLimitRate = 
RLimitRate = 0
HBS3 AuthMode = RSYNC
status = None
max downrate = 10240

# 模块名,自己定义,可以在下方添加其它模块。须与客户端执行命令中的模块名一致。
[server]
path = /share/backup/server

还需要配置 rsync 的账号密码,创建 rsyncd.secrets 文件:

# 用户名:密码
user:pass

最后启动 rsync 服务端:

[root@50_125 rsync]# rsync --daemon --config=/etc/rsync/rsyncd.conf

rsync 客户端

比如我要将服务器 /opt 文件夹备份到 NAS 上,那么执行下面的命令就可以完成:

rsync -aqzrtopg --delete /opt rsync://user@nas-host/server --port=100 --password-file=/etc/rsync.pass

参数解释:

  1. -aqzrtopg 的意思是:

    • -a:归档模式,表示以递归方式传输文件,并保持所有文件属性
    • -q:精简输出模式
    • -z:对备份的文件在传输过程中进行压缩
    • -r:对子目录以递归模式处理
    • -t:保持文件事件信息
    • -o:保持文件属主信息
    • -p:保持文件的权限
    • -g:保持文件属组信息
  2. --delete: 删除 DST 中 SRC 没有的文件(DST 是 destination 的缩写:目的;SRC 是 source 的缩写:源)。

    说人话就是如果服务器上一个文件被删除了,那么在备份的时候,本地 NAS 也会将此文件删除,NAS 有文件快照,可以保留历史版本,不需要担心服务器被删后 NAS 的数据也丢失。

  3. /opt:想要备份的文件
  4. rsync://user@nas-host/server:rsync 的链接

    • user:rsync 的用户名
    • nas-host:NAS 的域名或者 ip
    • server:在 rsync 服务端配置的模块名
  5. --port:指定 rsync 的端口,如果你没有使用默认端口的话
  6. --password-file:指定密码文件

    你的 rsync 用户对应的密码,文件中只需要存储密码即可,文件名和位置都可以任意

只要执行命令的用户有备份文件的读取权限,并且命令参数正确,执行这行命令,就可以完成一次文件备份了。接下来看看怎么实现定时定期备份

使用 Cron 完成定时任务

编辑 /etc/crontab 文件:

0 1 * * * root rsync -aqzrtopg --delete /opt rsync://user@nas-host/server --port=100 --password-file=/etc/rsync.pass
  • 前面 5 位分别表示 min,hour,day,month,week,
  • root 表示以 root 用户执行后面的命令

这样就完成了每天的凌晨 1 点整,增量备份 /opt 文件到本地 NAS 的定时定期任务的配置。

本文由 TOner 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Jun 8, 2020 at 08:12 pm


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK