Restic-快速,安全和高效的备份应用程序

时间:2020-03-21 11:46:50  来源:igfitidea点击:

Restic是使用Go编程语言编写的免费,快速,开源,安全且跨平台的备份程序。
Restic在计数器模式下使用AES-256加密数据,并使用Poly1305-AES加密消息身份验证代码对数据进行身份验证。
使用restic备份和还原数据确实非常快捷,容易。
在本教程中,我们将了解如何在Linux上使用Restic安装Restic以及如何备份和还原数据。

在Linux中安装Restic

在Arch Linux上,[社区]资源库中提供了Restic。
确保启用了社区存储库,并使用Pacman安装Restic,如下所示。

$sudo pacman -S restic

还可以在AUR中使用Restic。
因此,我们可以在基于Arch的系统中使用任何AUR帮助器(例如Yay)进行安装。

$yay -S restic-git

在Debian,Ubuntu和Linux Mint上:

$sudo apt-get install restic

在Nix操作系统上:

$nix-env --install restic

在openSUSE上:

$sudo zypper install restic

使用Linuxbrew:

$brew install restic

对于其他操作系统,我们可以像下面那样编译并安装它。
首先,请确保我们已在系统上安装Go语言。

  • 如何在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支持以下后端来存储备份:

  • 本地目录
  • sftp服务器(通过SSH)
  • HTTP REST服务器
  • AWS S3
  • OpenStack Swift
  • BackBlaze B2
  • Microsoft Azure Blob存储
  • 谷歌云存储

由于缺乏资源和时间,我仅介绍了如何在本地目录中备份和还原数据。
要了解其他备份方法,请单击上面给出的相应链接。

使用Restic备份本地目录中的数据

首先,让我们创建一个存储库来存储备份。
例如,我将在我的HOME目录中创建一个名为backup的存储库。

$restic init --repo ~/backup

输入存储库的密码两次。
我们必须记住密码,以后才能访问该存储库。
否则,我们将永久丢失存储在存储库中的数据。
你被警告了!

enter password for new repository: 
enter password again: 
created restic repository cbeb0c0585 at /home/sk/backup
Please note that knowledge of your password is required to access
the repository. Losing your password means that your data is
irrecoverably lost.

接下来,将数据备份到存储库:

$restic -r ~/backup backup ~/mydata

输入存储库的密码:

输出示例:

enter password for repository: 
password is correct
scan [/home/sk/mydata]
scanned 1 directories, 3 files in 0:00
[0:00] 100.00% 90.272 KiB/90.272 KiB 4/4 items 0 errors ETA 0:00 
duration: 0:00
snapshot 6eda7c7d saved

其中我将~/mydata文件夹备份到存储库~/backup中。

如我们所见,restic创建了给定目录的备份,例如:mydata。
此外,我还创建了唯一名称为6eda7c7d的当前备份的快照。

如果再次运行以上命令,restic将创建另一个具有唯一名称的快照,这一次它将比以前的备份快得多。
我们可以继续在文件夹中添加数据并运行备份以创建许多快照。

要检查两个快照之间的差异,我们可以使用diff选项,如下所示

$restic -r ~/backup diff 6eda7c7d b52d462b

输出示例:

enter password for repository: 
password is correct
comparing snapshot 6eda7c7d to b52d462b:
+ /mydata/Country road.mp3
Files: 1 new, 0 removed, 0 changed
Dirs: 0 new, 0 removed
Others: 0 new, 0 removed
Data Blobs: 2 new, 0 removed
Tree Blobs: 1 new, 1 removed
 Added: 2.991 MiB
 Removed: 1.127 KiB

如我们所见,我在备份中添加了一个新的mp3文件。

要列出存储库中的可用快照,请运行:

$restic -r ~/backup snapshots
enter password for repository: 
password is correct
ID Date Host Tags Directory
---------------------------------------------------------------------
6eda7c7d 2016-03-02 18:16:48 sk /home/sk/mydata
b52d462b 2016-03-02 18:21:11 sk /home/sk/mydata
---------------------------------------------------------------------
2 snapshots

如我们所见,我有2个快照,分别是6eda7c7d和b52d462b。

不仅是整个目录,restic还使我们能够备份单个文件。

$restic -r ~/backup backup ~/mydata/file.txt

也可以排除某些文件或者目录。
例如,以下命令将排除所有.doc类型的文件:

$restic -r ~/backup backup --exclude=*.doc ~/mydata

或者,我们可以将要从备份中排除的所有文件和文件夹的实际位置放在文件中,并在backup命令中指定其路径。

例如,创建一个名为exclude的文件:

$vi exclude

添加我们要排除的文件或者文件夹:

*.txt
theitroad.zip
mydata/movies

现在,使用以下命令启动备份过程:

$restic -r ~/backup backup --exclude-file=exclude ~/mydata

有关Restic备份的更多详细信息,请运行:

$restic help backup

现在,我们已经成功备份了数据。
接下来,我们将看到如何从本地备份还原数据。

使用Restic还原数据

恢复数据很容易!只需使用以下命令从快照还原数据:

$restic -r ~/backup restore b52d462b --target ~/mydata

输出示例:

enter password for repository: 
password is correct
restoring <Snapshot b52d462b of [/home/sk/mydata] at 2016-03-02 18:21:11.244087232 +0530 IST by Hyman@theitroad> to /home/sk/mydata

我们只是将数据从快照b52d462b恢复到~/mydata。

我们已经知道如何列出可用的快照了,对吧?
这是我们列出快照的方式。

$restic -r ~/backup snapshots

要从快照还原单个文件,我们执行以下操作:

$restic -r ~/backup restore b52d462b --target ~/mydata file.txt

有关更多详细信息,请查看“还原帮助”部分。

$restic help restore

查看备份数据

我们可能不希望还原数据,而只查看它们。
这很容易。
我们可以将备份作为常规文件系统浏览。
首先,创建一个挂载点:

$mkdir theitroad

然后,如下所示将存储库挂载到theitroad挂载点上。

$restic -r ~/backup mount theitroad/

现在,打开文件管理器,我们将看到存储库已安装。
我们可以将其作为本地文件系统的浏览方式。

有关更多详细信息,请查看帮助部分:

$restic help mount

删除快照

首先,列出存储库中所有可用的快照:

$restic -r ~/backup snapshots
enter password for repository: 
password is correct
ID Date Host Tags Directory
---------------------------------------------------------------------
6eda7c7d 2016-03-02 18:16:48 sk /home/sk/mydata
b52d462b 2016-03-02 18:21:11 sk /home/sk/mydata
---------------------------------------------------------------------
2 snapshots

要删除快照,例如6eda7c7d,请运行:

$restic -r ~/backup forget 6eda7c7d
enter password for repository: 
password is correct
removed snapshot 6eda7c7d

检查快照是否被删除:

$restic -r ~/backup snapshots
enter password for repository: 
password is correct
ID Date Host Tags Directory
---------------------------------------------------------------------
b52d462b 2016-03-02 18:21:11 sk /home/sk/mydata
---------------------------------------------------------------------
1 snapshots

快照不见了!但是,此快照中的文件引用的数据仍存储在资源库中。
要清除未引用的数据,请运行:

$restic -r ~/backup prune

输出示例:

enter password for repository: 
password is correct
counting files in repo
building new index for repo
[0:00] 100.00% 4/4 packs
repository contains 4 packs (9 blobs) with 3.081 MiB
processed 9 blobs: 0 duplicate blobs, 0B duplicate
load all snapshots
find data that is still in use for 1 snapshots
[0:00] 100.00% 1/1 snapshots
found 7 of 9 data blobs still in use, removing 2 blobs
will remove 0 invalid files
will delete 1 packs and rewrite 0 packs, this frees 1.555 KiB
counting files in repo
[0:00] 100.00% 3/3 packs
finding old index files
saved new indexes as [75bbcda0]
remove 2 old index files
[0:00] 100.00% 1/1 packs deleted
done

我们现在知道如何安装和使用Restic备份程序来保护重要数据。