發(fā)現(xiàn)一款數(shù)據(jù)備份恢復(fù)神器!開源免費(fèi)、快速高效、安全
簡介
restic 是一個快速、高效和安全的備份程序。它支持三大操作系統(tǒng)(Linux、macOS、Windows)和一些較小的操作系統(tǒng)(FreeBSD、OpenBSD)。它使用 go 編程語言編寫,使用 AES-256 對數(shù)據(jù)進(jìn)行加密,并使用 Poly1305-AES 對數(shù)據(jù)進(jìn)行身份驗(yàn)證。
github地址:https://github.com/restic/restic
設(shè)計(jì)原則
Restic 是一個可以正確進(jìn)行備份的程序,其設(shè)計(jì)遵循以下原則:
- 簡單:備份應(yīng)該是一個順暢的過程,否則您可能會想跳過它。Restic 應(yīng)該易于配置和使用,以便在數(shù)據(jù)丟失的情況下,您可以直接恢復(fù)它。同樣,恢復(fù)數(shù)據(jù)不應(yīng)該很復(fù)雜。
- 快速:使用 restic 備份數(shù)據(jù)應(yīng)僅受網(wǎng)絡(luò)或硬盤帶寬的限制,以便您可以每天備份文件。如果需要太多時間,沒有人會進(jìn)行備份?;謴?fù)備份應(yīng)該只傳輸要恢復(fù)的文件所需的數(shù)據(jù),這樣這個過程也很快。
- 可驗(yàn)證:比備份更重要的是恢復(fù),所以 restic 使您可以輕松驗(yàn)證所有數(shù)據(jù)是否可以恢復(fù)。
- 安全:Restic 使用加密技術(shù)來保證您數(shù)據(jù)的機(jī)密性和完整性。假設(shè)備份數(shù)據(jù)的存儲位置不是受信任的環(huán)境(例如,系統(tǒng)管理員等其他人可以訪問您的備份的共享空間)。Restic 旨在保護(hù)您的數(shù)據(jù)免受此類攻擊者的侵害。
- 高效:隨著數(shù)據(jù)的增長,額外的快照應(yīng)該只占用實(shí)際增量的存儲。更重要的是,在將重復(fù)數(shù)據(jù)實(shí)際寫入存儲后端之前,應(yīng)該對其進(jìn)行去重,以節(jié)省寶貴的備份空間。
安裝
CentOS
- [root@centos7 ~]# yum install yum-plugin-copr -y
- [root@centos7 ~]# yum copr enable copart/restic -y
- Loaded plugins: copr, fastestmirror
- copr done
- [root@centos7 ~]# yum install restic -y
如果上面的安裝出現(xiàn)錯誤,請執(zhí)行下面的命令解決源的問題
- [root@centos7 ~]# yum-config-manager --add-repo https://copr.fedorainfracloud.org/coprs/copart/restic/repo/epel-7/copart-restic-epel-7.repo
- Loaded plugins: fastestmirror
- adding repo from: https://copr.fedorainfracloud.org/coprs/copart/restic/repo/epel-7/copart-restic-epel-7.repo
- grabbing file https://copr.fedorainfracloud.org/coprs/copart/restic/repo/epel-7/copart-restic-epel-7.repo to /etc/yum.repos.d/copart-restic-epel-7.repo
- repo saved to /etc/yum.repos.d/copart-restic-epel-7.repo
macOS
- # brew
- $ brew install restic
- # macprots
- $ sudo port install restic
更多安裝方式請參考:https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases
配置備份存儲庫
保存?zhèn)浞莸奈恢梅Q為“存儲庫”。存儲庫可以存儲在本地,也可以存儲在某個遠(yuǎn)程服務(wù)器或服務(wù)上。支持以下幾種存儲方式:
對于自動備份,restic 接受環(huán)境變量中的存儲庫位置RESTIC_REPOSITORY。Restic 還可以從通過--repository-file選項(xiàng)或環(huán)境變量指定的文件中讀取存儲庫位置RESTIC_REPOSITORY_FILE。
對于密碼,有幾個選項(xiàng):
- 設(shè)置環(huán)境變量 RESTIC_PASSWORD
- 通過選項(xiàng)--password-file或環(huán)境變量指定帶有密碼的文件路徑 RESTIC_PASSWORD_FILE
- 通過選項(xiàng)--password-command或環(huán)境變量 配置需要密碼時調(diào)用的程序RESTIC_PASSWORD_COMMAND
創(chuàng)建本地存儲庫
以創(chuàng)建本地存儲庫為例
- [root@centos7 ~]# restic init --repo /restic/backup_dir
- enter password for new repository:
- enter password again:
- created restic repository dff64d39c6 at /restic/backup_dir
- Please note that knowledge of your password is required to access
- the repository. Losing your password means that your data is
- irrecoverably lost.
- #提示很明白,讓你記住在此處輸入的密碼,丟掉密碼就是丟掉了數(shù)據(jù)
其它存儲庫創(chuàng)建方式請參考官方文檔:https://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html
備份實(shí)操
備份目錄data下的內(nèi)容到存儲庫
- [root@centos7 ~]# restic -r /restic/backup_dir --verbose backup ./data
- open repository
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- created new cache in /root/.cache/restic
- lock repository
- load index files
- no parent snapshot found, will read all files
- start scan on [./data]
- start backup on [./data]
- scan finished in 1.455s: 2922 files, 71.126 MiB
- Files: 2922 new, 0 changed, 0 unmodified
- Dirs: 99 new, 0 changed, 0 unmodified
- Data Blobs: 2889 new
- Tree Blobs: 99 new
- Added to the repo: 72.083 MiB
- processed 2922 files, 71.126 MiB in 0:05 #備份的文件數(shù)及大小
- snapshot 4d20711e saved #創(chuàng)建了文件快照
- --verbose #輸出過程信息
也可以備份單個文件
- [root@centos7 ~]# ls ./data
- goInception-linux-amd64-v1.2.3.tar.gz httpd-2.4.6-95.el7.centos.x86_64.rpm mingongge.z02
- httpd-2.4.46 mingongge.file mingongge.zip
- httpd-2.4.46.tar.gz mingongge.z01
- [root@centos7 ~]# restic -r /restic/backup_dir --verbose backup ./data/mingongge.zip
- open repository
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- lock repository
- load index files
- no parent snapshot found, will read all files
- start scan on [./data/mingongge.zip]
- start backup on [./data/mingongge.zip]
- scan finished in 0.249s: 1 files, 942.793 KiB
- Files: 1 new, 0 changed, 0 unmodified
- Dirs: 1 new, 0 changed, 0 unmodified
- Data Blobs: 0 new
- Tree Blobs: 2 new
- Added to the repo: 750 B
- processed 1 files, 942.793 KiB in 0:00
- snapshot 3e5b7dea saved
如果你再次執(zhí)行第一步的備份命令,會發(fā)現(xiàn)它不再增加內(nèi)容,只是為當(dāng)前的數(shù)據(jù)再增加一個快照。其實(shí),restic它具有掃描文件的功能(逐個文件掃描比對),所以restic只會備份存儲一次相同的數(shù)據(jù)。
文件檢測功能
掃描每個文件的全部內(nèi)容,非常浪費(fèi)資源,所以 restic 還使用基于文件元數(shù)據(jù)的更改檢測規(guī)則來確定文件自上次備份以來是否可能未更改,如果是,則不會再次掃描文件。
在Unix(包括 Linux 和 Mac)上,鑒于文件與先前備份中的文件位于同一位置,以下文件元數(shù)據(jù)屬性必須匹配才能假定其內(nèi)容未更改:
- 修改時間戳(mtime)
- 元數(shù)據(jù)更改時間戳 (ctime)
- 文件大小
- inode 編號(用于引用文件系統(tǒng)中的文件的內(nèi)部編號)
所以,基于上述的原因,引入一些參數(shù),如下:
- --force #關(guān)閉更改檢測,重新掃描全部文件
- --ignore-ctime #要求 mtime 匹配,但允許 ctime 不同
- --ignore-inode #要求 mtime 匹配,但允許 inode number 和 ctime 不同
排除文件參數(shù)
- --exclude #指定一次或多次排除一個或多個項(xiàng)
- --iexclude #與exclude相同,但忽略路徑的情況
- --exclude-caches #指定一次排除包含特殊文件的文件夾
- --exclude-file #指定一次排除包含特殊文件的文件夾
- --iexclude-file #與exclude-file相同,但忽略路徑的情況
- --exclude-if-present foo #排除文件夾包含名為foo的文件
- --exclude-larger-than size #指定一次以排除大于給定大小的文件
更多相關(guān)的功能請參考:https://restic.readthedocs.io/en/latest/040_backup.html
存儲庫使用
既然數(shù)據(jù)備份到了存儲庫,所以,我們也需要去使用存儲庫,下面來介紹相關(guān)的操作。
列出存儲庫的所有快照
這個功能和平時在系統(tǒng)上使用ls命令相同,查看顯示的功能
- [root@centos7 ~]# restic -r /restic/backup_dir/ snapshots
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- ID Time Host Tags Paths
- -------------------------------------------------------------------------------
- 4d20711e 2021-06-04 03:40:47 centos7 /root/data
- 3e5b7dea 2021-06-04 03:46:34 centos7 /root/data/mingongge.zip
- 94c62288 2021-06-04 03:51:21 centos7 /root/data
- -------------------------------------------------------------------------------
- 3 snapshots
- #還可以使用下面的參數(shù)進(jìn)行過濾匹配查看
- --path="dir_name"
- --host hostname
- #通過相同的過濾器(主機(jī)、路徑、標(biāo)簽)對輸出進(jìn)行分組
- --group-by
更多內(nèi)容請參考:https://restic.readthedocs.io/en/latest/045_working_with_repos.html
檢測存儲庫數(shù)據(jù)
- [root@centos7 ~]# restic -r /restic/backup_dir/ check
- using temporary cache in /tmp/restic-check-cache-294136679
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- created new cache in /tmp/restic-check-cache-294136679
- create exclusive lock for repository
- load indexes
- check all packs
- check snapshots, trees and blobs
- [0:00] 100.00% 3 / 3 snapshots
- no errors were found
數(shù)據(jù)恢復(fù)
這個才是重點(diǎn)啊,恢復(fù)數(shù)據(jù)才是王炸。
創(chuàng)建模擬數(shù)據(jù)刪除的環(huán)境
- [root@centos7 ~]# cd data/
- [root@centos7 data]# ll
- total 33796
- -rw-r--r-- 1 root root 13034487 Aug 30 2020 goInception-linux-amd64-v1.2.3.tar.gz
- drwxr-sr-x 11 root 40 4096 Dec 24 22:35 httpd-2.4.46
- -rw-r--r-- 1 root root 9363314 Aug 5 2020 httpd-2.4.46.tar.gz
- -rw-r--r-- 1 root root 2846172 Oct 14 2020 httpd-2.4.6-95.el7.centos.x86_64.rpm
- -rw-r--r-- 1 root root 0 Jan 16 11:32 mingongge.file
- -rw-r--r-- 1 root root 4194304 Jan 16 16:24 mingongge.z01
- -rw-r--r-- 1 root root 4194304 Jan 16 16:24 mingongge.z02
- -rw-r--r-- 1 root root 965420 Jan 16 16:24 mingongge.zip
- [root@centos7 data]# rm -rf ./*
- [root@centos7 data]# ll
- total 0
恢復(fù)數(shù)據(jù)
直接從快照恢復(fù)誤刪除的數(shù)據(jù)
- [root@centos7 ~]# restic -r /restic/backup_dir/ restore 4d20711e --target /root/
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- restoring <Snapshot 4d20711e of [/root/data] at 2021-06-04 03:40:47.878873654 -0400 EDT by root@centos7> to /root/
- [root@centos7 ~]# ll /root/data/
- total 33796
- -rw-r--r-- 1 root root 13034487 Aug 30 2020 goInception-linux-amd64-v1.2.3.tar.gz
- drwxr-sr-x 11 root 40 4096 Dec 24 22:35 httpd-2.4.46
- -rw-r--r-- 1 root root 9363314 Aug 5 2020 httpd-2.4.46.tar.gz
- -rw-r--r-- 1 root root 2846172 Oct 14 2020 httpd-2.4.6-95.el7.centos.x86_64.rpm
- -rw-r--r-- 1 root root 0 Jan 16 11:32 mingongge.file
- -rw-r--r-- 1 root root 4194304 Jan 16 16:24 mingongge.z01
- -rw-r--r-- 1 root root 4194304 Jan 16 16:24 mingongge.z02
- -rw-r--r-- 1 root root 965420 Jan 16 16:24 mingongge.zip
上面的這種恢復(fù)方法相當(dāng)指定從某某快照恢復(fù)數(shù)據(jù),有時候快照較多,你不知道如何快速從哪個快照恢復(fù)的話,可以使用 latest 參數(shù),去從最后一次備份的快照中恢復(fù)數(shù)據(jù)。
- [root@centos7 ~]# rm -rf /root/data/*
- [root@centos7 ~]# ll /root/data/
- total 0
- [root@centos7 ~]# restic -r /restic/backup_dir/ restore latest --target /root/
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- restoring <Snapshot 94c62288 of [/root/data] at 2021-06-04 03:51:21.232686491 -0400 EDT by root@centos7> to /root/
- [root@centos7 ~]# ll /root/data/
- total 33796
- -rw-r--r-- 1 root root 13034487 Aug 30 2020 goInception-linux-amd64-v1.2.3.tar.gz
- drwxr-sr-x 11 root 40 4096 Dec 24 22:35 httpd-2.4.46
- -rw-r--r-- 1 root root 9363314 Aug 5 2020 httpd-2.4.46.tar.gz
- -rw-r--r-- 1 root root 2846172 Oct 14 2020 httpd-2.4.6-95.el7.centos.x86_64.rpm
- -rw-r--r-- 1 root root 0 Jan 16 11:32 mingongge.file
- -rw-r--r-- 1 root root 4194304 Jan 16 16:24 mingongge.z01
- -rw-r--r-- 1 root root 4194304 Jan 16 16:24 mingongge.z02
- -rw-r--r-- 1 root root 965420 Jan 16 16:24 mingongge.zip
其它管理介紹
刪除快照
這里的快照和備份的文件名義是相同的,快照多了,可能就需要定期刪除,一般對于數(shù)據(jù)的備份時間要求基本上30天是一個周期,除了特別重要的數(shù)據(jù)可能更久一點(diǎn),這個根據(jù)實(shí)際情況定。
- [root@centos7 ~]# restic -r /restic/backup_dir/ snapshots
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- ID Time Host Tags Paths
- -------------------------------------------------------------------------------
- 4d20711e 2021-06-04 03:40:47 centos7 /root/data
- 3e5b7dea 2021-06-04 03:46:34 centos7 /root/data/mingongge.zip
- 94c62288 2021-06-04 03:51:21 centos7 /root/data
- -------------------------------------------------------------------------------
- 3 snapshots
- [root@centos7 ~]# restic -r /restic/backup_dir/ forget 4d20711e
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- [0:00] 100.00% 1 / 1 files deleted
- [root@centos7 ~]# restic -r /restic/backup_dir/ snapshots
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- ID Time Host Tags Paths
- -------------------------------------------------------------------------------
- 3e5b7dea 2021-06-04 03:46:34 centos7 /root/data/mingongge.zip
- 94c62288 2021-06-04 03:51:21 centos7 /root/data
- -------------------------------------------------------------------------------
- 2 snapshots
- #這個方式和恢復(fù)相同,都是指定快照ID
這種方式的刪除,只是將快照刪除了,但是引用的文件仍然在存儲庫中,也就是說存儲庫的大小沒有變化,需要使用 prune 參數(shù)來清除數(shù)據(jù)。
- [root@centos7 ~]# restic -r /restic/backup_dir/ prune
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- loading indexes...
- loading all snapshots...
- finding data that is still in use for 2 snapshots
- [0:00] 100.00% 2 / 2 snapshots
- searching used packs...
- collecting packs for deletion and repacking
- [0:00] 100.00% 19 / 19 packs processed
- to repack: 0 blobs / 0 B
- this removes 0 blobs / 0 B
- to delete: 0 blobs / 0 B
- total prune: 0 blobs / 0 B
- remaining: 2990 blobs / 72.175 MiB
- unused size after prune: 0 B (0.00% of remaining size)
- done
還可以使用下面的命令同時執(zhí)行上面兩步的操作,相當(dāng)于合并操作
- restic forget --keep-last 1 --prune
- #--keep-last 1 不刪除最后一次的快照
更多關(guān)于快照刪除請參考:https://restic.readthedocs.io/en/latest/060_forget.html
存儲庫的密碼管理
我們可以使用key命令來為同一個存儲庫設(shè)置多個密碼,還可以使用 add、list、remove、passwd 來管理密碼。
- 查看密碼
- [root@centos7 ~]# restic -r /restic/backup_dir/ key list
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- ID User Host Created
- ---------------------------------------------
- *d216779f root centos7 2021-06-04 03:28:34
- ---------------------------------------------
- 新增密碼
- [root@centos7 ~]# restic -r /restic/backup_dir/ key add
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- enter new password:
- enter password again:
- saved new key as <Key of root@centos7, created on 2021-06-04 04:43:18.024358447 -0400 EDT m=+18.001857421>
- [root@centos7 ~]# restic -r /restic/backup_dir/ key list
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- ID User Host Created
- ---------------------------------------------
- *d216779f root centos7 2021-06-04 03:28:34
- 33d0b428 root centos7 2021-06-04 04:43:18
- ---------------------------------------------
- 刪除
- [root@centos7 ~]# restic -r /restic/backup_dir/ key remove 33d0b428
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- removed key 33d0b428cba5c62585f1190432e61d46b88b4a6418c693d09ec47db596eace1f
- [root@centos7 ~]# restic -r /restic/backup_dir/ key list
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- ID User Host Created
- ---------------------------------------------
- *d216779f root centos7 2021-06-04 03:28:34
- ---------------------------------------------
- 更改密碼
- [root@centos7 ~]# restic -r /restic/backup_dir/ key passwd
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- enter new password:
- enter password again:
- saved new key as <Key of root@centos7, created on 2021-06-04 04:51:13.658184739 -0400 EDT m=+27.022974479>
- [root@centos7 ~]# restic -r /restic/backup_dir/ key list
- enter password for repository:
- repository dff64d39 opened successfully, password is correct
- ID User Host Created
- ---------------------------------------------
- *a62168f6 root centos7 2021-06-04 04:51:13
- ---------------------------------------------
- #密碼已更新完成,ID變化上可以看出來
常見問題
https://restic.readthedocs.io/en/latest/faq.html
更多相關(guān)的功能,及其操作,有興趣的讀者可以自行安裝體驗(yàn)。民工哥我一圈體驗(yàn)下來,感覺這個restic 確實(shí)是一款非常強(qiáng)大、快速、安全高效的備份神器,關(guān)鍵的關(guān)鍵是開源免費(fèi),太香了,強(qiáng)裂推薦。