aboutsummaryrefslogtreecommitdiff
path: root/docker/auto-backup/daemon.bash
diff options
context:
space:
mode:
Diffstat (limited to 'docker/auto-backup/daemon.bash')
-rwxr-xr-xdocker/auto-backup/daemon.bash97
1 files changed, 67 insertions, 30 deletions
diff --git a/docker/auto-backup/daemon.bash b/docker/auto-backup/daemon.bash
index a4dd5dc..0c6beec 100755
--- a/docker/auto-backup/daemon.bash
+++ b/docker/auto-backup/daemon.bash
@@ -2,56 +2,93 @@
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
- echo "This script must be run as root" 1>&2
- exit 1
+ 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
-# Check xz, tar and coscmd
-xz --version
+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 {
- # Output "Begin backup..." in yellow and restore default
- echo -e "\e[0;103m\e[K\e[1mBegin backup..." "\e[0m"
+ note "Begin backup..."
- # Get current time and convert it to YYYY-MM-DDTHH:MM:SSZ
- current_time=$(date +%Y-%m-%dT%H:%M:%SZ)
- echo "Current time: $current_time"
+ # 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"
- echo "Create tar.xz for data..."
+ backup_file_ext="tar.zst"
+ tmp_file="/tmp/data.$backup_file_ext"
- # tar and xz /data to tmp
- tar -cJf /tmp/data.tar.xz -C / data
+ echo "Create $tmp_file for data..."
+ tar -cp --zstd -f "$tmp_file" -C / data
- # Output /tmp/data.tar.xz size
- du -h /tmp/data.tar.xz | cut -f1 | xargs echo "Size of data.tar.xz:"
+ du -h "$tmp_file" | cut -f1 | xargs echo "Size of $tmp_file:"
- destination="${current_time}.tar.xz"
+ des_file_name="$current_time.$backup_file_ext"
+ echo "Upload $des_file_name to COS..."
- # upload to remote
- dotnet /AutoBackup/AutoBackup.dll /tmp/data.tar.xz "$destination"
+ /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..."
- # remove tmp
- rm /tmp/data.tar.xz
+ echo "Remove tmp file..."
+ rm "$tmp_file"
- echo "$destination" >> /data/backup.log
+ echo "$des_file_name" >>/data/backup.log
- # echo "Backup finished!" in green and restore default
- echo -e "\e[0;102m\e[K\e[1mFinish backup!\e[0m"
+ success "Finish backup!"
}
-echo "Initial delay: $CRUPEST_AUTO_BACKUP_INIT_DELAY"
-sleep "$CRUPEST_AUTO_BACKUP_INIT_DELAY"
-
# forever loop
while true; do
- backup
+ backup
- # sleep for CRUPEST_AUTO_BACKUP_INTERVAL
- echo "Sleep for $CRUPEST_AUTO_BACKUP_INTERVAL for next backup..."
- sleep "$CRUPEST_AUTO_BACKUP_INTERVAL"
+ echo "Sleep for $CRUPEST_AUTO_BACKUP_INTERVAL for next backup..."
+ sleep "$CRUPEST_AUTO_BACKUP_INTERVAL"
done