前言

人们经常说硬盘有价,数据无价。可见对重要数据备份的必要性。这些数据一旦误删或者丢失对谁来说都是一笔巨大的损失。我曾经也有过误删重要文件的经历,所以一直有备份文件的习惯。在vps上,之前我一直在用宝塔的定时任务加onedrive插件进行目录或者网站的备份。对于我这种习惯图形界面操作的小白来说真的是方便快捷又简单。不过在有些不需要宝塔的项目上还是要想个别的方法定时备份文件夹和文件。因为是备份到OneDrive上,所以这时候第一时间想到了可以挂载OneDrive的程序:Rclone。我的想法是:定时执行脚本打包备份文件让Rclone上传备份文件到OneDrive实现定时备份。思路有了我们说干就干,经过一番资料查找我发现也有人有过这样的思路,并写过相应的文章。因为大佬们的技术比我强太多了,并且他们的用途范围超出了我单纯备份的思想。所以我参考了他们的一部分方法与思路,在这个基础上再修改成符合我思路的Rclone+OneDrive+Crontab定时备份。

配置Rclone+OneDrive

Rclone是一個開源、多線程、命令行界面的電腦程式,可用於管理雲存儲。其功能包括檔案同步、文件傳輸、加密、緩存和掛載。(维基百科)

想了解更多可以去rclone的官网:Rclone

首先安装Rclone

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

输入 rclone config 开始配置Rclone+OneDriver

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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
rclone config

rclone config
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n #选择 n 新建配置
name> OD
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
1 / 1Fichier
\ "fichier"
2 / Alias for an existing remote
\ "alias"
3 / Amazon Drive
\ "amazon cloud drive"
4 / Amazon S3 Compliant Storage Providers including AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, and Tencent COS
\ "s3"
5 / Backblaze B2
\ "b2"
6 / Box
\ "box"
7 / Cache a remote
\ "cache"
8 / Citrix Sharefile
\ "sharefile"
9 / Compress a remote
\ "compress"
10 / Dropbox
\ "dropbox"
11 / Encrypt/Decrypt a remote
\ "crypt"
12 / Enterprise File Fabric
\ "filefabric"
13 / FTP Connection
\ "ftp"
14 / Google Cloud Storage (this is not Google Drive)
\ "google cloud storage"
15 / Google Drive
\ "drive"
16 / Google Photos
\ "google photos"
17 / Hadoop distributed file system
\ "hdfs"
18 / Hubic
\ "hubic"
19 / In memory object storage system.
\ "memory"
20 / Jottacloud
\ "jottacloud"
21 / Koofr
\ "koofr"
22 / Local Disk
\ "local"
23 / Mail.ru Cloud
\ "mailru"
24 / Mega
\ "mega"
25 / Microsoft Azure Blob Storage
\ "azureblob"
26 / Microsoft OneDrive
\ "onedrive"
27 / OpenDrive
\ "opendrive"
28 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
\ "swift"
29 / Pcloud
\ "pcloud"
30 / Put.io
\ "putio"
31 / QingCloud Object Storage
\ "qingstor"
32 / SSH/SFTP Connection
\ "sftp"
33 / Sugarsync
\ "sugarsync"
34 / Tardigrade Decentralized Cloud Storage
\ "tardigrade"
35 / Transparently chunk/split large files
\ "chunker"
36 / Union merges the contents of several upstream fs
\ "union"
37 / Webdav
\ "webdav"
38 / Yandex Disk
\ "yandex"
39 / Zoho
\ "zoho"
40 / http Connection
\ "http"
41 / premiumize.me
\ "premiumizeme"
42 / seafile
\ "seafile"
Storage> 26 #OneDrive选择26,Rclone不同版本的序列不一样根据实际选择
** See help for onedrive backend at: https://rclone.org/onedrive/ **

OAuth Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> #回车 使用默认client_id
OAuth Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> #回车 使用默认client_secret
Choose national cloud region for OneDrive.
Enter a string value. Press Enter for the default ("global").
Choose a number from below, or type in your own value
1 / Microsoft Cloud Global
\ "global"
2 / Microsoft Cloud for US Government
\ "us"
3 / Microsoft Cloud Germany
\ "de"
4 / Azure and Office 365 operated by 21Vianet in China
\ "cn"
region> 1 # 选择1国际版,按照你的实际OneDrive版本选择情况选择
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n> n #选择 n 不进行高级编辑
Remote config
Use auto config?
* Say Y if not sure
* Say N if you are working on a remote or headless machine
y) Yes (default)
n) No
y/n> n #输入n
#输入后会打打开链接 登录Microsoft账号后 选择 是 即可
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"

Then paste the result below:
result> {"access_token":"XXX"} # 粘贴自己的 Token
Choose a number from below, or type in an existing value
1 / OneDrive Personal or Business
\ "onedrive"
2 / Root Sharepoint site
\ "sharepoint"
3 / Sharepoint site name or URL (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"
7 / Sharepoint server-relative path (advanced, e.g. /teams/hr)
\ "path"
Your choice> 1 # 选择 1
Found 1 drives, please select the one you want to use:
0: OneDrive (business) id=XXX
Chose drive to use:> 0 # 选择 0
Found drive 'root' of type 'business', URL: https://xxx.xxx.com
Is that okay?
y) Yes (default)
n) No
y/n> y # 选择 y
--------------------
[onedrive]
type = OD
region = global
token = {"access_token":"XXX"}
drive_id = XXX
drive_type = business
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y // 选择 y
Current remotes:

Name Type
==== ====
OD 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 配置完成

以上是我从网上抄来的rclone配置onedrive的过程。没有配置过的可以参考一下。而我以前在window上使用rclone配置过onedrive,据我之前的测试,rclone的配置文件 config.nof 在不同系统下是通用的。换句话说我只要把以前window上的rclone的配置文件 config 放到我的Ubuntu系统vps的对应目录就无需再重新配置。反之亦然!
在不同系统上rclone的配置文件路径:

1
2
C:\Users\用户名\.config\rclone\rclone.comf       # Windows
/root/.config/rclone/rclone.comf # Ubuntu

创建备份脚本

使用 vi 新建一个 backup.sh 脚本

1
vi backup.sh

i 进入编辑模式复制粘贴以下脚本,并根据解释定义进行修改:

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
#!/bin/bash

# 定义OneDrive的备份目录
OD_PATH="OD:Backup/test"

# 定义需要备份的目录及文件或文件
BACKUP_SRC="/root/test"

# 定义临时文件存放目录
BACKUP_DST="/root/tmp"

# 定义文件前缀名
NOW=$(date +"%Y.%m.%d")
OLD=$(date -d -10day +"%Y.%m.%d")

# 定义备份文件的存储路径及文件名
DESTFILE="$BACKUP_DST/$NOW.tar.gz"

# 将备份的目录打包成一个压缩包
tar cfz $DESTFILE $BACKUP_SRC

# 使用rclone上传到OD
rclone copy $DESTFILE $OD_PATH

# 删除本地的临时文件
rm -f $DESTFILE

# 删除10天前的备份
rclone delete $OD_PATH/$OLD.tar.gz

解释定义:

  • OD_PATH 是指你要备份到OneDrive中的目标目录,填写格式为rclone配置中的 name:目标目录 我上面是以 OD:Backup/test 为例,备份文件就会存到我OneDrive的Backup/test目录下,可根据实际进行修改。

  • BACKUP_SRC 是你想要备份的文件或目录我这里在root目录下建立了一个test文件夹作为演示,请根据实际需要备份的文件或文件夹进行修改。

  • BACKUP_DST 是一个临时缓存文件夹用于存放打包未上传好的文件,此处以root目录下的tmp文件夹为例,可修改并 需要在相应目录下建立好填写的文件夹

  • 定义文件前缀名:以时间定义文件名的前缀

  • 定义备份文件的存储路径及文件名:以 文件前缀名.tar.gz 的方式存储在临时缓存文件夹。我个人是使用时间备份所以不加什么后缀。

  • 删除10天前的备份:这个需要可以在上面的文件名前缀处修改,不需要可以删除,我这里备份十天以内的就行了。

创建Cron定时任务

关于Cron是一款類Unix作業系統下的基於時間的任務管理系統。用戶們可以通過cron在固定時間、日期、間隔下,運行定期任務(可以是命令和腳本)(维基百科)

直接输入:

1
crontab -e

首次输入会选编辑器,根据个人习惯选择吧。使用 select-editor 命令可以更换编辑器:

1
2
3
4
5
6
7
8
9
root:~# select-editor

Select an editor. To change later, run 'select-editor'.
1. /bin/ed
2. /bin/nano <---- easiest
3. /usr/bin/vim.basic
4. /usr/bin/vim.tiny

Choose 1-4 [2]: 3

这里我习惯用 vi ,所以选3。

进入编辑器后按 i 开始编辑并在最后一行添加:

1
30 13 * * * bash /root/sync.sh

这行的意思是:每天的 13:30 执行 /root/backup.sh 这个程序。备份时间是可以自定义的,我这里以每天备份为例你可以设置每星期备份或者指定日期备份。

关于crontab时间格式简单的说明如下:

1
2
3
4
5
6
7
8
*    *    *    *    *    bash /root/sync.sh
- - - - - -------------------
| | | | | 执行的程序
| | | | +----- 星期中星期几 (0 - 6) (星期天 为0)
| | | +---------- 月份 (1 - 12)
| | +--------------- 一个月中的第几天 (1 - 31)
| +-------------------- 小时 (0 - 23)
+------------------------- 分钟 (0 - 59)

如果想学习更多可以谷歌一下,这里只用于定时备份的目的已达到,故不做详细说明。

查看一下定时任务有没有添加成功,重启cron使定时任务生效:

1
2
crontab -l                       #查看定时任务列表
service cron restart #重启cron服务

到此为止我们的Rclone+OneDrive+crontab的定时备份就已经完成了。它会在指定时间打包指定目录并上传到OneDrive的指点位置。

最后补充

本来在这里已经可以了但是我想说一个我在测试这个备份方案时的一个小插曲。

我在测试时单独执行备份脚本是没有问题的可以正常备份到OneDrive。

但是crontab任务设置后到时间却没有文件上传到OneDrive。

我重新设置了还重启了cron服务也还是不行。

于是我检查了一下时区:

1
2
root:~# date -R
Mon, 25 Mar 2022 08:54:50 +0100

我才明白过来,并被自己的愚蠢行为逗笑。原来这vps的时区是+1区的

所以说吧时区更改为我所在的+8区就行了:

1
cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

更新系统时间时区后需要重启cron:

1
2
3
/etc/init.d/cron start       #启动cron
/etc/init.d/cron stop #停止cron
/etc/init.d/cron restart #重启cron

这下子终于可以了。

其实改不改这个时间都是可以的,因为它也是按时备份。只是时区不一样可能不是按照你所在的地区时间进行备份。我只是测试效果才改的。

这个方法很简单粗暴,并不一定是备份在OneDrive上,只要是rclone支持的云盘服务理论都是可行的,只要根据思路稍加修改就可以。希望能帮到你。