欢迎光临!
若无相欠,怎会相见

使用 Rsync 备份服务器数据

序言

又是月底了, 实际上, 如果不是前段时间及时止损, 或许这篇文章又是水文一篇

相亲内耗了两个月, 又修整了一段时间, 去年又因新冠修整了一段时间, 自己已经3个多月没有静下心好好学习了, 现在开始好好搞自己的事情, 好好挣钱, 感情就顺其自然吧

还是相亲过程中, 那段时间有点儿负能量爆棚, 就顺便使用 Memos 搭建了一个自托管的 flomo · 浮墨笔记, 刚好还有个安卓端, 自己就随时可以把一些想法什么的记录下来, 还有自己的碎碎念。

前几天使用的 pacificrack 连不上了, 把我给吓了一跳, 因为我的 memos 中已经记录了不少数据了, 我没有备份, 差点儿裂开, 还好后来恢复了, 赶紧找方案备份数据。

第一种使用了邮件方式备份, 但是遇到 mutt 一直报错的问题, 我一时半会儿无法解决, 就只能使用 rsync 同步数据到另一台稳定的机器上了, 还好 memos 使用的数据库是 SQLite, 整个数据目录就 3 个文件

配置服务端

服务端就是原始数据库文件所在的服务器。 我的服务器系统时 Ubuntu 20.04, 实际上已经有 rsync 软件了。

直接开始操作, 先复制模版配置文件到 /etc/rsyncd.conf

sudo cp /usr/share/doc/rsync/examples/rsyncd.conf /etc

如下是我设置的 rsync 参数

# sample rsyncd.conf configuration file

# GLOBAL OPTIONS

motd file=/etc/motd
log file=/var/log/rsyncd
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
# The init.d script does its own pid file handling,
# so omit the "pid file" line completely in that case.
pid file=/var/run/rsyncd.pid
syslog facility=daemon
#socket options=

# MODULE OPTIONS

[rsync]

        comment = public archive
        path = /root/memos
        use chroot = yes
#       max connections=10
        lock file = /var/lock/rsyncd
# the default for read only is yes...
        read only = yes
        list = yes
        uid = nobody
        gid = nogroup
#       exclude =
#       exclude from =
#       include =
#       include from =
        auth users = backup
        secrets file = /etc/rsyncd.pass
        strict modes = no
        hosts allow = xxx.xxx.xxx.xxx
#       hosts deny =
        ignore errors = no
        ignore nonreadable = yes
        transfer logging = no
#       log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
        timeout = 600
        refuse options = checksum dry-run
        dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz

其中, 为了安全, 我设置了 hosts allow 参数, 只允许自己的机器访问。 secrets file 是 /etc/rsyncd.pass 文件, 其内容格式如下, 密码按自己的喜好

backup:123456

然后使用命令启动服务端

rsync --daemon --config=/etc/rsyncd.conf

设置客户端

这里的客户端就是把数据同步到的机器

首先创建一个密码文件, 就是在服务端设置的密码, 如上文的 backup:123456, 把 123456 存到文件中。

123456

然后就可以使用 rsync 命令进行数据同步了

rsync -av --password-file=/etc/rsyncd.pass [email protected]::rsync /root/memos

其中 backup 就是你在服务端设置 auth users, @ 后的就是你的服务器地址, :: 后是配置段, 如上文中的配置是在 [rsync] 之后; 最后的 /root/memos 是同步后的数据路径

[root@host memos]# rsync -av --password-file=/etc/rsyncd.pass [email protected]::rsync /root/memos

receiving incremental file list
memos_prod.db-shm
memos_prod.db-wal

sent 5,790 bytes  received 13,881 bytes  5,620.29 bytes/sec
total size is 885,616  speedup is 45.02
[root@host memos]#

执行结果如上, 我已经使用 memos 记录了 800 KB + 的文字数据了

然后为了方便, 使用 crontab -e  将同步任务添加到定时执行计划中

*/10 * * * * rsync -av --password-file=/etc/rsyncd.pass [email protected]::rsync /root/memos

每十分钟执行一次数据同步, 我觉得可以了。

数据备份了, 以后可以随时迁移, 就算服务器灵车漂移了, 咱也没啥担心的了

结语

就先到此结束了, 也不是啥高深的东西, 权当记录一下吧, 不知不觉写了 40 分钟了, 天色已晚, 早些休息

如有错误,敬请指出,感谢指正!    — 2023-03-31  00:24:38

 

赞(0) 打赏
转载请注明:飘零博客 » 使用 Rsync 备份服务器数据
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

欢迎光临