本指南解释了什么是 Restic,如何在各种 Linux 发行版中安装 Restic,最后如何在 Linux 操作系统中使用 Restic 备份和恢复数据。
Restic – 快速、安全、高效的备份应用程序
休息 是一个快速、开源、安全和跨平台的备份程序。 它允许将文件和目录的多个修订版本保存在存储在不同后端的加密存储库中。
Restic 使用密码学来保证数据的机密性和完整性。 它使用加密数据 AES-256 在计数器模式下并使用 Poly1305-AES 加密消息认证码.
使用 Restic 备份和恢复数据既简单又快速! 它只传输修改后的文件(增量备份),就像 同步.
使用 Restic,我们可以轻松地备份本地磁盘或远程系统或云存储中的文件和目录。 Restic 支持流行的云存储后端,例如 AWS、Azure、BackBlaze、Google Cloud、OpenStack 等等。
Restic 是使用编写的 去 编程语言及其源代码可在 GitHub 中免费获得。 它是跨平台的备份应用程序。 它支持 GNU/Linux、macOS、FreeBSD、openBSD 和 Microsoft Windows。
在 Linux 中安装 Restic
Restic 是为流行的 Linux 操作系统打包的。
在 Alpine Linux 中安装 Restic:
$ sudo apk add restic
在 Arch Linux、EndeavourOS、Manjaro Linux 中安装 Restic:
Restic 在 Arch Linux 的默认存储库中可用。 确保 [Community]
已启用存储库并在 Arch Linux 及其变体(如 EndeavourOS、Manjaro Linux)中安装 Restic 吃豆人 如下所示。
$ sudo pacman -S restic
从 AUR 安装 Restic:
Restic 也可用于 AUR. 因此,您可以使用任何 AUR 助手安装它,例如 帕鲁 或者 耶在基于 Arch 的系统中。
$ paru -S restic-git
或者,
$ yay -S restic-git
在 Debian、Ubuntu、Linux Mint、Pop_OS 中安装 Restic!:
$ sudo apt-get install restic
安装 Restic Fedora:
$ sudo dnf install restic
在 RHEL、CentOS、AlmaLinux 和 Rocky Linux 中安装 Restic:
$ sudo dnf copr enable copart/restic
$ sudo dnf install restic
在 Solus 中安装 Restic:
$ sudo eopkg install restic
在 Nix OS 中安装 Restic:
$ nix-env --install restic
在 openSUSE 中安装 Restic:
$ sudo zypper install restic
使用 Linuxbrew 安装 restic:
$ brew install restic
从源代码安装 Restic:
对于其他操作系统,您可以像下面这样编译和安装它。 首先,确保你已经安装 去 系统上的语言。
- 如何在 Linux 中安装 Go 语言
安装 Go 后, git clone restic github 存储库:
$ git clone https://github.com/restic/restic
此命令将克隆当前工作目录中 restic 存储库的所有内容。
cd 进入 restic 目录:
$ cd restic
并且,像下面这样安装 Restic:
$ go run build.go
检查安装的 Restic 版本:
$ restic version restic 0.12.0 compiled with go1.14.12 on linux/amd64
更新 Restic
如果您从官方存储库安装了 Restic,它可能有点旧版本。
要将 Restic 更新到最新版本,请运行:
$ sudo restic self-update
现在用命令检查它的版本:
$ restic version restic 0.12.1 compiled with go1.16.6 on linux/amd64
在 Linux 中使用 Restic 备份和恢复数据
Restic 支持以下后端存储备份:
- 本地目录,
- sftp 服务器(通过 SSH),
- HTTP REST 服务器,
- AWS S3,
- OpenStack 斯威夫特,
- BackBlaze B2,
- 微软 Azure Blob 存储,
- 谷歌 Cloud 贮存,
- 和许多其他服务通过 克隆.
现在,让我们看看如何在本地目录中备份和恢复数据。
创建新的本地存储库
首先,让我们创建一个存储库来存储备份。 为了 example,我将创建一个名为 我的备份 在我的 $HOME 目录中。
$ restic init --repo ~/mybackup
Enter 存储库的密码两次。 您必须记住密码才能稍后访问此存储库。 否则,您将永久丢失存储在存储库中的数据。 你被警告了!
enter password for new repository: enter password again: created restic repository c1c5bcfdb8 at /home/sk/mybackup Please note that knowledge of your password is required to access the repository. Losing your password means that your data is irrecoverably lost.
正如您在上面的输出中看到的,具有 ID 的本地静态存储库 c1c5bcfdb8
创建于 /home/sk/mybackup
地点。
现在是时候将数据备份到新创建的存储库了。
使用 Restic 将数据备份到本地目录
我要备份整个 ~/mydata
目录(包括目录本身)到存储库 ~/mybackup
使用命令:
$ restic -r ~/mybackup backup ~/mydata
Enter 您的存储库的密码:
样本输出:
enter password for repository: repository c1c5bcfd opened successfully, password is correct no parent snapshot found, will read all files Files: 7 new, 0 changed, 0 unmodified Dirs: 4 new, 0 changed, 0 unmodified Added to the repo: 21.919 MiB processed 7 files, 21.913 MiB in 0:00 snapshot 7457d80e saved
如您所见,restic 创建了给定目录的备份,即 我的数据. 此外,我使用唯一名称创建了当前备份的快照 7457d80e.
让我们分解上面的命令,看看每个选项的作用。
restic
: restic 命令-r
:表示存储库~/mybackup
:存储库名称backup
– 用于备份文件/文件夹的 Restic 子命令~/mydata
– 要备份的目录。
在备份目录中添加更多文件/文件夹。 例如,我添加了一个名为 vi editor.pdf
在 ~/mydata
目录。 现在,再次运行相同的 restic 命令:
$ restic -r ~/mybackup backup ~/mydata enter password for repository: repository c1c5bcfd opened successfully, password is correct using parent snapshot 7457d80e Files: 1 new, 0 changed, 7 unmodified Dirs: 0 new, 3 changed, 1 unmodified Added to the repo: 50.123 KiB processed 8 files, 21.957 MiB in 0:00 snapshot 963d4de3 saved

每次运行时,Restic 都会不断创建具有唯一名称的新快照。
由于 Restic 进行增量备份,因此后续备份将比以前的备份快得多。 我们可以继续在备份文件夹中添加数据并运行备份以创建许多快照。
不仅仅是目录,Restic 还允许我们备份单个文件。
$ restic -r ~/mybackup backup ~/mydata/vi editor.pdf
样本输出:
enter password for repository: repository c1c5bcfd opened successfully, password is correct no parent snapshot found, will read all files Files: 1 new, 0 changed, 0 unmodified Dirs: 3 new, 0 changed, 0 unmodified Added to the repo: 47.129 KiB processed 1 files, 45.139 KiB in 0:00 snapshot 59439d78 saved
如您所见,在存储库中创建了一个名为 59439d78 的新快照。
列出快照
要列出本地存储库中的可用快照,请运行:
$ restic -r ~/mybackup snapshots
样本输出:
enter password for repository: repository c1c5bcfd opened successfully, password is correct ID Time Host Tags Paths ---------------------------------------------------------------------- 7457d80e 2021-08-13 12:51:58 ostechnix /home/sk/mydata 963d4de3 2021-08-13 12:57:55 ostechnix /home/sk/mydata ---------------------------------------------------------------------- 2 snapshots

如您所见,我有2个快照,即 7457d80e 和 963d4de3.
检查两个快照之间的差异
要检查两个快照之间的差异,我们可以使用 差异 选项如下:
$ restic -r ~/mybackup diff 7457d80e 963d4de3
此命令比较给定的两个快照并列出差异,即添加了哪些文件,从存储库中删除了多少文件和目录。
enter password for repository: repository c1c5bcfd opened successfully, password is correct comparing snapshot 7457d80e to 963d4de3: + /home/sk/mydata/vi editor.pdf Files: 1 new, 0 removed, 0 changed Dirs: 0 new, 0 removed Others: 0 new, 0 removed Data Blobs: 1 new, 0 removed Tree Blobs: 4 new, 4 removed Added: 50.123 KiB Removed: 4.511 KiB

如您所见,我在备份中添加了一个新的 pdf 文件。
排除文件和文件夹
也可以排除某些文件或目录。 为了 example 以下命令将排除所有 .doc 类型文件:
$ restic -r ~/mybackup backup --exclude=*.doc ~/mydata
或者,您可以将要从备份中排除的所有文件和文件夹的实际位置放在一个文件中,并在备份命令中指定其路径。
为了 example创建一个名为 排除:
$ vi exclude.txt
添加要排除的文件或文件夹:
*.txt ostechnix.zip mydata/movies
现在,使用命令开始备份过程:
$ restic -r ~/backup backup --exclude-file=exclude.txt ~/mydata
有关更多详细信息 restic backup
命令,请运行:
$ restic help backup
现在,我们已经成功备份了我们的数据。 接下来我们将看到如何从本地备份中恢复数据。
使用 Restic 恢复数据
恢复数据很容易!
首先,使用命令列出存储库中的可用快照:
$ restic -r ~/mybackup snapshots
要从特定快照恢复数据,对于 example 7457d80e
, 跑:
$ restic -r ~/mybackup restore 7457d80e --target ~/mydata
样本输出:
enter password for repository: repository c1c5bcfd opened successfully, password is correct restoring <Snapshot 7457d80e of [/home/sk/mydata] at 2021-08-13 12:51:58.592771333 +0530 IST by [email protected]> to /home/sk/mydata

我们刚刚从快照中恢复了数据 7457d80e 到 〜/我的数据 目录。
要从快照中恢复单个文件,我们执行以下操作:
$ restic -r ~/mybackup restore 7457d80e --target ~/mydata file.json
有关更多详细信息,请查看 restore
命令帮助部分。
$ restic help restore
挂载仓库查看备份数据
您可以将备份作为常规文件系统进行浏览。 当您想要查看存储库的内容时,这很方便。
首先,创建一个挂载点:
$ mkdir ostechnix
然后,将您的存储库安装在 ostechnix 挂载点如下图所示。
$ restic -r ~/mybackup mount ostechnix/
此命令将在您的本地文件系统中挂载 Restic 存储库。
enter password for repository: repository c1c5bcfd opened successfully, password is correct Now serving the repository at ostechnix/ When finished, quit with Ctrl-c or umount the mountpoint.
现在,打开您的文件管理器,您将看到您的存储库已安装在那里。 转到快照文件夹并验证数据。

完成后,按 Ctrl-c
从文件管理器中退出或卸载挂载点。
有关更多详细信息,请查看帮助部分:
$ restic help mount
删除快照
首先,列出存储库中所有可用的快照:
$ restic -r ~/mybackup snapshots enter password for repository: repository c1c5bcfd opened successfully, password is correct ID Time Host Tags Paths ---------------------------------------------------------------------- 7457d80e 2021-08-13 12:51:58 ostechnix /home/sk/mydata 963d4de3 2021-08-13 12:57:55 ostechnix /home/sk/mydata ---------------------------------------------------------------------- 2 snapshots
要删除快照,对于 example 963d4de3, 跑:
$ restic -r ~/mybackup forget 963d4de3
样本输出:
enter password for repository: repository c1c5bcfd opened successfully, password is correct [0:00] 100.00% 1 / 1 files deleted
检查快照是否被删除:
$ restic -r ~/mybackup snapshots enter password for repository: repository c1c5bcfd opened successfully, password is correct ID Time Host Tags Paths ---------------------------------------------------------------------- 7457d80e 2021-08-13 12:51:58 ostechnix /home/sk/mydata ---------------------------------------------------------------------- 1 snapshots

截图不见了! 但是,此快照中的文件引用的数据仍存储在存储库中。
要清理未引用的数据,请运行:
$ restic -r ~/mybackup prune
样本输出:
enter password for repository: repository c1c5bcfd opened successfully, password is correct loading indexes... loading all snapshots... finding data that is still in use for 1 snapshots [0:00] 100.00% 1 / 1 snapshots searching used packs... collecting packs for deletion and repacking [0:00] 100.00% 8 / 8 packs processed to repack: 0 blobs / 0 B this removes 0 blobs / 0 B to delete: 5 blobs / 50.279 KiB total prune: 5 blobs / 50.279 KiB remaining: 25 blobs / 21.919 MiB unused size after prune: 0 B (0.00% of remaining size) rebuilding index [0:00] 100.00% 6 / 6 packs processed deleting obsolete index files [0:00] 100.00% 3 / 3 files deleted removing 2 old packs [0:00] 100.00% 2 / 2 files deleted done
删除所有快照和相关数据后,您可以删除存储库。
$ rm -fr ~/mybackup/
结论
您现在知道如何安装和使用 Restic 备份程序来保护您的重要数据。 我们只介绍了如何使用 Restic 将文件和文件夹备份到本地目录。 还有更多! 在我们即将发布的指南中,我们将了解如何将数据备份到远程系统和云存储。
我还建议您检查 Restic官方文档 了解更详细的用法。
资源:
相关阅读:
- 如何使用 Rsync 备份整个 Linux 系统
- 如何在 Linux 中使用 Rsync 备份文件和目录
- 如何在 Linux 中使用 Deja Dup 备份和恢复文件
- 如何使用 Rsnapshot 设置备份服务器
- 如何使用 Timeshift 备份和恢复 Linux 系统
- 如何在 Linux 上使用 Unison 同步文件
- 如何在 Linux 中同步本地和远程目录
- 如何使用 Zaloha.sh 同步文件和目录
- CYA – 适用于 Linux 的系统快照和还原实用程序