本指南介紹了什麼是 Restic,如何在各種 Linux 發行版上安裝 Restic,最後介紹瞭如何使用 Restic 在 Linux 操作系統上備份和恢復數據。
內容
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]
使用 Restic 在 Arch Linux 及其變體(EndeavourOS、Manjaro Linux 等)上啟用和安裝存儲庫 吃豆人 如下。
$ 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
單獨安裝 Restic。
$ sudo eopkg install restic
在 NixOS 上安裝 Restic。
$ nix-env --install restic
在 OpenSUSE 上安裝 Restic。
$ sudo zypper install restic
使用 Linuxbrew 安裝 restic。
$ brew install restic
從源代碼安裝 Restic。
對於其他操作系統,可以如下編譯安裝。首先,確保您已安裝它 走 系統上的語言。
- 如何在 Linux 上安裝 Go 語言
安裝 Go 後,Git 會克隆 resticgithub 存儲庫。
$ git clone https://github.com/restic/restic
此命令克隆當前工作目錄中 restic 存儲庫的所有內容。
Restic 目錄中的 CD:
$ 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 支持以下用於存儲備份的後端:
- 本地目錄,
- sftp 服務器(通過 SSH),
- HTTP REST 服務器,
- AWS S3,
- OpenStack 斯威夫特,
- BackBlaze B2,
- 微軟 Azure Blob 存儲,
- 谷歌云存儲,
- 和許多其他服務 克隆..
現在讓我們看看如何備份和恢復本地目錄中的數據。
創建一個新的本地存儲庫
首先,讓我們創建一個存儲庫來存儲備份。例如,創建以下存儲庫 我的備份 位於$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.
正如您在上面的輸出中看到的,具有 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 會備份指定目錄。 我的數據此外,使用唯一名稱創建當前備份的快照 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

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

從快照恢復數據 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.
現在,如果您打開文件管理器,您將看到存儲庫已安裝在那裡。轉到快照文件夾並檢查數據。

完成後,按 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 -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 文檔 更詳細的用法。
資源:
- 網站
- ResticGitHub 存儲庫
相關閱讀:
- 如何使用 Rsync 備份你的整個 Linux 系統
- 如何在 Linux 上使用 Rsync 備份文件和目錄
- 如何在 Linux 上使用 DejaDup 備份和恢復文件
- 如何使用 Rsnapshot 設置備份服務器
- 如何使用 timeshift 備份和恢復你的 Linux 系統
- 如何在 Linux 上使用 Unison 同步文件
- 如何在 Linux 上同步本地和遠程目錄
- 如何使用 Zaloha.sh 同步文件和目錄
- CYA-Linux 系統快照和恢復實用程序
Backup Linux Backup Tool Backup Utility CLI 命令行 文件備份 GoGo 編程語言 Linux Linux Management Linux Backup Linux Backup Tool Open Source Restic