鬼使神差的未备份就重置了服务器,真是见了鬼了。

开始玩云服务器的时候使用宝塔面板备份数据到网盘是很方便的,后面放弃宝塔面板,这个功能一直想做而没有做,这次再也不拖了。

Gemini 给我写了一份自动备份脚本,主要功能:压缩备份 docker 文件夹、使用当日日期命名文件、输出日志到日志文件夹、只保留最近7天的备份、最后通过 rclone 同步到微软 OneDrive 网盘。

1、Debian 安装 rclone

官方有一键安装脚本,适用于 Linux/macOS/BSD 系统。

1
curl https://rclone.org/install.sh | sudo bash

由于服务器没有网页,需要使用电脑辅助获取 access_token,二者版本要保持一致。默认使用官方 API,由于官方 API 为所有用户共享,速度可能容易被限制,如果要求较高可以官方参照教程去 Microsoft Azure 自己注册应用并设置 API 获取相应的 client_idclient_secret 以减少限制。

2、Debian 配置 rclone

  • 服务器输入以下命令进行配置
1
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
root@debian:~# rclone config
2024/06/26 18:15:45 NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults
No remotes found, make a new one?
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n # 输入 n,新建远程连接

Enter name for new remote.
name> onedrive # 输入自定义名称,用于区分不同连接

Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
1 / 1Fichier
\ (fichier)
2 / Akamai NetStorage
\ (netstorage)
3 / Alias for an existing remote
\ (alias)
4 / Amazon S3 Compliant Storage Providers including AWS, Alibaba, ArvanCloud, Ceph, ChinaMobile, Cloudflare, DigitalOcean, Dreamhost, GCS, HuaweiOBS, IBMCOS, IDrive, IONOS, LyveCloud, Leviia, Liara, Linode, Magalu, Minio, Netease, Petabox, RackCorp, Rclone, Scaleway, SeaweedFS, StackPath, Storj, Synology, TencentCOS, Wasabi, Qiniu and others
\ (s3)
5 / Backblaze B2
\ (b2)
6 / Better checksums for other remotes
\ (hasher)
7 / Box
\ (box)
8 / Cache a remote
\ (cache)
9 / Citrix Sharefile
\ (sharefile)
10 / Combine several remotes into one
\ (combine)
11 / Compress a remote
\ (compress)
12 / Dropbox
\ (dropbox)
13 / Encrypt/Decrypt a remote
\ (crypt)
14 / Enterprise File Fabric
\ (filefabric)
15 / FTP
\ (ftp)
16 / Google Cloud Storage (this is not Google Drive)
\ (google cloud storage)
17 / Google Drive
\ (drive)
18 / Google Photos
\ (google photos)
19 / HTTP
\ (http)
20 / Hadoop distributed file system
\ (hdfs)
21 / HiDrive
\ (hidrive)
22 / ImageKit.io
\ (imagekit)
23 / In memory object storage system.
\ (memory)
24 / Internet Archive
\ (internetarchive)
25 / Jottacloud
\ (jottacloud)
26 / Koofr, Digi Storage and other Koofr-compatible storage providers
\ (koofr)
27 / Linkbox
\ (linkbox)
28 / Local Disk
\ (local)
29 / Mail.ru Cloud
\ (mailru)
30 / Mega
\ (mega)
31 / Microsoft Azure Blob Storage
\ (azureblob)
32 / Microsoft Azure Files
\ (azurefiles)
33 / Microsoft OneDrive
\ (onedrive)
34 / OpenDrive
\ (opendrive)
35 / OpenStack Swift (Rackspace Cloud Files, Blomp Cloud Storage, Memset Memstore, OVH)
\ (swift)
36 / Oracle Cloud Infrastructure Object Storage
\ (oracleobjectstorage)
37 / Pcloud
\ (pcloud)
38 / PikPak
\ (pikpak)
39 / Proton Drive
\ (protondrive)
40 / Put.io
\ (putio)
41 / QingCloud Object Storage
\ (qingstor)
42 / Quatrix by Maytech
\ (quatrix)
43 / SMB / CIFS
\ (smb)
44 / SSH/SFTP
\ (sftp)
45 / Sia Decentralized Cloud
\ (sia)
46 / Storj Decentralized Cloud Storage
\ (storj)
47 / Sugarsync
\ (sugarsync)
48 / Transparently chunk/split large files
\ (chunker)
49 / Uloz.to
\ (ulozto)
50 / Union merges the contents of several upstream fs
\ (union)
51 / Uptobox
\ (uptobox)
52 / WebDAV
\ (webdav)
53 / Yandex Disk
\ (yandex)
54 / Zoho
\ (zoho)
55 / premiumize.me
\ (premiumizeme)
56 / seafile
\ (seafile)
Storage> 33 # 找到 Microsoft OneDrive 的序号,我这里的版本是 33

Option client_id.
OAuth Client Id.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_id> # 一般留空,直接回车,如果你需要同步大量文件可以自己去 Microsoft Azure 注册应用程序并设置 API,而不使用 rclone 的 API 减少限制

Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_secret> # 一般留空,直接回车

Option region.
Choose national cloud region for OneDrive.
Choose a number from below, or type in your own value of type string.
Press Enter for the default (global).
1 / Microsoft Cloud Global
\ (global)
2 / Microsoft Cloud for US Government
\ (us)
3 / Microsoft Cloud Germany
\ (de)
4 / Azure and Office 365 operated by Vnet Group in China
\ (cn)
region> 1 # 选择你的 OneDrive 区域

Edit advanced config?
y) Yes
n) No (default)
y/n> n # 输入 n,不进行高级设置

Use web browser to automatically authenticate rclone with remote?
* Say Y if the machine running rclone has a web browser you can use
* Say N if running rclone on a (remote) machine without web browser access
If not sure try Y. If Y failed, try N.

y) Yes (default)
n) No
y/n> n # 输入 n ,服务器没有网页

Option config_token.
For this to work, you will need rclone available on a machine that has
a web browser available.
For more help and alternate methods see: https://rclone.org/remote_setup/
Execute the following on the machine with the web browser (same rclone
version recommended):
rclone authorize "onedrive" "xxxx" # 复制这段内容,用电脑上相同版本的 rclone 去获取 token
Then paste the result.
Enter a value.
config_token> xxxx # 输入电脑辅助获取的 token

3、电脑辅助获取 access_token

连接 OneDriveaccess_token 需要在本地 Win­dows 电脑上下载相同版本的 Windows 版 rclone 辅助获取,下载后解压,在 rclone 文件夹中的地址栏输入 cmd,打开命令提示符,输入服务器 rclone 配置中获取的内容rclone authorize "onedrive" "xxxx"

1
rclone authorize "onedrive" "xxxx"

回车会自动打开网页去授权,最好授权前网页先退出微软账号,授权时再登录,授权完成返回 cmd

1
2
3
4
5
6
7
8
9
C:\Program Files\rclone-v1.67.0-windows-amd64>rclone authorize "onedrive" "xxxx"
2024/06/27 11:41:54 NOTICE: Make sure your Redirect URL is set to "http://localhost:53682/" in your custom config.
2024/06/27 11:41:54 NOTICE: If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth?state=xxxx
2024/06/27 11:41:54 NOTICE: Log in and authorize rclone for access
2024/06/27 11:41:54 NOTICE: Waiting for code...
2024/06/27 11:43:18 NOTICE: Got code
Paste the following into your remote machine --->
xxxx
<---End paste

复制保存 xxxx 所有内容,粘贴到服务器 config_token 即可

4、Debian 继续配置 rclone

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
62
63
64
65
66
67
Option config_type.
Type of connection
Choose a number from below, or type in an existing value of type string.
Press Enter for the default (onedrive).
1 / OneDrive Personal or Business
\ (onedrive)
2 / Root Sharepoint site
\ (sharepoint)
/ Sharepoint site name or URL
3 | E.g. mysite or https://contoso.sharepoint.com/sites/mysite
\ (url)
4 / Search for a Sharepoint site
\ (search)
5 / Type in driveID (advanced)
\ (driveid)
6 / Type in SiteID (advanced)
\ (siteid)
/ Sharepoint server-relative path (advanced)
7 | E.g. /teams/hr
\ (path)
config_type> 1 # 输入你的 OneDrive 类型

Option config_driveid.
Select drive you want to use
Choose a number from below, or type in your own value of type string.
Press Enter for the default (xxxx).
1 / (personal)
\ (xxxx)
config_driveid> 1 # 输入 1 即可

Drive OK?

Found drive "root" of type "personal"
URL: https://onedrive.live.com/?cid=xxxx

y) Yes (default)
n) No
y/n> y # 输入 y 确认

Configuration complete.
Options:
- type: onedrive
- client_id: xxxx
- client_secret: xxxx
- token: {"access_token":"xxxx"}
- drive_id: xxxx
- drive_type: personal
Keep this "onedrive" remote?
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y # 输入 y 确认

Current remotes:

Name Type
==== ====
onedrive onedrive

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> q # 输入 q 退出配置

5、自动备份

  • 创建 docker_backup.sh 文件,内容如下
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
62
#!/bin/bash

# 设置备份目录
backup_dir="/root/backup"
# 设置日志目录
log_dir="/root/backup/log"

# 创建备份目录(如果不存在)
mkdir -p "$backup_dir"
# 创建日志文件目录(如果不存在)
mkdir -p "$log_dir"

# 设置备份文件名,包含日期
backup_file="docker-backup-$(date +%Y-%m-%d).tar.gz"
# 设置备份日志文件名,包含日期
backup_log_file="$log_dir/backup-$(date +%Y-%m-%d).log"
# 设置同步日志文件名,包含日期
rclone_log_file="$log_dir/rclone-$(date +%Y-%m-%d).log"

# 使用tar命令进行压缩备份,并将输出和错误信息重定向到备份日志文件
tar -czvf "$backup_dir/$backup_file" /root/.docker > "$backup_log_file" 2>&1

# 检查备份是否成功
if [ $? -eq 0 ]; then
# 在备份日志文件中记录备份完成信息
echo "$(date +%Y-%m-%d_%H:%M:%S) - Backup completed successfully." >> "$backup_log_file"

# 保留最近7天的备份,删除其他备份
find "$backup_dir" -type f -mtime +7 -delete
if [ $? -eq 0 ]; then
# 在备份日志文件中记录删除完成信息
echo "$(date +%Y-%m-%d_%H:%M:%S) - Old backups deleted successfully." >> "$backup_log_file"
else
# 在备份日志文件中记录删除失败信息
echo "$(date +%Y-%m-%d_%H:%M:%S) - Deleting old backups failed." >> "$backup_log_file"
fi

# 保留最近7天的日志文件,删除其他日志文件
find "$log_dir" -type f -mtime +7 -delete
if [ $? -eq 0 ]; then
# 在备份日志文件中记录删除完成信息
echo "$(date +%Y-%m-%d_%H:%M:%S) - Old logs deleted successfully." >> "$backup_log_file"
else
# 在备份日志文件中记录删除失败信息
echo "$(date +%Y-%m-%d_%H:%M:%S) - Deleting old logs failed." >> "$backup_log_file"
fi

# 使用 rclone 同步本地备份到 OneDrive,设置同步日志文件名
rclone sync "$backup_dir" "onedrive:backups/docker" --log-file="$rclone_log_file"

# 检查同步是否成功
if [ $? -eq 0 ]; then
# 在同步日志文件中记录同步完成信息
echo "$(date +%Y-%m-%d_%H:%M:%S) - Sync to OneDrive completed successfully." >> "$rclone_log_file"
else
# 在同步日志文件中记录同步失败信息
echo "$(date +%Y-%m-%d_%H:%M:%S) - Sync to OneDrive failed." >> "$rclone_log_file"
fi
else
# 在备份日志文件中记录备份失败信息
echo "$(date +%Y-%m-%d_%H:%M:%S) - Backup failed." >> "$backup_log_file"
fi
  • 赋予脚本执行权限
1
chmod +x docker_backup.sh
  • 使用 crontab -e 编辑您的 crontab 任务,添加一行代码,让系统每天凌晨1点执行该脚本
1
crontab -e
1
0 1 * * * /bin/bash /root/docker_backup.sh

参考文章:
Linux 定时自动备份数据到 OneDrive/Google Drive