.
Some checks failed
Dropshell Test / Build_and_Test (push) Failing after 19s

This commit is contained in:
Your Name 2025-05-10 20:12:58 +12:00
parent 4d6702b099
commit 535eed2ece

View File

@ -1,12 +1,10 @@
#!/bin/bash
# This script contains the common code for the autocommands.
_check_required_env_vars "BACKUP_FILE" "TEMP_DIR"
MYID=$(id -u)
MYGRP=$(id -g)
BACKUP_TEMP_PATH="$TEMP_DIR/backup"
_autocommandrun_volume() {
local command="$1"
@ -47,10 +45,10 @@ _autocommandrun_path() {
;;
nuke)
echo "Nuking path ${path}"
PATHPARENT=$(dirname ${path})
PATHCHILD=$(basename ${path})
if [ -d "${PATHPARENT}/${PATHCHILD}" ]; then
docker run --rm -v ${PATHPARENT}:/volume debian bash -c "rm -rf /volume/${PATHCHILD}" || echo "Failed to nuke path ${path}"
local path_parent=$(dirname ${path})
local path_child=$(basename ${path})
if [ -d "${path_parent}/${path_child}" ]; then
docker run --rm -v ${path_parent}:/volume debian bash -c "rm -rf /volume/${path_child}" || echo "Failed to nuke path ${path}"
else
echo "Path ${path} does not exist - nothing to nuke"
fi
@ -83,18 +81,18 @@ _autocommandrun_file() {
;;
backup)
echo "Backing up file ${filepath}"
FILEPARENT=$(dirname ${filepath})
FILENAME=$(basename ${filepath})
if [ -f "${FILEPARENT}/${FILENAME}" ]; then
docker run --rm-v ${FILEPARENT}:/volume -v ${backup_folder}:/backup debian bash -c "cp /volume/${FILENAME} /backup/${FILENAME} && chown -R $MYID:$MYGRP /backup"
local file_parent=$(dirname ${filepath})
local file_name=$(basename ${filepath})
if [ -f "${file_parent}/${file_name}" ]; then
docker run --rm -v ${file_parent}:/volume -v ${backup_folder}:/backup debian bash -c "cp /volume/${file_name} /backup/${file_name} && chown -R $MYID:$MYGRP /backup"
else
echo "File ${filepath} does not exist - nothing to backup"
fi
;;
restore)
echo "Restoring file ${filepath}"
local FILENAME=$(basename ${filepath})
cp ${backup_folder}/${FILENAME} ${filepath}
local file_name=$(basename ${filepath})
cp ${backup_folder}/${file_name} ${filepath}
;;
esac
}
@ -112,7 +110,11 @@ _autocommandparse() {
local command="$1"
shift
echo "autocommandparse: command=$command"
local backup_temp_path="$1"
shift
echo "autocommandparse: command=$command backup_temp_path=$backup_temp_path"
# Extract the backup file and temp path (last two arguments)
local args=("$@")
@ -132,7 +134,7 @@ _autocommandparse() {
# create backup folder unique to key/value.
local bfolder=$(echo "${key}_${value}" | tr -cd '[:alnum:]_-')
local targetpath="${BACKUP_TEMP_PATH}/${bfolder}"
local targetpath="${backup_temp_path}/${bfolder}"
mkdir -p ${targetpath}
# Key must be one of volume, path or file
@ -155,31 +157,36 @@ _autocommandparse() {
autocreate() {
_autocommandparse create "$@"
_autocommandparse create none "$@"
}
autonuke() {
_autocommandparse nuke "$@"
_autocommandparse nuke none "$@"
}
autobackup() {
_check_required_env_vars "BACKUP_FILE" "TEMP_DIR"
BACKUP_TEMP_PATH="$TEMP_DIR/backup"
mkdir -p "$BACKUP_TEMP_PATH"
echo "_autocommandparse [backup] [$@]"
_autocommandparse backup "$@"
echo "_autocommandparse [backup] [$BACKUP_TEMP_PATH] [$@]"
_autocommandparse backup "$BACKUP_TEMP_PATH" "$@"
tar zcvf "$BACKUP_FILE" -C "$BACKUP_TEMP_PATH" .
}
autorestore() {
echo "_autocommandparse [restore] [$@]"
_check_required_env_vars "BACKUP_FILE" "TEMP_DIR"
BACKUP_TEMP_PATH="$TEMP_DIR/restore"
echo "_autocommandparse [restore] [$BACKUP_TEMP_PATH] [$@]"
mkdir -p "$BACKUP_TEMP_PATH"
tar zxvf "$BACKUP_FILE" -C "$BACKUP_TEMP_PATH" --strip-components=1
_autocommandparse restore "$@"
_autocommandparse restore "$BACKUP_TEMP_PATH" "$@"
}