加密增量冷备份至 onedrive

加密增量冷备份至 onedrive

最后修改于 2022-4-20 ⋅ 共 2.5k 字 ⋅ 6分钟 / #Tutorial / #Restic, #Rclone, #Onedrive, #备份

前言 #

为了防止数据丢失,我配置了一套自动加密上传本地文件到云盘的服务。在所有云盘服务商中,TeraBox (原名 DuBox,百度网盘海外限定版)和 OneDrive (微软云盘教育版)提供了可以获得免费 1T 以上空间的途径。由于 rclone 支持后者,并且教育版帐号(A1)也相对稳定,所以最后我将冷备份空间定在了 OneDrive。之所以仅作为冷备份,是因为该类型帐号实际违反了 Tos,合法帐号需要真实的学校邮箱,用一段时间后有被 ban 的可能性。

在原定方案中只使用了 rclone 作为备份工具,但是由于 rclone 在加密上的缺陷(加密后的文件名长度会超过云盘限制导致无法上传),因此这里采用 restic(加密增量备份)-> rclone(连接至 onedrive)-> onedrive A1 的方式来冷备份。restic 带来的好处是以块为单位自动去重和加密,因此搭配 vhdx+Bitlocker 这个自带的磁盘加密方案时每次更改无需重新打包。

配置 onedrive 和 rclone #

注册帐号 #

收集:汇总地址
此处是别人提供的可用于注册教育版帐号的临时邮箱合集,推荐自助平台栏目下的链接。未被划去的链接中也有很大一部分已经失效(无法访问/已被限制 throttled)。
推荐1ove.club提供的帐号,注册完毕后还需要绑定手机号和邮箱。同主页下还有其他邮箱可选,包含不同容量,均为 A1 订阅。

查看可用空间 #

创建完帐号后如果想要查询容量,可以将形如https://....sharepoint.com/personal/.../onedrive.aspxonedrive.aspx替换为storman.aspx进行查询。

可选:创建 rclone 应用凭证 #

rclone 使用的访问方式会与所有用户共享额度,在小部分情况下可能会超出微软限制,因此如果有需要,可以创建一个专属的应用访问凭证。
原文

  1. Open https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade and then click New registration.
  2. Enter a name for your app, choose account type Accounts in any organizational directory (Any Azure AD directory - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox), select Web in Redirect URI, then type (do not copy and paste) http://localhost:53682/ and click Register. Copy and keep the Application (client) ID under the app name for later use.
  3. Under manage select Certificates & secrets, click New client secret. Enter a description (can be anything) and set Expires to 24 months. Copy and keep that secret Value for later use (you won’t be able to see this value afterwards).
  4. Under manage select API permissions, click Add a permission and select Microsoft Graph then select delegated permissions.
    Search and select the following permissions: Files.Read, Files.ReadWrite, Files.Read.All, Files.ReadWrite.All, offline_access, User.Read. Once selected click Add permissions at the bottom.

配置 rclone #

在 rclone 目录下输入rclone config

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Current remotes:

e) Edit existing remote
n) New remote
...
e/n/d/r/c/s/q> n # 新建
name> YOUR***REPO # 输入名字
Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
...
25 / Microsoft Azure Blob Storage
   \ "azureblob"
26 / Microsoft OneDrive
   \ "onedrive"
27 / OpenDrive
   \ "opendrive"
...
Storage> 26 # 或者输入 onedrive
OAuth Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> # 上一步可选没做的话留空
OAuth Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> # 上一步可选没做的话留空

在打开的网页中登录并授权。

配置 restic #

参考资料:愉快的使用restic备份数据下载

假设之前设置 rclone 目标为 YOUR***REPO,初始化 restic 并输入密码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
restic -r rclone:YOUR***REPO:restic init
# -o rclone.args="serve restic --stdio --retries 1 --transfers=4 --checkers 16 --fast-list -P --verbose" \
'''
enter password for new repository:
enter password again:
created restic repository **** at rclone:YOUR***REPO:restic

Please note that knowledge of your password is required to access
the repository. Losing your password means that your data is
irrecoverably lost.
'''

配置脚本 #

task.bat

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
@echo off
cd /d %~dp0
:: 代理
:: set http_proxy=socks5://localhost:2090/
set HTTP_PROXY=%http_proxy%
set https_proxy=%http_proxy%
set HTTPS_PROXY=%http_proxy%

echo Start at %date%-%time%
:: 备份多个目录(目录间不会互相影响,且只生成一个快照)
set RESTIC_PASSWORD=PASS***WORD
restic -r rclone:YOUR***REPO:restic backup D:\ E:\ F:\ G:\ --exclude-file=restic.ignore -o rclone.args="serve restic --stdio --retries 1 --transfers=4 --checkers 16 --fast-list"
:: 错误时自动通知
if %ERRORLEVEL% GEQ 1 curl -G https://api.day.app/YOUR***KEY/Restic/FAIL%ERRORLEVEL%?group=restic
:: 自动过期快照
restic -r rclone:YOUR***REPO:restic forget --keep-daily 3 --keep-weekly 3 --keep-monthly 3 --keep-last 4 --prune
echo End at %date%-%time%
ping 127.0.0.1 -n 3600 >nul

restic.ignore

1
2
3
4
5
6
7
System Volume Information
Games
WindowsApps
$$RECYCLE.BIN
Config.Msi
Program Files (x86)
Program Files

脚本说明 #

策略删除快照

  • 手动删除费时费力,则 restic 允许根据策略指定应该自动删除哪些快照
  • 可以指定保留多少小时/每日/每周/每月和每年快照,以及删除多少其他快照
  • 可以使用 –dry-run 参数来测试需要删除的文件和目录列表
参数参数含义解释
–keep-last n不删除最近的 n 个快照
–keep-hourly n不删除快照的最后 n 个小时;每小时只保留最后一个快照
–keep-daily n不删除快照的最后 n 个天;每小时只保留最后一个快照
–keep-weekly n不删除快照的最后 n 个周;每小时只保留最后一个快照
–keep-monthly n不删除快照的最后 n 个月;每小时只保留最后一个快照
–keep-yearly n不删除快照的最后 n 个年;每小时只保留最后一个快照
–keep-tag保留所有由此选项指定的标签的快照;可多次指定
–keep-within duration保留在最新快照持续时间内创建的所有快照
1
restic -r /restic-repo forget --keep-monthly 2 --keep-last 4 --prune # --dry-run

常用命令

  • backup 备份
  • check 检查
  • snapshots 列出快照
  • forget 删除快照
  • copy --repo2 不同 repo 间复制
  • diff 比较
  • restore [latest|77***75] --target /restore-work 恢复
  • dump latest production.sql 直接读取

错误时自动通知 Bark

1
if %ERRORLEVEL% GEQ 1 curl -G https://api.day.app/YOUR***KEY/Restic/FAIL%ERRORLEVEL%?group=restic

设置自动运行服务 #

任务计划程序中新建:

选项内容
名称restic_backup
常规:安全选项只在用户登录时运行,使用最高权限运行
触发器每日,在每 4 天的 23:45
操作启动程序,[运行脚本所在的位置]\task.bat
设置如果任务运行时间超过以下时间,停止任务:3天

检查是否被 ban #

如果运行日志中出现Failed to create file system for "...": failed to make remote "..." to wrap: failed to get root: activityLimitReached: throttledRequest: The request has been throttled,说明该域名邮箱已被处理,需要重新注册其他邮箱。

rclone 冷备份配置文件,仅作记录。

(已弃用) #

原 rclone 加密 #

作为冷备份,加密也是必需品,rclone 提供了便捷的透明加密模式,可以在保留目录结构的同时加密文件/文件夹名称和内容。输入rclone config

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Current remotes:

Name                 Type
====                 ====
YOUR***REPO          onedrive

e) Edit existing remote
n) New remote
d) Delete remote
...
e/n/d/r/c/s/q> n
name> encrypt_YOUR***REPO
Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
...
10 / Dropbox
   \ "dropbox"
11 / Encrypt/Decrypt a remote
   \ "crypt"
12 / Enterprise File Fabric
   \ "filefabric"
...
Storage> 11
...
Enter a string value. Press Enter for the default ("").
remote> YOUR***REPO:encrypt_folder # 刚刚新建的名字:文件夹名
How to encrypt the filenames.
...
filename_encryption> 1 # 1 的安全性最好,但是过长的文件名会出错,2 的文件名加密可破解的
Option to either encrypt directory names or leave them intact.
...
y/g> g # 选 y 代表用自己的密码而不是生成
Password strength in bits.
...
Bits> 1024
Your password is: 
...
Use this password? Please note that an obscured version of this 
password (and not the password itself) will be stored under your 
configuration file, so keep this generated password in a safe place.
...
y/n> y
Password or pass phrase for salt. Optional but recommended.
...
y/g/n> g
Password strength in bits.
...
Bits> 1024
Your password is: 
...
Use this password? Please note that an obscured version of this 
password (and not the password itself) will be stored under your 
configuration file, so keep this generated password in a safe place.
y) Yes (default)
n) No
y/n> y
Edit advanced config?
y) Yes
n) No (default)
y/n> n

注意:在选择文件名加密方式 filename_encryption 时, rclone 提供了标准 standard 和 混淆 obsucure 两种加密方式。标准加密这个方式会使加密后的文件名变长而导致文件无法上传,而混淆加密虽然不会显著增加文件名长度但只是简单的移位,仅能保证无法直接被阅读。所以个人建议还是使用标准加密,对因此产生错误的文件用--exclude系列参数过滤。

备份脚本 #

backup.bat

1
2
3
4
echo D:%date%-%time% 
echo Start at %date%-%time% >active_doc_D.log
rclone sync D:\ secret_od:active_doc_D --exclude-from rclone.ignore --retries 1 --transfers=4 --checkers 16 --fast-list -P --log-file active_doc_D.log
echo End at %date%-%time% >>active_doc_D.log

rclone.ignore

1
2
3
4
5
/System Volume Information/**
/Games/**
/WindowsApps/**
/$RECYCLE.BIN/**
/Config.Msi/**