发现一款数据备份恢复神器!开源免费、快速高效、安全

开源
restic 是一个快速、高效和安全的备份程序。它支持三大操作系统(Linux、macOS、Windows)和一些较小的操作系统(FreeBSD、OpenBSD)。

[[405900]]

 简介

restic 是一个快速、高效和安全的备份程序。它支持三大操作系统(Linux、macOS、Windows)和一些较小的操作系统(FreeBSD、OpenBSD)。它使用 go 编程语言编写,使用 AES-256 对数据进行加密,并使用 Poly1305-AES 对数据进行身份验证。

github地址:https://github.com/restic/restic

设计原则

Restic 是一个可以正确进行备份的程序,其设计遵循以下原则:

  •  简单:备份应该是一个顺畅的过程,否则您可能会想跳过它。Restic 应该易于配置和使用,以便在数据丢失的情况下,您可以直接恢复它。同样,恢复数据不应该很复杂。
  •  快速:使用 restic 备份数据应仅受网络或硬盘带宽的限制,以便您可以每天备份文件。如果需要太多时间,没有人会进行备份。恢复备份应该只传输要恢复的文件所需的数据,这样这个过程也很快。
  •  可验证:比备份更重要的是恢复,所以 restic 使您可以轻松验证所有数据是否可以恢复。
  •   安全:Restic 使用加密技术来保证您数据的机密性和完整性。假设备份数据的存储位置不是受信任的环境(例如,系统管理员等其他人可以访问您的备份的共享空间)。Restic 旨在保护您的数据免受此类攻击者的侵害。
  •  高效:随着数据的增长,额外的快照应该只占用实际增量的存储。更重要的是,在将重复数据实际写入存储后端之前,应该对其进行去重,以节省宝贵的备份空间。

安装

CentOS 

  1. [root@centos7 ~]# yum install yum-plugin-copr -y  
  2. [root@centos7 ~]# yum copr enable copart/restic -y  
  3. Loaded plugins: copr, fastestmirror  
  4. copr done  
  5. [root@centos7 ~]# yum install restic -y 

如果上面的安装出现错误,请执行下面的命令解决源的问题 

  1. [root@centos7 ~]# yum-config-manager --add-repo https://copr.fedorainfracloud.org/coprs/copart/restic/repo/epel-7/copart-restic-epel-7.repo  
  2. Loaded plugins: fastestmirror  
  3. adding repo from: https://copr.fedorainfracloud.org/coprs/copart/restic/repo/epel-7/copart-restic-epel-7.repo  
  4. 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  
  5. repo saved to /etc/yum.repos.d/copart-restic-epel-7.repo 

macOS 

  1. # brew  
  2. $ brew install restic  
  3. # macprots  
  4. $ sudo port install restic 

更多安装方式请参考:https://restic.readthedocs.io/en/latest/020_installation.html#stable-releases

配置备份存储库

保存备份的位置称为“存储库”。存储库可以存储在本地,也可以存储在某个远程服务器或服务上。支持以下几种存储方式:

对于自动备份,restic 接受环境变量中的存储库位置RESTIC_REPOSITORY。Restic 还可以从通过--repository-file选项或环境变量指定的文件中读取存储库位置RESTIC_REPOSITORY_FILE。

对于密码,有几个选项:

  •  设置环境变量 RESTIC_PASSWORD
  •  通过选项--password-file或环境变量指定带有密码的文件路径 RESTIC_PASSWORD_FILE
  •  通过选项--password-command或环境变量 配置需要密码时调用的程序RESTIC_PASSWORD_COMMAND

创建本地存储库

以创建本地存储库为例 

  1. [root@centos7 ~]# restic init --repo /restic/backup_dir  
  2. enter password for new repository:   
  3. enter password again:  
  4. created restic repository dff64d39c6 at /restic/backup_dir  
  5. Please note that knowledge of your password is required to access  
  6. the repository. Losing your password means that your data is  
  7. irrecoverably lost.  
  8. #提示很明白,让你记住在此处输入的密码,丢掉密码就是丢掉了数据 

其它存储库创建方式请参考官方文档:https://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html

备份实操

备份目录data下的内容到存储库 

  1. [root@centos7 ~]# restic -r /restic/backup_dir --verbose backup ./data  
  2. open repository  
  3. enter password for repository:   
  4. repository dff64d39 opened successfully, password is correct  
  5. created new cache in /root/.cache/restic  
  6. lock repository  
  7. load index files  
  8. no parent snapshot found, will read all files  
  9. start scan on [./data]  
  10. start backup on [./data]  
  11. scan finished in 1.455s: 2922 files, 71.126 MiB  
  12. Files:        2922 new,     0 changed,     0 unmodified  
  13. Dirs:           99 new,     0 changed,     0 unmodified  
  14. Data Blobs:   2889 new  
  15. Tree Blobs:     99 new  
  16. Added to the repo: 72.083 MiB  
  17. processed 2922 files, 71.126 MiB in 0:05  #备份的文件数及大小  
  18. snapshot 4d20711e saved                   #创建了文件快照  
  19. --verbose #输出过程信息 

也可以备份单个文件 

  1. [root@centos7 ~]# ls ./data  
  2. goInception-linux-amd64-v1.2.3.tar.gz  httpd-2.4.6-95.el7.centos.x86_64.rpm  mingongge.z02  
  3. httpd-2.4.46                           mingongge.file                        mingongge.zip  
  4. httpd-2.4.46.tar.gz                    mingongge.z01  
  5. [root@centos7 ~]# restic -r /restic/backup_dir --verbose backup ./data/mingongge.zip  
  6. open repository  
  7. enter password for repository:   
  8. repository dff64d39 opened successfully, password is correct  
  9. lock repository  
  10. load index files  
  11. no parent snapshot found, will read all files  
  12. start scan on [./data/mingongge.zip]  
  13. start backup on [./data/mingongge.zip]  
  14. scan finished in 0.249s: 1 files, 942.793 KiB  
  15. Files:           1 new,     0 changed,     0 unmodified  
  16. Dirs:            1 new,     0 changed,     0 unmodified  
  17. Data Blobs:      0 new  
  18. Tree Blobs:      2 new  
  19. Added to the repo: 750 B  
  20. processed 1 files, 942.793 KiB in 0:00  
  21. snapshot 3e5b7dea saved 

如果你再次执行第一步的备份命令,会发现它不再增加内容,只是为当前的数据再增加一个快照。其实,restic它具有扫描文件的功能(逐个文件扫描比对),所以restic只会备份存储一次相同的数据。

文件检测功能

扫描每个文件的全部内容,非常浪费资源,所以 restic 还使用基于文件元数据的更改检测规则来确定文件自上次备份以来是否可能未更改,如果是,则不会再次扫描文件。

在Unix(包括 Linux 和 Mac)上,鉴于文件与先前备份中的文件位于同一位置,以下文件元数据属性必须匹配才能假定其内容未更改:

  •  修改时间戳(mtime)
  •  元数据更改时间戳 (ctime)
  •  文件大小
  •  inode 编号(用于引用文件系统中的文件的内部编号)

所以,基于上述的原因,引入一些参数,如下: 

  1. --force         #关闭更改检测,重新扫描全部文件  
  2. --ignore-ctime #要求 mtime 匹配,但允许 ctime 不同  
  3. --ignore-inode #要求 mtime 匹配,但允许 inode number 和 ctime 不同 

排除文件参数 

  1. --exclude   #指定一次或多次排除一个或多个项  
  2. --iexclude #与exclude相同,但忽略路径的情况  
  3. --exclude-caches #指定一次排除包含特殊文件的文件夹  
  4. --exclude-file   #指定一次排除包含特殊文件的文件夹  
  5. --iexclude-file   #与exclude-file相同,但忽略路径的情况  
  6. --exclude-if-present foo   #排除文件夹包含名为foo的文件  
  7. --exclude-larger-than size #指定一次以排除大于给定大小的文件 

更多相关的功能请参考:https://restic.readthedocs.io/en/latest/040_backup.html

存储库使用

既然数据备份到了存储库,所以,我们也需要去使用存储库,下面来介绍相关的操作。

列出存储库的所有快照

这个功能和平时在系统上使用ls命令相同,查看显示的功能 

  1. [root@centos7 ~]# restic -r /restic/backup_dir/ snapshots  
  2. enter password for repository:   
  3. repository dff64d39 opened successfully, password is correct  
  4. ID        Time                 Host        Tags        Paths  
  5. -------------------------------------------------------------------------------  
  6. 4d20711e  2021-06-04 03:40:47  centos7                 /root/data  
  7. 3e5b7dea  2021-06-04 03:46:34  centos7                 /root/data/mingongge.zip  
  8. 94c62288  2021-06-04 03:51:21  centos7                 /root/data  
  9. -------------------------------------------------------------------------------  
  10. 3 snapshots  
  11. #还可以使用下面的参数进行过滤匹配查看  
  12. --path="dir_name"  
  13. --host hostname  
  14. #通过相同的过滤器(主机、路径、标签)对输出进行分组  
  15. --group-by 

更多内容请参考:https://restic.readthedocs.io/en/latest/045_working_with_repos.html

检测存储库数据 

  1. [root@centos7 ~]# restic -r /restic/backup_dir/ check  
  2. using temporary cache in /tmp/restic-check-cache-294136679  
  3. enter password for repository:   
  4. repository dff64d39 opened successfully, password is correct  
  5. created new cache in /tmp/restic-check-cache-294136679  
  6. create exclusive lock for repository  
  7. load indexes  
  8. check all packs  
  9. check snapshots, trees and blobs  
  10. [0:00] 100.00%  3 / 3 snapshots  
  11. no errors were found 

数据恢复

这个才是重点啊,恢复数据才是王炸。

创建模拟数据删除的环境 

  1. [root@centos7 ~]# cd data/  
  2. [root@centos7 data]# ll  
  3. total 33796  
  4. -rw-r--r--  1 root root 13034487 Aug 30  2020 goInception-linux-amd64-v1.2.3.tar.gz  
  5. drwxr-sr-x 11 root   40     4096 Dec 24 22:35 httpd-2.4.46  
  6. -rw-r--r--  1 root root  9363314 Aug  5  2020 httpd-2.4.46.tar.gz  
  7. -rw-r--r--  1 root root  2846172 Oct 14  2020 httpd-2.4.6-95.el7.centos.x86_64.rpm  
  8. -rw-r--r--  1 root root        0 Jan 16 11:32 mingongge.file  
  9. -rw-r--r--  1 root root  4194304 Jan 16 16:24 mingongge.z01  
  10. -rw-r--r--  1 root root  4194304 Jan 16 16:24 mingongge.z02  
  11. -rw-r--r--  1 root root   965420 Jan 16 16:24 mingongge.zip  
  12. [root@centos7 data]# rm -rf ./*  
  13. [root@centos7 data]# ll  
  14. total 0 

恢复数据

直接从快照恢复误删除的数据 

  1. [root@centos7 ~]# restic -r /restic/backup_dir/ restore 4d20711e --target /root/  
  2. enter password for repository:   
  3. repository dff64d39 opened successfully, password is correct  
  4. restoring <Snapshot 4d20711e of [/root/data] at 2021-06-04 03:40:47.878873654 -0400 EDT by root@centos7> to /root/  
  5. [root@centos7 ~]# ll /root/data/  
  6. total 33796  
  7. -rw-r--r--  1 root root 13034487 Aug 30  2020 goInception-linux-amd64-v1.2.3.tar.gz  
  8. drwxr-sr-x 11 root   40     4096 Dec 24 22:35 httpd-2.4.46  
  9. -rw-r--r--  1 root root  9363314 Aug  5  2020 httpd-2.4.46.tar.gz  
  10. -rw-r--r--  1 root root  2846172 Oct 14  2020 httpd-2.4.6-95.el7.centos.x86_64.rpm  
  11. -rw-r--r--  1 root root        0 Jan 16 11:32 mingongge.file  
  12. -rw-r--r--  1 root root  4194304 Jan 16 16:24 mingongge.z01  
  13. -rw-r--r--  1 root root  4194304 Jan 16 16:24 mingongge.z02  
  14. -rw-r--r--  1 root root   965420 Jan 16 16:24 mingongge.zip 

上面的这种恢复方法相当指定从某某快照恢复数据,有时候快照较多,你不知道如何快速从哪个快照恢复的话,可以使用 latest 参数,去从最后一次备份的快照中恢复数据。 

  1. [root@centos7 ~]# rm -rf /root/data/*  
  2. [root@centos7 ~]# ll /root/data/  
  3. total 0  
  4. [root@centos7 ~]# restic -r /restic/backup_dir/ restore latest --target /root/  
  5. enter password for repository:   
  6. repository dff64d39 opened successfully, password is correct  
  7. restoring <Snapshot 94c62288 of [/root/data] at 2021-06-04 03:51:21.232686491 -0400 EDT by root@centos7> to /root/  
  8. [root@centos7 ~]# ll /root/data/  
  9. total 33796  
  10. -rw-r--r--  1 root root 13034487 Aug 30  2020 goInception-linux-amd64-v1.2.3.tar.gz  
  11. drwxr-sr-x 11 root   40     4096 Dec 24 22:35 httpd-2.4.46  
  12. -rw-r--r--  1 root root  9363314 Aug  5  2020 httpd-2.4.46.tar.gz  
  13. -rw-r--r--  1 root root  2846172 Oct 14  2020 httpd-2.4.6-95.el7.centos.x86_64.rpm  
  14. -rw-r--r--  1 root root        0 Jan 16 11:32 mingongge.file  
  15. -rw-r--r--  1 root root  4194304 Jan 16 16:24 mingongge.z01  
  16. -rw-r--r--  1 root root  4194304 Jan 16 16:24 mingongge.z02  
  17. -rw-r--r--  1 root root   965420 Jan 16 16:24 mingongge.zip 

其它管理介绍

删除快照

这里的快照和备份的文件名义是相同的,快照多了,可能就需要定期删除,一般对于数据的备份时间要求基本上30天是一个周期,除了特别重要的数据可能更久一点,这个根据实际情况定。 

  1. [root@centos7 ~]# restic -r /restic/backup_dir/ snapshots  
  2. enter password for repository:  
  3. repository dff64d39 opened successfully, password is correct  
  4. ID        Time                 Host        Tags        Paths  
  5. -------------------------------------------------------------------------------  
  6. 4d20711e  2021-06-04 03:40:47  centos7                 /root/data  
  7. 3e5b7dea  2021-06-04 03:46:34  centos7                 /root/data/mingongge.zip  
  8. 94c62288  2021-06-04 03:51:21  centos7                 /root/data  
  9. -------------------------------------------------------------------------------  
  10. 3 snapshots  
  11. [root@centos7 ~]# restic -r /restic/backup_dir/ forget 4d20711e 
  12. enter password for repository:   
  13. repository dff64d39 opened successfully, password is correct  
  14. [0:00] 100.00%  1 / 1 files deleted  
  15. [root@centos7 ~]# restic -r /restic/backup_dir/ snapshots  
  16. enter password for repository:   
  17. repository dff64d39 opened successfully, password is correct  
  18. ID        Time                 Host        Tags        Paths  
  19. -------------------------------------------------------------------------------  
  20. 3e5b7dea  2021-06-04 03:46:34  centos7                 /root/data/mingongge.zip  
  21. 94c62288  2021-06-04 03:51:21  centos7                 /root/data  
  22. -------------------------------------------------------------------------------  
  23. 2 snapshots  
  24. #这个方式和恢复相同,都是指定快照ID 

这种方式的删除,只是将快照删除了,但是引用的文件仍然在存储库中,也就是说存储库的大小没有变化,需要使用 prune 参数来清除数据。 

  1. [root@centos7 ~]# restic -r /restic/backup_dir/ prune  
  2. enter password for repository:  
  3. repository dff64d39 opened successfully, password is correct  
  4. loading indexes...  
  5. loading all snapshots...  
  6. finding data that is still in use for 2 snapshots  
  7. [0:00] 100.00%  2 / 2 snapshots  
  8. searching used packs...  
  9. collecting packs for deletion and repacking  
  10. [0:00] 100.00%  19 / 19 packs processed  
  11. to repack:            0 blobs / 0 B  
  12. this removes          0 blobs / 0 B  
  13. to delete:            0 blobs / 0 B  
  14. total prune:          0 blobs / 0 B  
  15. remaining:         2990 blobs / 72.175 MiB  
  16. unused size after prune: 0 B (0.00% of remaining size)  
  17. done 

还可以使用下面的命令同时执行上面两步的操作,相当于合并操作 

  1. restic forget --keep-last 1 --prune  
  2. #--keep-last 1 不删除最后一次的快照 

更多关于快照删除请参考:https://restic.readthedocs.io/en/latest/060_forget.html

存储库的密码管理

我们可以使用key命令来为同一个存储库设置多个密码,还可以使用 add、list、remove、passwd 来管理密码。

  •  查看密码 
  1. [root@centos7 ~]# restic -r /restic/backup_dir/ key list  
  2. enter password for repository:   
  3. repository dff64d39 opened successfully, password is correct 
  4.  ID        User  Host     Created  
  5. ---------------------------------------------  
  6. *d216779f  root  centos7  2021-06-04 03:28:34  
  7. --------------------------------------------- 
  •  新增密码 
  1. [root@centos7 ~]# restic -r /restic/backup_dir/ key add  
  2. enter password for repository:   
  3. repository dff64d39 opened successfully, password is correct  
  4. enter new password:   
  5. enter password again:   
  6. saved new key as <Key of root@centos7, created on 2021-06-04 04:43:18.024358447 -0400 EDT m=+18.001857421>  
  7. [root@centos7 ~]# restic -r /restic/backup_dir/ key list  
  8. enter password for repository:   
  9. repository dff64d39 opened successfully, password is correct  
  10.  ID        User  Host     Created  
  11. ---------------------------------------------  
  12. *d216779f  root  centos7  2021-06-04 03:28:34  
  13.  33d0b428  root  centos7  2021-06-04 04:43:18  
  14. --------------------------------------------- 
  •  删除 
  1. [root@centos7 ~]# restic -r /restic/backup_dir/ key remove 33d0b428  
  2. enter password for repository:  
  3. repository dff64d39 opened successfully, password is correct  
  4. removed key 33d0b428cba5c62585f1190432e61d46b88b4a6418c693d09ec47db596eace1f  
  5. [root@centos7 ~]# restic -r /restic/backup_dir/ key list 
  6. enter password for repository:   
  7. repository dff64d39 opened successfully, password is correct  
  8.  ID        User  Host     Created  
  9. ---------------------------------------------  
  10. *d216779f  root  centos7  2021-06-04 03:28:34  
  11. --------------------------------------------- 
  •  更改密码 
  1. [root@centos7 ~]# restic -r /restic/backup_dir/ key passwd  
  2. enter password for repository:   
  3. repository dff64d39 opened successfully, password is correct  
  4. enter new password:   
  5. enter password again:   
  6. saved new key as <Key of root@centos7, created on 2021-06-04 04:51:13.658184739 -0400 EDT m=+27.022974479>  
  7. [root@centos7 ~]# restic -r /restic/backup_dir/ key list  
  8. enter password for repository:   
  9. repository dff64d39 opened successfully, password is correct  
  10.  ID        User  Host     Created  
  11. ---------------------------------------------  
  12. *a62168f6  root  centos7  2021-06-04 04:51:13  
  13. ---------------------------------------------  
  14. #密码已更新完成,ID变化上可以看出来 

常见问题

https://restic.readthedocs.io/en/latest/faq.html

更多相关的功能,及其操作,有兴趣的读者可以自行安装体验。民工哥我一圈体验下来,感觉这个restic 确实是一款非常强大、快速、安全高效的备份神器,关键的关键是开源免费,太香了,强裂推荐。 

 

责任编辑:庞桂玉 来源: 民工哥技术之路
相关推荐

2021-01-08 15:49:31

数据恢复软件软件技术

2020-05-26 14:15:09

开源网盘 免费

2023-09-06 08:19:53

2022-07-12 09:35:59

JSON可视化工具

2015-08-25 09:23:00

2020-08-28 10:40:13

PythonFaker数据

2015-08-07 09:44:36

开源数据中心

2023-06-08 08:46:37

Motrix下载工具

2021-08-25 11:10:41

GitHub命令Linux

2011-09-16 10:35:13

Android应用数独经典游戏

2023-09-13 13:32:04

数据库开源

2019-02-25 10:18:43

工具代码测试

2017-03-06 11:02:59

产品软件Power Desig

2022-04-14 11:09:17

开源项目搜索

2020-12-18 08:38:22

开源前端mitojs

2021-04-27 09:00:59

PythonAidLearning编程神器

2009-11-06 10:11:02

赛门铁克解决方案Linux支持

2020-04-24 13:03:10

工具开源黑科技

2022-10-09 10:11:30

Python爬虫神器

2022-01-13 11:15:36

磁盘镜像软件Linux
点赞
收藏

51CTO技术栈公众号