Borg – 具有重複數據刪除、壓縮和加密功能的 Linux 備份工具

博格備份 (又名:Borg)是一個支持壓縮和認證加密的重複數據刪除備份程序。 Borg 的主要目標是提供一種高效且安全的方式來備份數據。 Borg 備份中使用的重複數據刪除技術使其適用於日常備份,因為僅存儲更改,並且經過身份驗證的加密技術使其適用於備份到不完全受信任的目標。

本文介紹了 Borg Backup 在 Ubuntu 16 和其他 Linux 版本中的安裝和使用。

Borg 備份的主要特點

節省空間的存儲

→重複數據刪除基於內容定義的分塊,用於減少存儲的字節數。 每個文件被分割成許多可變長度的塊,並且只有以前從未見過的塊被添加到存儲庫中。

→要進行重複數據刪除,會考慮同一存儲庫中的所有塊,無論它們來自不同的機器、以前的備份、相同的備份,甚至來自相同的單個文件。

→與其他重複數據刪除方法相比,borg 中使用的方法不依賴於

• 文件/目錄名稱保持不變:因此您可以在不終止重複數據刪除的情況下移動文件和目錄,即使在共享存儲庫的機器之間也是如此。
• 完整的文件或時間戳保持不變:如果一個大文件稍有變化,只需要存儲幾個新的塊——這對於虛擬機或原始磁盤來說非常有用。
• 文件內數據塊的絕對位置:內容可能會發生偏移,但仍會被重複數據刪除算法找到。

速度

• 性能關鍵代碼(分塊、壓縮、加密)在 C/Cython 中實現
• 文件/塊索引數據的本地緩存
• 快速檢測未修改的文件

數據加密

→所有數據均可使用 256 位 AES 加密進行保護,使用 HMAC-SHA256 驗證數據完整性和真實性。 數據在客戶端加密。

壓縮

→所有數據都可以通過lz4(超快、低壓縮)、zlib(中速、壓縮)或lzma(低速、高壓縮)壓縮。

異地備份

→Borg 可以將數據存儲在任何可通過 SSH 訪問的遠程主機上。 如果 Borg 安裝在遠程主機上,與使用網絡文件系統(sshfs、nfs 等)相比,可以獲得很大的性能提升。

備份可掛載為文件系統

→備份檔案可作為用戶空間文件系統掛載,以便於交互式備份檢查和恢復(例如,通過使用常規文件管理器)。

在多個平台上輕鬆安裝

→我們提供不需要安裝任何東西的單文件二進製文件 – 您可以在這些平台上運行它們:

• Linux
Mac OS X
• FreeBSD
• OpenBSD 和 NetBSD(尚無 xattrs/ACL 支持或二進製文件)
• Cygwin(不支持,還沒有二進製文件)
• Windows 10 的 Linux 子系統(不支持)

免費和開源軟件

• 安全性和功能性可以獨立審核
• 在 BSD(3 條款)許可下獲得許可,請參閱許可以獲得完整的許可

安裝博格

要在 Ubuntu 16 中安裝 Borg 備份,請在終端中執行以下命令。

# sudo apt-get install borgbackup borgbackup-doc

您還可以從以下位置下載 borg 二進製文件 github 並將其移動到 PATH 環境變量指向的位置。

# wget https://github.com/borgbackup/borg/releases/download/1.0.10/borg-linux64
# mv borg-linux64 /usr/local/bin/borg
# chmod u+x /usr/local/bin/borg
# chown root:root /usr/local/bin/borg

博格的用法

Borg 由許多命令組成。 每個命令都接受許多參數和選項。 以下部分將詳細描述每個命令。

博格初始化

初始化一個新的備份存儲庫並創建一個備份存檔。

# Local repository (default is to use encryption in repokey mode)
$ borg init /path/to/repo

# Local repository (no encryption)
$ borg init --encryption=none /path/to/repo

# Remote repository (accesses a remote borg via ssh)
$ borg init user@hostname:backup

# Remote repository (store the key your home dir)
$ borg init --encryption=keyfile user@hostname:backup

使用加密,因為它會在未經授權的用戶訪問備份存儲庫的情況下保護您。

博格創建

此命令創建一個 borg 備份存檔,其中包含在遞歸遍歷所有指定路徑時找到的所有文件。 當給出 ‘-‘ 作為路徑時,borg 將從標準輸入中讀取數據並從該數據在創建的存檔中創建一個文件 ‘stdin’。 對於已經存儲在其他存檔中的文件或文件的一部分,存檔幾乎不會佔用磁盤空間。

# Backup ~/Documents into an archive named "my-documents"
$ borg create /path/to/repo::my-documents ~/Documents

# same, but verbosely list all files as we process them
$ borg create -v --list /path/to/repo::my-documents ~/Documents

# Backup ~/Documents and ~/src but exclude pyc files
$ borg create /path/to/repo::my-files 
~/Documents                       
~/src                             
--exclude '*.pyc'

# Backup home directories excluding image thumbnails (i.e. only
# /home/*/.thumbnails is excluded, not /home/*/*/.thumbnails)
$ borg create /path/to/repo::my-files /home 
--exclude 're:^/home/[^/]+/.thumbnails/'

# Do the same using a shell-style pattern
$ borg create /path/to/repo::my-files /home 
--exclude 'sh:/home/*/.thumbnails'

# Backup the root filesystem into an archive named "root-YYYY-MM-DD"
# use zlib compression (good, but slow) - default is no compression
$ borg create -C zlib,6 /path/to/repo::root-{now:%Y-%m-%d} / --one-file-system

# Make a big effort in fine granular deduplication (big chunk management
# overhead, needs a lot of RAM and disk space, see formula in internals
# docs - same parameters as borg < 1.0 or attic):
$ borg create --chunker-params 10,23,16,4095 /path/to/repo::small /smallstuff

# Backup a raw device (must not be active/in use/mounted at that time)
$ dd if=/dev/sdx bs=10M | borg create /path/to/repo::my-sdx -

# No compression (default)
$ borg create /path/to/repo::arch ~

# Super fast, low compression
$ borg create --compression lz4 /path/to/repo::arch ~

# Less fast, higher compression (N = 0..9)
$ borg create --compression zlib,N /path/to/repo::arch ~

# Even slower, even higher compression (N = 0..9)
$ borg create --compression lzma,N /path/to/repo::arch ~

# Use short hostname, user name and current time in archive name
$ borg create /path/to/repo::{hostname}-{user}-{now} ~
# Similar, use the same datetime format as borg 1.1 will have as default
$ borg create /path/to/repo::{hostname}-{user}-{now:%Y-%m-%dT%H:%M:%S} ~
# As above, but add nanoseconds
$ borg create /path/to/repo::{hostname}-{user}-{now:%Y-%m-%dT%H:%M:%S.%f} ~

博格提取物

此命令從存檔中提取內容並寫入當前目錄。

# Extract entire archive
$ borg extract /path/to/repo::my-files

# Extract entire archive and list files while processing
$ borg extract -v --list /path/to/repo::my-files

# Extract the "src" directory
$ borg extract /path/to/repo::my-files home/USERNAME/src

# Extract the "src" directory but exclude object files
$ borg extract /path/to/repo::my-files home/USERNAME/src --exclude '*.o'

# Restore a raw device (must not be active/in use/mounted at that time)
$ borg extract --stdout /path/to/repo::my-sdx | dd of=/dev/sdx bs=10M

博格支票

check 命令驗證存儲庫和相應檔案的一致性。

# check for corrupt chunks / segments:
borg check -v --repository-only REPO
# repair the repo:
borg check -v --repository-only --repair REPO
# make sure everything is fixed:
borg check -v --repository-only REPO

博格重命名

此命令重命名存儲庫中的存檔。

$ borg create /path/to/repo::archivename ~
$ borg list /path/to/repo
archivename                          Mon, 2017-03-06 19:50:19

$ borg rename /path/to/repo::archivename newname
$ borg list /path/to/repo
newname                              Mon, 2017-03-06 19:52:19

博格列表

此命令列出存儲庫或存檔的內容。

$ borg list /path/to/repository
$ borg list /path/to/repo::root-2017-03-06
$ borg list /path/to/repo::archiveA --list-format="{mode} {user:6} {group:6} {size:8d} {isomtime} {path}{extra}{NEWLINE}"
# see what is changed between archives, based on file modification time, size and file path
$ borg list /path/to/repo::archiveA --list-format="{mtime:%s}{TAB}{size}{TAB}{path}{LF}" |sort -n > /tmp/list.archiveA
$ borg list /path/to/repo::archiveB --list-format="{mtime:%s}{TAB}{size}{TAB}{path}{LF}" |sort -n > /tmp/list.archiveB
$ diff -y /tmp/list.archiveA /tmp/list.archiveB

博格刪除

此命令從存儲庫或完整存儲庫中刪除存檔。 相應地回收磁盤空間。 如果您刪除整個存儲庫,它的本地緩存(如果有)也會被刪除。

# delete a single backup archive:
$ borg delete /path/to/repo::Monday

# delete the whole repository and the related local cache:
$ borg delete /path/to/repo
You requested to completely DELETE the repository *including* all archives it contains:
repo                                 Mon, 2017-03-06 19:26:54
root-2017-03-06                      Mon, 2017-03-06 19:50:29
newname                              Mon, 2017-03-06 19:52:19
Type 'YES' if you understand this and want to continue: YES

博格西梅

prune 命令通過刪除與任何指定保留選項不匹配的所有檔案來修剪存儲庫。 此命令通常由想要保留一定數量的歷史 borg 備份的自動備份腳本使用。

# Keep 7 end of day and 4 additional end of week archives.
# Do a dry-run without actually deleting anything.
$ borg prune -v --list --dry-run --keep-daily=7 --keep-weekly=4 /path/to/repo

# Same as above but only apply to archive names starting with the hostname
# of the machine followed by a "-" character:
$ borg prune -v --list --keep-daily=7 --keep-weekly=4 --prefix='{hostname}-' /path/to/repo

# Keep 7 end of day, 4 additional end of week archives,
# and an end of month archive for every month:
$ borg prune -v --list --keep-daily=7 --keep-weekly=4 --keep-monthly=-1 /path/to/repo

# Keep all backups in the last 10 days, 4 additional end of week archives,
# and an end of month archive for every month:
$ borg prune -v --list --keep-within=10d --keep-weekly=4 --keep-monthly=-1 /path/to/repo

博格信息

此命令顯示有關指定存檔的一些詳細信息。

$ borg info /path/to/repo::root-2017-03-06

博格山

此命令將存檔安裝為 FUSE 文件系統。 這對於瀏覽存檔或恢復單個文件非常有用。

$ borg mount /path/to/repo::root-2017-03-06 /tmp/mymountpoint
$ ls /tmp/mymountpoint

博格山

此命令卸載使用 borg mount 掛載的 FUSE 文件系統。

$ borg umount /tmp/mymountpoint

博格密鑰導出

如果使用存儲庫加密,則沒有密鑰就無法訪問存儲庫。 此命令允許備份此基本密鑰。

$ borg key export /path/to/repo /path/to/export

博格密鑰導入

此命令允許恢復先前使用導出命令備份的密鑰。

$ borg key import /path/to/repo /path/to/import

博格更改密碼

用於存儲庫加密的密鑰文件可選擇受密碼保護。 此命令可用於更改此密碼。

# Create a key file protected repository
$ borg init --encryption=keyfile -v /path/to/repo
Initializing repository at "/path/to/repo"
Enter new passphrase:
Enter same passphrase again:
Remember your passphrase. Your data will be inaccessible without it.
Key in "/root/.config/borg/keys/mnt_backup" created.
Keep this key safe. Your data will be inaccessible without it.
Synchronizing chunks cache...
Archives: 0, w/ cached Idx: 0, w/ outdated Idx: 0, w/o cached Idx: 0.
Done.

# Change key file passphrase
$ borg change-passphrase -v /path/to/repo
Enter passphrase for key /root/.config/borg/keys/mnt_backup:
Enter new passphrase:
Enter same passphrase again:
Remember your passphrase. Your data will be inaccessible without it.
Key updated

博格服務

此命令啟動存儲庫服務器進程。 此命令通常不手動使用。

# Allow an SSH keypair to only run borg, and only have access to /path/to/repo.
# Use key options to disable unneeded and potentially dangerous SSH functionality.
# This will help to secure an automated remote backup system.
$ cat ~/.ssh/authorized_keys
command="borg serve --restrict-to-path /path/to/repo",no-pty,no-agent-forwarding,no-port-forwarding,no-X11-forwarding,no-user-rc ssh-rsa AAAAB3[...]

博格升級

升級現有的 Borg 備份存儲庫。

$ borg upgrade --tam REPO

博格斷鎖

此命令會破壞存儲庫和緩存鎖定。 請小心使用,並且僅在沒有 borg 進程(在任何機器上)嘗試訪問緩存或存儲庫時使用。

$ borg break-lock /path/to/repo

結論

Borg 是用 Python 編寫的,與其他經典備份解決方案相比具有許多優勢。 重複數據刪除和加密是兩個功能。 大多數 Linux 發行版都已經在他們的官方存儲庫中了。 備份服務器上的配置很簡單,因為客戶端使用 SSH 連接到服務器。 因此,您可能只想設置一個 borg 用戶並稍微限制客戶端。