restic + rclone 实现 vps 备份

每到考试周,就会忍不住折腾,所以今天随便谢谢 vps 备份方案

vps 备份主要是两种,一种是依靠服务器提供商自带的快照和备份;另一种就是自己折腾放到云盘上。

前者不一定所有厂商都有,而且不一定免费;后者更具有折腾性,但往往只能选择备份部分重要数据。

这里采用后者方法,具体使用 restic + rclone 来备份(rclone 主要是用于读写 Google Drive,而 restic 是备份工具)。

原因

我的备份需求:

  1. 备份分散在各处的配置文件:.vimrc, .bashrc, nginx.conf 等。
  2. 备份 ~/documents 以及 ~/ws

一个涉及分散文件的备份,一个涉及大型文件的备份。

如果单独采用 rclone,那么两个需求都不好实现:

  1. rclone 需要运行多次来满足第一个需求,不能从文件中读取所有需要备份的文件。
  2. rclone 本质是文件同步,尽管有增量功能和每个文件很小,但在有大量文件的情况下,表现极差(不到 30 M 文件需要 20 分钟,当然这是在没有配置多线程的情况下)。

而 restic 很好能解决这两个问题并且有额外好处:

  1. restic 能从文件中读取。
  2. restic 传送的 snapshot,每次传输的文件数很少,且 restic 会进行文件压缩,文件大小也会更小。
  3. restic 支持版本控制。

目标

  1. rclone 连接到 Google Drive
  2. restic 利用 rclone 创建 repository。
  3. 自动备份。

步骤

  1. 下载 rclone

    1
    curl https://rclone.org/install.sh | sudo bash
  2. 获取 Google Drive API

    参考

  3. rclone 配置

    rclone 配置文件处于 ~/.config/rclone/rclone.conf,不同用户不同配置,使用硬链接可以相同配置。

  4. 下载 restic

    1
    sudo apt install restic
  5. restic 初始化以及备份

    1
    2
    restic -r rclone:remote:backup init
    restic -r rclone:remote:backup backup -v --files-from backup_files
  6. 定时删除

    保留最后 3 天(每天一份),最后 5 周(每周一份)。

    1
    restic -r rclone:remote:backup forget --keep-daily 3 --keep-weekly 5 --prune
  7. 自动任务

    编辑脚本:

    1
    2
    3
    4
    5
    6
    7
    8
    #!/bin/bash

    export RESTIC_REPOSITORY="rclone:remote:backup"
    export RESTIC_PASSWORD="pass"

    restic backup -v --files-from /home/lllei/documents/backup_files

    restic forget --keep-daily 3 --keep-weekly 5 --prune
    • 使用 cron:

      1
      2
      su root
      crontab -e

      配置以下:

      1
      30 17 * * * /home/lllei/documents/backup.sh

      使用 cron 的坏处就是所有 cron 都会公用一个 log 文件,你可以通过:

      1
      2
      3
      sudo journalctl -u cron.service | grep backup.sh
      # 而 cron 产生的日志文件又是由 syslog 管理:
      sudo grep backup.sh /var/log/syslog
    • 使用 systemd timer

      参考

      1. 配置邮件

        ssmtp

        安装 ssmtp,配置 /etc/ssmtp/ssmtp.conf

        之后就可以通过 sendmail 命令发送邮件。

        配置邮件脚本 send-email.sh:

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        #!/bin/sh

        /usr/sbin/sendmail -t <<ERRMAIL
        To: $1
        From: systemd <root@$HOSTNAME>
        Subject: $2
        Content-Transfer-Encoding: 8bit
        Content-Type: text/plain; charset=UTF-8

        $(systemctl status --full "$2")
        ERRMAIL
      2. 配置邮件 service

        /etc/systemd/system/[email protected]:

        1
        2
        3
        4
        5
        6
        7
        [Unit]
        Description=status email for %i to user

        [Service]
        Type=oneshot
        User=root
        ExecStart=<your_send_email_script> <your_email> %i
      3. 配置 backup timer, service:

        etc/systemd/system/backup.timer:

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        [Unit]
        Description=Backup daily

        [Timer]
        OnCalendar=*-*-* 01:30:00 Asia/Shanghai
        # 默认为同名的 service,可以不用
        Unit=backup.service

        [Install]
        WantedBy=timers.target

        etc/systemd/system/backup.service:

        1
        2
        3
        4
        5
        6
        [Unit]
        Description=Backup daily service.
        OnFailure=status_email_user@%n.service

        [Service]
        ExecStart=/bin/bash /usr/bin/backup.sh

rclone

常用命令

注意被操作的是一个文件夹时,操作对象总是文件夹内的内容,而非文件夹本身。

rclone copy 不会删除文件,rclone sync 只对远端操作,会删除文件。

rclone 实现 Google Drive 挂载:

1
rclone mount remote: /google --allow-other --allow-non-empty --vfs-cache-mode writes --daemon

restic

  • restic 备份是基于路径的,这意味着它会将从根目录 / 到你需要备份的文件 /path/to/backup 路过的文件夹都会备份下来(和 linux 下的压缩有点像),当恢复时,你可以指定路径,例如 /tmp/restore,则会得到类似 /tmp/restore/home/username/... 之类的东西。而且 restic 也提供了挂载的方式来恢复。

  • restic 可以配置环境变量:RESTIC_REPOSITORY 以及 RESTIC_PASSWORD 来实现每次不用输入 -r repository 和密码。

  • remove files from snapshots

  • 查看所有快照:restic snapshots

  • 查看某个快照内容:restic ls <snapshot-id>,同样支持不完整输出 id。

  • 从快照中恢复:

    1
    2
    3
    4
    5
    6
    7
    # 从某个仓库恢复所有文件,恢复到指定路径:
    restic -r /srv/restic-repo restore 79766175 --target /tmp/restore-work

    # 可以用 latest 代替上面的 79766175,用 path 限制恢复的路径,用 host 限制快照来源。
    restic -r /srv/restic-repo restore latest --target /tmp/restore-work --path /home/username --host hostname

    # 可以利用 include 和 exclude 进一步限制恢复的文件
  • You can use the command restic ls latest or restic find foo to find the path to the file within the snapshot. This path you can then pass to –include in verbatim to only restore the single file or directory.

其他方案

borg: 作用和用法和 restic 相似,但 restic 对部分的云端有天然的支持。

duplicati: 似乎稳定性有点问题?而且是 .net 所写,linux 下运行需要 mono,所以排除。

duplicacy: 商用付费,个人免费。一个是文档感觉不规范,一个是似乎不支持从多个文件备份,不太符合我的需求,一个是 GitHub 上一堆 Issue 没有处理。但从官网来看,性能似乎是最好的一个(当然从备份角度来讲,性能不是最重要的)。另一个好处就是天然支持大部分云端,不用 rclone。

参考链接

rclone

restic

附录

由于 UESTC Office 365 A1 订阅没有管理员,不能挂载应用,获取不了 token。但 rclone 能通过 WebDAV 方式进行挂载,具体挂载链接为:https://[YOUR-DOMAIN]-my.sharepoint.com/personal/[YOUR-EMAIL]/Documents

Windows 下 rclone 配置文件在:%APPDATA%/rclone/rclone.conf

restic profile


restic + rclone 实现 vps 备份
https://lllei.top/2023/12/28/restic + rclone vps 备份/
作者
Lei
发布于
2023年12月28日
许可协议