简介
restic 是一个快速、高效和安全的备份程序。它支持三大操作系统(Linux、macOS、Windows)和一些较小的操作系统(FreeBSD、OpenBSD)。它使用 go 编程语言编写,使用 AES-256 对数据进行加密,并使用 Poly1305-AES 对数据进行身份验证。
github地址:https://github.com/restic/restic
设计原则
Restic 是一个可以正确进行备份的程序,其设计遵循以下原则:
安装
CentOS
[root@centos7 ~]# yum install yum-plugin-copr -y[root@centos7 ~]# yum copr enable copart/restic -yLoaded plugins: copr, fastestmirrorcopr done[root@centos7 ~]# yum install restic -y
如果上面的安装出现错误,请执行下面的命令解决源的问题
[root@centos7 ~]# yum-config-manager --add-repo https://copr.fedorainfracloud.org/coprs/copart/restic/repo/epel-7/copart-restic-epel-7.repoLoaded plugins: fastestmirroradding repo from: https://copr.fedorainfracloud.org/coprs/copart/restic/repo/epel-7/copart-restic-epel-7.repograbbing 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.reporepo 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
配置备份存储库
保存备份的位置称为“存储库”。存储库可以存储在本地,也可以存储在某个远程服务器或服务上。支持以下几种存储方式:
对于自动备份,restic 接受环境变量中的存储库位置RESTIC_REPOSITORY。Restic 还可以从通过–repository-file选项或环境变量指定的文件中读取存储库位置RESTIC_REPOSITORY_FILE。
对于密码,有几个选项:
创建本地存储库
以创建本地存储库为例
[root@centos7 ~]# restic init --repo /restic/backup_direnter password for new repository: enter password again: created restic repository dff64d39c6 at /restic/backup_dirPlease note that knowledge of your password is required to accessthe repository. Losing your password means that your data isirrecoverably lost.#提示很明白,让你记住在此处输入的密码,丢掉密码就是丢掉了数据
其它存储库创建方式请参考官方文档:https://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html
备份实操
备份目录data下的内容到存储库
[root@centos7 ~]# restic -r /restic/backup_dir --verbose backup ./dataopen repositoryenter password for repository: repository dff64d39 opened successfully, password is correctcreated new cache in /root/.cache/resticlock repositoryload index filesno parent snapshot found, will read all filesstart scan on [./data]start backup on [./data]scan finished in 1.455s: 2922 files, 71.126 MiBFiles: 2922 new, 0 changed, 0 unmodifiedDirs: 99 new, 0 changed, 0 unmodifiedData Blobs: 2889 newTree Blobs: 99 newAdded to the repo: 72.083 MiBprocessed 2922 files, 71.126 MiB in 0:05 #备份的文件数及大小snapshot 4d20711e saved #创建了文件快照--verbose #输出过程信息
也可以备份单个文件
[root@centos7 ~]# ls ./datagoInception-linux-amd64-v1.2.3.tar.gz httpd-2.4.6-95.el7.centos.x86_64.rpm mingongge.z02httpd-2.4.46 mingongge.file mingongge.ziphttpd-2.4.46.tar.gz mingongge.z01[root@centos7 ~]# restic -r /restic/backup_dir --verbose backup ./data/mingongge.zipopen repositoryenter password for repository: repository dff64d39 opened successfully, password is correctlock repositoryload index filesno parent snapshot found, will read all filesstart scan on [./data/mingongge.zip]start backup on [./data/mingongge.zip]scan finished in 0.249s: 1 files, 942.793 KiBFiles: 1 new, 0 changed, 0 unmodifiedDirs: 1 new, 0 changed, 0 unmodifiedData Blobs: 0 newTree Blobs: 2 newAdded to the repo: 750 Bprocessed 1 files, 942.793 KiB in 0:00snapshot 3e5b7dea saved
如果你再次执行第一步的备份命令,会发现它不再增加内容,只是为当前的数据再增加一个快照。其实,restic它具有扫描文件的功能(逐个文件扫描比对),所以restic只会备份存储一次相同的数据。
文件检测功能
扫描每个文件的全部内容,非常浪费资源,所以 restic 还使用基于文件元数据的更改检测规则来确定文件自上次备份以来是否可能未更改,如果是,则不会再次扫描文件。
在Unix(包括 Linux 和 Mac)上,鉴于文件与先前备份中的文件位于同一位置,以下文件元数据属性必须匹配才能假定其内容未更改:
所以,基于上述的原因,引入一些参数,如下:
--force #关闭更改检测,重新扫描全部文件--ignore-ctime #要求 mtime 匹配,但允许 ctime 不同--ignore-inode #要求 mtime 匹配,但允许 inode number 和 ctime 不同
排除文件参数
--exclude #指定一次或多次排除一个或多个项--iexclude #与exclude相同,但忽略路径的情况--exclude-caches #指定一次排除包含特殊文件的文件夹--exclude-file #指定一次排除包含特殊文件的文件夹--iexclude-file #与exclude-file相同,但忽略路径的情况--exclude-if-present foo #排除文件夹包含名为foo的文件--exclude-larger-than size #指定一次以排除大于给定大小的文件
更多相关的功能请参考:https://restic.readthedocs.io/en/latest/040_backup.html
存储库使用
既然数据备份到了存储库,所以,我们也需要去使用存储库,下面来介绍相关的操作。
列出存储库的所有快照
这个功能和平时在系统上使用ls命令相同,查看显示的功能
[root@centos7 ~]# restic -r /restic/backup_dir/ snapshotsenter password for repository: repository dff64d39 opened successfully, password is correctID Time Host Tags Paths-------------------------------------------------------------------------------4d20711e 2021-06-04 03:40:47 centos7 /root/data3e5b7dea 2021-06-04 03:46:34 centos7 /root/data/mingongge.zip94c62288 2021-06-04 03:51:21 centos7 /root/data-------------------------------------------------------------------------------3 snapshots#还可以使用下面的参数进行过滤匹配查看--path="dir_name"--host hostname#通过相同的过滤器(主机、路径、标签)对输出进行分组--group-by
更多内容请参考:https://restic.readthedocs.io/en/latest/045_working_with_repos.html
检测存储库数据
[root@centos7 ~]# restic -r /restic/backup_dir/ checkusing temporary cache in /tmp/restic-check-cache-294136679enter password for repository: repository dff64d39 opened successfully, password is correctcreated new cache in /tmp/restic-check-cache-294136679create exclusive lock for repositoryload indexescheck all packscheck snapshots, trees and blobs[0:00] 100.00% 3 / 3 snapshotsno errors were found
数据恢复
这个才是重点啊,恢复数据才是王炸。
创建模拟数据删除的环境
[root@centos7 ~]# cd data/[root@centos7 data]# lltotal 33796-rw-r--r-- 1 root root 13034487 Aug 30 2020 goInception-linux-amd64-v1.2.3.tar.gzdrwxr-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]# lltotal 0
恢复数据
直接从快照恢复误删除的数据
[root@centos7 ~]# restic -r /restic/backup_dir/ restore 4d20711e --target /root/enter password for repository: repository dff64d39 opened successfully, password is correctrestoring <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.gzdrwxr-sr-x 11 root 40 4096 Dec 24 22:35 httpd-2.4.46-rw-r--r--
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!