愉快的使用restic备份数据

Restic 是一个免费的,快速,开源,安全和跨平台的备份程序,使用 go 编程语言编写,使用 AES-256 对数据进行加密,并使用 Poly1305-AES 对数据进行身份验证。

Restic 是一个快速且安全的数据文件备份程序。在下面的章节中,将介绍该工具的典型工作流程,从安装开始,准备一个新的资源备份数据,并进行第一次备份。

愉快的使用restic备份数据

0. 快速上手就够了

后续查看命令的常规使用的话,看博客的这个快速上手,基本就够了!

  • [1] 使用 Restic 工具的示例演示 - 备份
# 创建本地存储库来存储备份(初始化需输入两次密码)
1. 必须记住以后访问此存储库的密码,否则将永久丢失存储数据
$ restic --repo /data/backup init

1. 将数据备份到存储库(需输入密码)
1. 将备份/home/data文件夹到存储库/data/backup中
$ restic -r /data/backup backup /home/data

1. 备份单文件到存储库(需输入密码)
1. 这里使用exclude最好是从同步不经开始算起
$ restic -r /data/backup backup /home/data/file.txt
$ restic -r /data/backup backup --exclude=*.doc /home/data
$ restic -r /data/backup backup --exclude=/files/ /home/data
$ restic -r /data/backup backup --exclude-file=exclude.conf /home/data

1. 检查两个快照之间的差异(需输入密码)
$ restic -r /data/backup diff 6eda7c7d b52d462b

1. 列出存储库中的可用快照
$ restic -r /data/backup snapshots
  • [2] 使用 Restic 工具的示例演示 - 恢复
# 将数据从快照b52d462b恢复到/home/data目录(使用Restic恢复数据)
$ restic -r /data/backup restore b52d462b --target /home/data

1. 从快照还原单个文件到/home/data目录(使用Restic恢复数据)
$ restic -r /data/backup restore b52d462b --target /home/data file.txt
  • [3] 使用 Restic 工具的示例演示 - 查看
# 查看备份数据
1. 可以浏览备份作为一个常规文件系统(挂载)
$ mkdir data_list
$ restic -r /data/backup mount data_list
  • [4] 使用 Restic 工具的示例演示 - 删除
# 删除快照列表信息
$ restic -r /data/backup snapshots
$ restic -r /data/backup forget 6eda7c7d

1. 清除未引用的数据
1. 快照中文件引用的数据仍然存储在存储库中
$ restic -r /data/backup prune
  • [5] 使用 Restic 工具的示例演示 - 备份实例
# init
$ restic --repo sftp://[email protected]:22//data/restic_backup init

1. backup
1. 一定加上--no-lock=True不然会相互影响
$ restic --limit-upload=30000 --password-file=/data/passwd/restic_bj1_passwd 
    -r sftp://[email protected]:22//data/restic_backup --no-lock=True 
    backup /data/app/data_cache

1. forget
1. 保留的snapshot份数只能通过备份的任务来控制
$ restic --limit-upload=30000 --password-file=/data/passwd/restic_bj1_passwd 
    -r sftp://[email protected]:22//data/restic_backup --no-lock=True 
    forget --keep-last 72 --path /data/app/data_cache

1. 需要注意的问题
1. 备份时: --no-lock=True 避免多个仓库同时备份时,远程仓库锁住
1. 清除时: prune的时候会锁住远程备份仓库,导致测试备份的任务会挂掉
1. 所以清除的操作,最好是在远程备份仓库进行,防止清除时扫描仓库索引,导致命令很慢
$ restic -r /data/restic_backup prune

1. 工具的安装方式

该工具提供多种平台的对应安装包!

  • [1] macOS
# brew
$ brew install restic

1. macprots
$ sudo port install restic
  • [2] Debian
# apt
$ apt-get install restic
  • [3] CentOS
# yum
$ yum install yum-plugin-copr
$ yum copr enable copart/restic
$ yum install restic
  • [4] Docker
# docker
$ docker pull restic/restic
  • [5] 二进制安装
# clone
$ git clone https://github.com/restic/restic

1. run
$ cd restic && go run build.go

1. build
$ go run build.go --goos linux --goarch arm --goarm 6
  • [6] Official Binaries
$ restic version
restic 0.9.3 compiled with go1.11.2 on linux/amd64

$ restic self-update
find latest release of restic at GitHub
latest version is 0.9.4
download file SHA256SUMS
download SHA256SUMS
......
successfully updated restic to version 0.9.4

$ restic version
restic 0.9.4 compiled with go1.12.1 on linux/amd64

2. 命令行自动补全

执行如下命令可以使我们在使用 restic 时参数自动补全,提升使用效率!

  • 补全命令帮助提示
$ ./restic generate --help

The "generate" command writes automatically generated files (like the man pages
and the auto-completion files for bash and zsh).

Usage:
  restic generate [flags] [command]

Flags:
      --bash-completion file   write bash completion file
  -h, --help                   help for generate
      --man directory          write man pages to directory
      --zsh-completion file    write zsh completion file
  • 执行如下命令即可完成命令补全
# autocompletion
$ sudo ./restic generate --bash-completion /etc/bash_completion.d/restic
writing bash completion file to /etc/bash_completion.d/restic

3. 可配置的存储库

介绍 Restic 支持的数据存储类型!

安装好了 Restic 后,还需要配置下存储方式,也就是你想备份到本地还是远程服务器上。以下配置过程中都会要你输入密码,千万不能忘了,不然数据就找不回来了。 这里需要说的是,Rclone 这种方式,其可以挂载 OneDrive 或者 Google 网盘,具体的使用和配置情况可以参考官方文档进行操作。当然不仅限于我下面列举出来的这些,更多的存储库支持请查看官方文档。

  • [1] Local
    • 设置本地存储