本指南解釋了什麼是 Restic,如何在各種 Linux 發行版上安裝它,最後如何使用它在 Linux 操作系統上備份和恢復數據。

內容

  1. Restic – 快速、安全和高效的備份應用程序
  2. 在 Linux 上安裝 Restic
    1. 更新休息
  3. 在 Linux 上使用 Restic 備份和恢復數據
    1. 創建一個新的本地存儲庫
    2. 使用 Restic 將數據備份到本地目錄
    3. 列出快照
    4. 查看兩個快照之間的差異
    5. 排除文件和文件夾
    6. 使用 Restic 恢復數據
    7. 掛載倉庫查看備份數據
    8. 刪除快照
  4. 結論是

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、EndeavorOS 和 Manjaro Linux 上安裝 Restic。

Restic 在 Arch Linux 默認存儲庫中可用。一定 [Community] 使用 Restic 在 Arch Linux 及其變體(EndeavorOS、Manjaro Linux 等)上啟用和安裝存儲庫 吃豆人 如下。

$ sudo pacman -S restic

從 AUR 安裝 Restic。

Restic 也可用於 AUR。所以你可以像這樣使用 A​​UR 助手安裝它: 非常,在基於 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

單獨安裝 Restic。

$ sudo eopkg install restic

在你的 Nix 操作系統上安裝 Restic。

$ nix-env --install restic

在 openSUSE 上安裝 Restic。

$ sudo zypper install restic

使用 Linuxbrew 安裝 restic。

$ brew install restic

從源代碼安裝 Restic。

對於其他操作系統,可以編譯安裝如下:首先,確保你已經安裝 系統語言。

  • 如何在 Linux 上安裝 Go 語言

安裝 Go 後,git 克隆 restic github 存儲庫。

$ git clone https://github.com/restic/restic

此命令克隆當前工作目錄中 restic 存儲庫的所有內容。

轉到 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 更新到最新版本,請運行:

$ sudo restic self-update

使用以下命令檢查版本:

$ restic version
restic 0.12.1 compiled with go1.16.6 on linux/amd64

在 Linux 上使用 Restic 備份和恢復數據

Restic 支持以下用於存儲備份的後端:

  1. 本地目錄,
  2. sftp 服務器(通過 SSH),
  3. HTTP REST 服務器,
  4. AWS S3,
  5. OpenStack 斯威夫特,
  6. 背部火焰 B2,
  7. 微軟 Azure Blob 存儲,
  8. 谷歌云存儲,
  9. 和許多其他服務 克隆.

現在讓我們看看如何備份和恢復本地目錄中的數據。

創建一個新的本地存儲庫

首先,讓我們創建一個存儲庫來存儲我們的備份。例如,創建一個這樣的存儲庫: 我的備份 它在您的 $HOME 目錄中。

$ restic init --repo ~/mybackup

輸入您的存儲庫密碼兩次。您需要記住密碼才能稍後訪問此存儲庫。否則,存儲在存儲庫中的數據將永久丟失。你被警告了!

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.
使用 Restic 創建本地存儲庫

正如您在上面的輸出中看到的,具有 ID 的本地靜態存儲庫 c1c5bcfdb8 內置 /home/sk/mybackup 地方。

現在將您的數據備份到新創建的存儲庫。

使用 Restic 將數據備份到本地目錄

我想備份整個 ~/mydata 目錄(包括目錄本身)到存儲庫 ~/mybackup 使用命令:

$ restic -r ~/mybackup backup ~/mydata

輸入倉庫密碼:

示例輸出:

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 將目錄備份到本地存儲庫

如您所見,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 進行增量備份,因此後續備份比以前的備份快得多。您可以將數據添加到備份文件夾並運行備份以創建許多快照。

除了目錄,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
列出快照

如您所見,有兩個快照。 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 文件。

排除文件和文件夾

您還可以排除特定文件或目錄。例如,以下命令排除所有內容。 .doc 類型文件:

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

或者,您可以將要從備份中排除的所有文件和文件夾的實際位置放在一個文件中,並在備份命令中指定它們的路徑。

例如, 排除

$ 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

例如,從特定快照恢復數據 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
使用 Restic 恢復數據使用 Restic 恢復數據

從快照恢復數據 7457d80e 到達 〜/我的數據 目錄。

要從快照恢復單個文件,請執行以下操作:

$ restic -r ~/mybackup restore 7457d80e --target ~/mydata file.json

欲了解更多信息,請查看 restore 命令幫助部分。

$ restic help restore

掛載倉庫查看備份數據

您可以將備份作為常規文件系統進行瀏覽。如果您想查看存儲庫包含的內容,這很有用。

首先,創建一個掛載點。

$ mkdir 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.

現在打開您的文件管理器,您將看到存儲庫安裝在那裡。轉到快照文件夾並檢查數據。

在文件系統上掛載 Restic 存儲庫在文件系統上掛載 Restic 存儲庫

完成後,按 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

刪除快照。 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 中刪除快照在 Restic 中刪除快照

我的截圖不見了!但是,此快照中文件引用的數據仍存儲在存儲庫中。

要清理未引用的數據,請運行:

$ 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 Backup 來保護您的重要數據了。 我們只介紹瞭如何使用 Restic 將文件和文件夾備份到本地目錄。還有更多!未來的指南將介紹如何將數據備份到遠程系統和雲存儲。

我們還建議您檢查 Restic 官方文檔 詳細用法。

資源:

  • 網站
  • Restic GitHub 存儲庫

相關文件:

  • 如何使用 Rsync 備份你的整個 Linux 系統
  • 如何在 Linux 上使用 Rsync 備份文件和目錄
  • 如何在 Linux 上使用 Deja Dup 備份和恢復文件
  • 如何使用 Rsnapshot 設置備份服務器
  • 如何使用 Timeshift 備份和恢復你的 Linux 系統
  • 如何在 Linux 上使用 Unison 同步文件
  • 如何在 Linux 中同步本地和遠程目錄
  • 如何使用 Zaloha.sh 同步文件和目錄
  • CYA – Linux 系統快照和恢復實用程序

Backup Linux Backup Tool Backup Utility CLI 命令行 文件備份 GoGo 編程語言 LinuxLinux Admin Linux Backup Linux Backup Tool Open Source Restic