分享一个脚本: 加密备份网站/目录到huggingface

本文分享了一个使用shell脚本加密备份网站/目录到HuggingFace的脚本。该脚本首先生成公钥,然后加密指定目录的文件,最后将加密文件推
送到Git仓库。脚本使用Git LFS存储文件,并需要在HuggingFace仓库配置SSH密钥,同时确保已安装age命令。

age使用方法

1
2
3
4
$ age-keygen -o key.txt
Public key: age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p #公钥
$ tar cvz ~/data | age -r age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p > data.tar.gz.age # 加密文件
$ age --decrypt -i key.txt -o data.tar.gz data.tar.gz.age # 解密压缩包

脚本内容

注意事项

  1. 是借助git lfs存储文件的, 所以需要先配置好huggingface仓库 ssh 秘钥
  2. 安装好 age 命令
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
#!/bin/bash
#
# 备份和加密指定目录,然后将其推送到 Git 仓库。

set -euo pipefail

export GIT_SSH_COMMAND='ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'

check_ssh_connection() {
if ! ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -T [email protected] | grep -q meleuo; then
echo "登录失败" >&2
exit 1
fi
}

check_age_command() {
if ! command -v age &> /dev/null; then
echo "age 命令不可用" >&2
exit 1
fi
}

cleanup() {
local uuid=$1
if [[ -d /tmp/${uuid} ]]; then
rm -rf "/tmp/${uuid}"
fi
}

backup_and_push() {
local dir_path=$1
local taskname=$2
local dir_name
dir_name=$(basename "${dir_path}")
local uuid
uuid=$(openssl rand -hex 16)
local datetime
datetime=$(date +"%Y%m%d%H%M")
local archive_name="${taskname}_${uuid}_${datetime}.tar.gz"
local encrypted_name="${taskname}_${datetime}.bin"

mkdir "/tmp/${uuid}"
cd "/tmp/${uuid}" || exit 1

if ! tar -czf "${archive_name}" -C "$(dirname "${dir_path}")" "$(basename "${dir_path}")"; then
echo "压缩失败" >&2
cleanup "${uuid}"
exit 1
fi

if ! tar cvz "${archive_name}" | age -r [age] > "${encrypted_name}"; then
echo "加密失败" >&2
cleanup "${uuid}"
exit 1
fi

echo "压缩和加密完成: ${encrypted_name}"

GIT_LFS_SKIP_SMUDGE=1 git clone git@[huggingface仓库地址]
cd b || exit 1
mkdir -p "${taskname}"
mv "../${encrypted_name}" "./${taskname}/${dir_name}${datetime}.bin"
git add "./${taskname}/${dir_name}${datetime}.bin"
git commit -m "ADD ${taskname}/${dir_name}${datetime}.bin"
git push

cleanup "${uuid}"
}

main() {
check_ssh_connection
check_age_command

docker stop nginx
backup_and_push /var/html/www nginx_back
docker start nginx

}

main "$@"

分享一个脚本: 加密备份网站/目录到huggingface

https://www.mutter.cn/2024/10/31/Linux/加密备份网站目录到huggingface/

作者

默吟

发布于

2024-10-31

更新于

2024-12-02

许可协议

CC BY-NC-SA 4.0

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×