一次致命的失误

本文最后更新于:2024年5月3日 晚上

一次致命的失误

今晚清理文件的时候, 不小心用api把图床上所有, 五年来积攒下的全部图片

清空了

image-20230415233902580
image-20230415233847437

只能把所有博客隐藏掉了, 心情很崩溃, 还得去解决一些关键笔记的图片问题…

以后坚决要把图片至少在两个地方 + 本地备份.

想死.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import os
import yaml

for root, dirs, files in os.walk("."):
for file in files:
if file.endswith(".md"):
filepath = os.path.join(root, file)
with open(filepath, "r+", encoding="utf-8") as f:
content = f.read()
if content.startswith("---"):
front_matter, _, content = content[3:].partition("---\n")
data = yaml.load(front_matter, Loader=yaml.FullLoader)
data['hide'] = True
front_matter = yaml.dump(data, sort_keys=False, allow_unicode=True)
f.seek(0)
f.write("---\n" + front_matter.strip() + "\n---\n" + content)
f.truncate()
print(f"{filepath} 已经添加了 hide: true 标识")
else:
f.seek(0)
f.write("---\nhide: true\n---\n" + content)
f.truncate()
print(f"{filepath} 新生成 front-matter")

后续:

后来采用的图床同步方案:

img

现在相当于三端同步, 我就不信还能碰到惨案…

附同步code:

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
import os
import time
import git
import upyun
import wget

# 变量设置
upyun_service =
upyun_username =
upyun_password =
upyun_path =

github_repo_path =
github_username =
github_password =
github_branch =

# 初始化UpYun对象
up = upyun.UpYun(upyun_service, upyun_username, upyun_password)

# 初始化Git仓库
repo = git.Repo()
git = repo.git

# 获取Github仓库当前分支到本地
git.pull("origin", github_branch)

# 获取当前时间
t = time.localtime()
current_time = time.strftime("%Y-%m-%d %H:%M:%S", t)

# 定义文件列表
files = []

# 同步upyun
for item in up.getlist(upyun_path):
filepath = item["name"]
filename = os.path.basename(filepath)
files.append(filename)
if not os.path.exists('./' + filename):
# 下载文件到本地
wget.download(f"https://youpai.roccoshi.top/img/{filename}")
# 将文件提交到Github仓库
print(f"\n Committing new file {filepath}...")
git.add(filepath)

git.commit("-m", f"sync from upyun at {current_time}")
git.push('origin', github_branch)
# 推送文件到Github仓库
print("Pushing changes to GitHub...")
git.push("origin", github_branch)

print("Backup finished!")

一次致命的失误
https://moreality.net/posts/64403/
作者
Moreality
发布于
2023年4月15日
许可协议