blob: 0c6beeca9ea87f23a3cfd022a7152b6766dd0a50 (
plain)
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
|
#!/usr/bin/env bash
set -e
die() {
echo -e "\033[31mError: " "$@" "\033[0m" >&2
exit 1
}
note() {
echo -e "\033[33mNote: " "$@" "\033[0m"
}
success() {
echo -e "\033[32mSuccess: " "$@" "\033[0m"
}
# Check I'm root.
if [[ $EUID -ne 0 ]]; then
die "This script must be run as root"
fi
if [[ ! -f /run/secrets/auto-backup ]]; then
die "/run/secrets/auto-backup not found, please use docker secrets to set it."
fi
if [[ -z "$CRUPEST_AUTO_BACKUP_INTERVAL" ]]; then
die "Backup interval not set, please set it!"
fi
# shellcheck source=/dev/null
. /run/secrets/auto-backup
note "Checking secrets..."
[[ -n "$CRUPEST_AUTO_BACKUP_COS_ENDPOINT" ]] || die "COS endpoint not set!"
[[ -n "$CRUPEST_AUTO_BACKUP_COS_BUCKET" ]] || die "COS bucket not set!"
[[ -n "$CRUPEST_AUTO_BACKUP_COS_SECRET_ID" ]] || die "COS secret ID not set!"
[[ -n "$CRUPEST_AUTO_BACKUP_COS_SECRET_KEY" ]] || die "COS secret key not set!"
success "Secrets check passed."
note "Checking tools..."
tar --version
zstd --version
/app/coscli --version
success "Tools check passed."
echo "Backup interval set to $CRUPEST_AUTO_BACKUP_INTERVAL..."
if [[ -z "$CRUPEST_AUTO_BACKUP_INIT_DELAY" ]]; then
echo "Initial delay not set, will do a backup immediately!"
else
echo "Initial delay set to $CRUPEST_AUTO_BACKUP_INIT_DELAY ..."
sleep "$CRUPEST_AUTO_BACKUP_INIT_DELAY"
fi
function backup {
note "Begin backup..."
# Get current time and convert it to YYYY-MM-DDTHH:MM:SSZ
current_time="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "Current time UTC: $current_time"
backup_file_ext="tar.zst"
tmp_file="/tmp/data.$backup_file_ext"
echo "Create $tmp_file for data..."
tar -cp --zstd -f "$tmp_file" -C / data
du -h "$tmp_file" | cut -f1 | xargs echo "Size of $tmp_file:"
des_file_name="$current_time.$backup_file_ext"
echo "Upload $des_file_name to COS..."
/app/coscli --init-skip \
--secret-id "${CRUPEST_AUTO_BACKUP_COS_SECRET_ID}" \
--secret-key "${CRUPEST_AUTO_BACKUP_COS_SECRET_KEY}" \
--endpoint "${CRUPEST_AUTO_BACKUP_COS_ENDPOINT}" \
cp "$tmp_file" "cos://${CRUPEST_AUTO_BACKUP_COS_BUCKET}/$des_file_name"
echo "Remove tmp file..."
rm "$tmp_file"
echo "$des_file_name" >>/data/backup.log
success "Finish backup!"
}
# forever loop
while true; do
backup
echo "Sleep for $CRUPEST_AUTO_BACKUP_INTERVAL for next backup..."
sleep "$CRUPEST_AUTO_BACKUP_INTERVAL"
done
|