others-七牛云图床备份

others-七牛云图床备份, 备份一下求个心安, 万一真的某天挂了还可以丢到其他云上.
支持增量下载, 本地已有的文件就不再下载.


备份姿势

官网地址: https://developer.qiniu.com/kodo/kb/3744/batch-download-and-backup-space

这里用的是 python3 脚本. 本来想用 go 写的, 毕竟并发好, 量大下载速度爽, 机子上没有环境懒得去搭了, 直接上了 python.

先安装 七牛云 的 py库

1
# pip3 install qiniu

批量下载脚本. 简单粗暴上代码.

有时执行时 bucket.list 接口会返回 None, 是七牛云的问题. 下载完对比一下文件总量确保完全下载, 对不上的话可以执行多几次脚本, 反正是增量的.

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
# -*- coding: utf-8 -*-
from qiniu import Auth
from qiniu import BucketManager
import requests
import os

access_key = "aaa"
secret_key = "bbb"

q = Auth(access_key, secret_key)
bucket = BucketManager(q)

# 前缀
prefix = None
# 列举条目
limit = 1000 # 一次列举最多 1000 个
# 列举出除'/'的所有文件以及以'/'为分隔的所有前缀
delimiter = None

local_save_path = "H:/Picture/qiniu_pic"

local_map = {}

bucket_map = {
"hexo-blog" : "http://domain01.com",
'mywiki' : 'http://domain02.com',
'mywiki-2' : 'http://domain03.com',
'mywiki-3' : 'http://domain04.com',
}


def download_bucket(bucketName, bucketDomain, marker, cnt):
save_dir = os.path.join(local_save_path, bucketName)
if not os.path.exists(save_dir):
os.makedirs(save_dir)

if cnt == 0:
print(
"------------ start download bucket:[%s], domain:[%s]"
% (bucketName, bucketDomain)
)

ret, eof, info = bucket.list(bucketName, prefix, marker, limit, delimiter)
if ret == None:
print("--- Error, ret is None!", ret)
return

for i in ret.get("items"):
# print('key:', i['key'])
webfile = i["key"]

# 本地已有的文件就跳过
if (
local_map.get(bucketName) != None
and local_map.get(bucketName).get(webfile) != None
):
continue

base_url = "%s/%s" % (bucketDomain, webfile)
print("--- downloading: %s" % base_url)

# 如果空间有时间戳防盗链或是私有空间,可以调用该方法生成私有链接
private_url = q.private_download_url(base_url, expires=100)
# print(private_url)

r = requests.get(private_url)

if r.content:
cnt += 1
file = open(os.path.join(save_dir, webfile), "wb")
file.write(r.content)
file.flush()
file.close()

retMarker = ret.get("marker")
if retMarker != None: # 如果没有列举完, 这个字段是个字符串标记, 用于下次的列举
download_bucket(bucketName, bucketDomain, retMarker, cnt)
else:
print(
"------------ over, bucket:[%s], domain:[%s], count:[%d]"
% (bucketName, bucketDomain, cnt)
)
print()


# 收集本地已有的图片, 避免重复下载
def collect_local_exist(bucketName):
save_dir = os.path.join(local_save_path, bucketName)
if not os.path.exists(save_dir):
return

local_map[bucketName] = {}
for root, dirs, files in os.walk(save_dir):
for file in files:
local_map[bucketName][file] = True


def main():
for (k, v) in bucket_map.items():
collect_local_exist(k)
download_bucket(k, v, None, 0)


if __name__ == "__main__":
main()

存储空间

下好的文件


trick

我的使用方式是集成到 vscode 中, 把备份的目录拖到vscode中作为一个工作区, 写一个任务去执行这个python脚本, 在python脚本增加一个git的 pull -> commit -> push 自动提交到一个git远端仓库备份. 以后只需要打开这个工作区, 按快捷键 ctrl + shift + b 即可执行任务进行备份.
很多日常使用的工具都集成到 vscode 中, 因为写工具插件太方便了.


git 保存

可以将 七牛云 下载下来的图片备份到一个 git 仓库中. 国内使用 码云

码云

仓库必须设置为 公有 仓库

上传到码云上, 码云管理后台看到的图片的地址

1
https://gitee.com/username/my_repo/blob/master/img/20200315140313.png

需要把 blob 修改为 raw, 才能在 md, 浏览器访问到正真的图片

1
https://gitee.com/username/my_repo/raw/master/img/20200315140313.png