diff --git a/templates/caddy/backup.sh b/templates/caddy/backup.sh index 9d38bcf..810b024 100644 --- a/templates/caddy/backup.sh +++ b/templates/caddy/backup.sh @@ -4,9 +4,7 @@ _check_required_env_vars _stop_container "$CONTAINER_NAME" -if ! autobackup volume=$DATA_VOLUME volume=$CONFIG_VOLUME $1 $2; then - _die "Failed to create backup" -fi +autobackup "$1" "$2" "volume=$DATA_VOLUME" "volume=$CONFIG_VOLUME" || _die "Failed to create backup" _start_container "$CONTAINER_NAME" diff --git a/templates/caddy/install.sh b/templates/caddy/install.sh index 706067d..1ad9eba 100644 --- a/templates/caddy/install.sh +++ b/templates/caddy/install.sh @@ -2,9 +2,7 @@ source "${AGENT_PATH}/_common.sh" _check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "DATA_VOLUME" "CONFIG_VOLUME" "CONFIG_PATH" -if ! autocreate volume=$DATA_VOLUME volume=$CONFIG_VOLUME; then - _die "Failed to autocreate volumes and paths" -fi +autocreate "volume=$DATA_VOLUME" "volume=$CONFIG_VOLUME" || _die "Failed to autocreate volumes $DATA_VOLUME and $CONFIG_VOLUME" _check_docker_installed || _die "Docker test failed, aborting installation..." diff --git a/templates/caddy/nuke.sh b/templates/caddy/nuke.sh index 02f0814..5495b27 100644 --- a/templates/caddy/nuke.sh +++ b/templates/caddy/nuke.sh @@ -8,8 +8,6 @@ _check_required_env_vars # any docker volumes and any custom local data folders. -if ! autonuke volume=$DATA_VOLUME volume=$CONFIG_VOLUME; then - _die "Failed to nuke" -fi +autonuke "volume=$DATA_VOLUME" "volume=$CONFIG_VOLUME" || _die "Failed to nuke" echo "Nuking of ${CONTAINER_NAME} complete." diff --git a/templates/caddy/restore.sh b/templates/caddy/restore.sh index cfc10ba..e5c7993 100644 --- a/templates/caddy/restore.sh +++ b/templates/caddy/restore.sh @@ -9,9 +9,7 @@ _check_required_env_vars bash ./uninstall.sh || _die "Failed to uninstall service before restore" # restore data from backup file -if ! autorestore volume=$DATA_VOLUME volume=$CONFIG_VOLUME "$1" "$2"; then - _die "Failed to restore data from backup file" -fi +autorestore "$1" "$2" "volume=$DATA_VOLUME" "volume=$CONFIG_VOLUME" || _die "Failed to restore data from backup file" # reinstall service bash ./install.sh || _die "Failed to reinstall service after restore" diff --git a/templates/dropshell-agent/shared/_autocommands.sh b/templates/dropshell-agent/shared/_autocommands.sh index bb2e36f..4e92163 100644 --- a/templates/dropshell-agent/shared/_autocommands.sh +++ b/templates/dropshell-agent/shared/_autocommands.sh @@ -3,49 +3,77 @@ _autocommandrun_volume() { command="$1" - value="$2" - backup_file="$3" - temp_path="$4" + volume_name="$2" + + case "$command" in + create) + echo "Creating volume ${volume_name}" + docker volume create ${volume_name} + ;; + nuke) + echo "Nuking volume ${volume_name}" + docker volume rm ${volume_name} + ;; + backup) + local backup_file="$3" + echo "Backing up volume ${volume_name}" + docker run --rm -v ${volume_name}:/volume -v ${temp_path}:/backup alpine tar -czvf /backup/volume.tar.gz -C /volume . + ;; + restore) + local backup_file="$3" + echo "Restoring volume ${volume_name}" + docker volume rm ${volume_name} + docker volume create ${volume_name} + docker run --rm -v ${volume_name}:/volume -v ${temp_path}:/backup alpine tar -xzvf /backup/volume.tar.gz -C /volume --strip-components=1 + ;; + esac } _autocommandrun_path() { command="$1" - value="$2" - backup_file="$3" - temp_path="$4" + path="$2" + + case "$command" in + create) + echo "Creating path ${path}" + mkdir -p ${path} + ;; + nuke) + echo "Nuking path ${path}" + rm -rf ${path} + ;; + backup) + local backup_file="$3" + echo "Backing up path ${path}" + tar -czvf ${backup_file} -C ${path} . + ;; + restore) + local backup_file="$3" + echo "Restoring path ${path}" + tar -xzvf ${backup_file} -C ${path} --strip-components=1 + ;; + esac } _autocommandrun_file() { command="$1" value="$2" - backup_file="$3" - temp_path="$4" -} -_autocommandrun() { - command="$1" - key="$2" - value="$3" - - # only passed through if command is backup or restore. - backup_file="$4" - temp_path="$5" - - case "$key" in - volume) - echo "Volume: $value" - _autocommandrun_volume "$command" "$value" "$backup_file" "$temp_path" + case "$command" in + create) ;; - path) - echo "Path: $value" - _autocommandrun_path "$command" "$value" "$backup_file" "$temp_path" + nuke) + rm -f ${value} ;; - file) - echo "File: $value" - _autocommandrun_file "$command" "$value" "$backup_file" "$temp_path" + backup) + local backup_file="$3" + echo "Backing up file ${value}" + cp ${value} ${backup_file} ;; - *) - _die "Unknown key $key passed to auto${command}. We only support volume, path and file." + restore) + local backup_file="$3" + echo "Restoring file ${value}" + cp ${backup_file} ${value} ;; esac } @@ -59,19 +87,20 @@ _autocommandparse() { # value is the path or volume name. # we iterate over the key=value arguments, and for each we call: - # autorun + # autorun local command="$1" shift + local temp_path="$2" + shift + # Extract the backup file and temp path (last two arguments) local args=("$@") local arg_count=${#args[@]} - local backup_file="${args[$arg_count-2]}" - local temp_path="${args[$arg_count-1]}" # Process all key=value pairs - for ((i=0; i<$arg_count-2; i++)); do + for ((i=0; i<$arg_count; i++)); do local pair="${args[$i]}" # Skip if not in key=value format @@ -81,30 +110,71 @@ _autocommandparse() { local key="${pair%%=*}" local value="${pair#*=}" - + + local bfile="${temp_path}/${key}_${value}.tgz" + # Key must be one of volume, path or file - _autocommandrun "$command" "$key" "$value" "$backup_file" "$temp_path" + case "$key" in + volume) + _autocommandrun_volume "$command" "$value" "$bfile" + ;; + path) + _autocommandrun_path "$command" "$value" "$bfile" + ;; + file) + _autocommandrun_file "$command" "$value" "$bfile" + ;; + *) + _die "Unknown key $key passed to auto${command}. We only support volume, path and file." + ;; + esac done } autocreate() { - _autocommandparse create "$@" "-" "-" + _autocommandparse create "-" "$@" } autonuke() { - _autocommandparse nuke "$@" "-" "-" + _autocommandparse nuke "-" "$@" } autobackup() { - _autocommandparse backup "$@" + local backup_file="$1" + shift + local temp_path="$1" + shift + + [ -f "$backup_file" ] || _die "Backup file $backup_file does not exist" + [ -d "$temp_path" ] || _die "Temp path $temp_path does not exist" + + local backup_temp_path="$temp_path/backup" + + mkdir -p "$backup_temp_path" + _autocommandparse backup "$backup_temp_path" "$@" + + tar zcvf "$backup_file" -C "$backup_temp_path" . } autorestore() { - _autocommandparse restore "$@" + local backup_file="$1" + shift + local temp_path="$1" + shift + + [ -f "$backup_file" ] || _die "Backup file $backup_file does not exist" + [ -d "$temp_path" ] || _die "Temp path $temp_path does not exist" + + local restore_temp_path="$temp_path/restore" + + mkdir -p "$restore_temp_path" + tar zxvf "$backup_file" -C "$restore_temp_path" --strip-components=1 + + _autocommandparse restore "$restore_temp_path" "$@" } diff --git a/templates/example-nginx/backup.sh b/templates/example-nginx/backup.sh index 104ce1f..46e9593 100644 --- a/templates/example-nginx/backup.sh +++ b/templates/example-nginx/backup.sh @@ -5,6 +5,6 @@ _check_required_env_vars "LOCAL_DATA_FOLDER" # Nginx Example Backup Script # hot backup is fine for nginx website content. -autobackup "path=${LOCAL_DATA_FOLDER}" $1 $2 || _die "Failed to create backup" +autobackup "$1" "$2" "path=${LOCAL_DATA_FOLDER}" || _die "Failed to create backup" echo "Backup complete" diff --git a/templates/example-nginx/install.sh b/templates/example-nginx/install.sh index e93687b..d0270a9 100644 --- a/templates/example-nginx/install.sh +++ b/templates/example-nginx/install.sh @@ -5,11 +5,7 @@ _check_required_env_vars "LOCAL_DATA_FOLDER" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAG # Nginx Example Install Script # Ensure local data folder exists -if [ ! -d "${LOCAL_DATA_FOLDER}" ]; then - echo "Creating local data folder ${LOCAL_DATA_FOLDER}..." - mkdir -p "${LOCAL_DATA_FOLDER}" - chmod 777 "${LOCAL_DATA_FOLDER}" -fi +autocreate "path=${LOCAL_DATA_FOLDER}" echo "Checking Docker installation..." _check_docker_installed || _die "Docker test failed, aborting installation..." diff --git a/templates/example-nginx/nuke.sh b/templates/example-nginx/nuke.sh index dddb809..09ae539 100644 --- a/templates/example-nginx/nuke.sh +++ b/templates/example-nginx/nuke.sh @@ -8,7 +8,6 @@ _check_required_env_vars "LOCAL_DATA_FOLDER" "CONTAINER_NAME" # Call uninstall script first ./uninstall.sh -echo "Removing local data folder ${LOCAL_DATA_FOLDER}..." -rm -rf $LOCAL_DATA_FOLDER || _die "Failed to remove local data folder ${LOCAL_DATA_FOLDER}" +autonuke "path=${LOCAL_DATA_FOLDER}" || _die "Failed to nuke ${LOCAL_DATA_FOLDER}" echo "Nuke complete for service ${CONTAINER_NAME}." diff --git a/templates/example-nginx/restore.sh b/templates/example-nginx/restore.sh index c6f6902..fca14ee 100644 --- a/templates/example-nginx/restore.sh +++ b/templates/example-nginx/restore.sh @@ -9,7 +9,7 @@ BACKUP_FILE="$1" echo "Uninstalling service before restore..." bash ./uninstall.sh || _die "Failed to uninstall service before restore" -autorestore "path=${LOCAL_DATA_FOLDER}" $1 $2 || _die "Failed to restore data folder from backup" +autorestore "$1" "$2" "path=${LOCAL_DATA_FOLDER}" || _die "Failed to restore data folder from backup" echo "Restore complete. Reinstalling service..." bash ./install.sh || _die "Failed to reinstall service after restore" diff --git a/templates/simple-object-storage/backup.sh b/templates/simple-object-storage/backup.sh index 5d7f145..56dad4c 100644 --- a/templates/simple-object-storage/backup.sh +++ b/templates/simple-object-storage/backup.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -_check_required_env_vars +_check_required_env_vars "VOLUME_NAME" # Simple Object Storage Backup Script # Creates a backup tarball of the volume contents. @@ -8,6 +8,6 @@ _check_required_env_vars # HOT backup is fine for simple-object-storage -autobackup "volume=${VOLUME_NAME}" $1 $2 || _die "Failed to create backup" +autobackup "$1" "$2" "volume=${VOLUME_NAME}" || _die "Failed to create backup" echo "Backup complete: ${BACKUP_FILE}" diff --git a/templates/simple-object-storage/install.sh b/templates/simple-object-storage/install.sh index cb79fff..f0fc364 100644 --- a/templates/simple-object-storage/install.sh +++ b/templates/simple-object-storage/install.sh @@ -5,8 +5,6 @@ _check_required_env_vars # Simple Object Storage Install Script # Pulls image, creates volume/config, starts container. - - autocreate "volume=${VOLUME_NAME}" # check can pull image on remote host and exit if fails diff --git a/templates/simple-object-storage/nuke.sh b/templates/simple-object-storage/nuke.sh index 00053df..9caebee 100644 --- a/templates/simple-object-storage/nuke.sh +++ b/templates/simple-object-storage/nuke.sh @@ -6,4 +6,6 @@ _check_required_env_vars # Removes container AND volume. -autonuke "volume=${VOLUME_NAME}" +autonuke "volume=${VOLUME_NAME}" || _die "Failed to nuke volume ${VOLUME_NAME}" + +echo "Nuke complete for service ${CONTAINER_NAME}." diff --git a/templates/simple-object-storage/restore.sh b/templates/simple-object-storage/restore.sh index 01d0989..20c47ec 100644 --- a/templates/simple-object-storage/restore.sh +++ b/templates/simple-object-storage/restore.sh @@ -12,9 +12,7 @@ bash ./uninstall.sh || _die "Failed to uninstall service before restore" echo "Restoring data for ${CONTAINER_NAME} from ${BACKUP_FILE}..." -if ! autorestore "volume=${VOLUME_NAME}" "$1" "$2"; then - _die "Failed to restore data from backup file" -fi +autorestore "$1" "$2" "volume=${VOLUME_NAME}" || _die "Failed to restore data from backup file" echo "Restore complete. Reinstalling service..." diff --git a/templates/squashkiwi/backup.sh b/templates/squashkiwi/backup.sh index e3b4a8d..028fdad 100644 --- a/templates/squashkiwi/backup.sh +++ b/templates/squashkiwi/backup.sh @@ -5,7 +5,7 @@ _check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" # Stop container before backup _stop_container "$CONTAINER_NAME" -autobackup "path=${LOCAL_DATA_FOLDER}" $1 $2 || _die "Failed to create backup" +autobackup "$1" "$2" "path=${LOCAL_DATA_FOLDER}" || _die "Failed to create backup" # Start container after backup _start_container "$CONTAINER_NAME" diff --git a/templates/squashkiwi/nuke.sh b/templates/squashkiwi/nuke.sh index f70d2fa..d3e4cc0 100644 --- a/templates/squashkiwi/nuke.sh +++ b/templates/squashkiwi/nuke.sh @@ -2,10 +2,7 @@ source "${AGENT_PATH}/_common.sh" _check_required_env_vars "LOCAL_DATA_FOLDER" -# remove the local data folder -if [ -d "${LOCAL_DATA_FOLDER}" ]; then - rm -rf ${LOCAL_DATA_FOLDER} -fi +autonuke "path=${LOCAL_DATA_FOLDER}" || _die "Failed to nuke ${LOCAL_DATA_FOLDER}" echo "Nuke of ${CONTAINER_NAME} complete" diff --git a/templates/squashkiwi/restore.sh b/templates/squashkiwi/restore.sh index 78744ca..2623a49 100644 --- a/templates/squashkiwi/restore.sh +++ b/templates/squashkiwi/restore.sh @@ -10,7 +10,7 @@ _check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" # # Stop container before backup bash ./uninstall.sh || _die "Failed to uninstall service before restore" -autorestore "path=${LOCAL_DATA_FOLDER}" $1 $2 || _die "Failed to restore data folder from backup" +autorestore "$1" "$2" "path=${LOCAL_DATA_FOLDER}" || _die "Failed to restore data folder from backup" # reinstall service bash ./install.sh || _die "Failed to reinstall service after restore"