diff --git a/src/service_runner.cpp b/src/service_runner.cpp index 9acb65e..61668cb 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -491,6 +491,12 @@ bool service_runner::restore(std::string backup_file, bool silent) } std::string local_backups_dir = gConfig().get_local_backup_path(); + + if (backup_file == "latest") { + // get the latest backup file from the server + backup_file = get_latest_backup_file(mServer, mService); + } + std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_file).string(); if (! std::filesystem::exists(local_backup_file_path)) { @@ -520,14 +526,6 @@ bool service_runner::restore(std::string backup_file, bool silent) std::cout << "Restoring " << nicedate << " backup of " << backup_template_name << " taken from "<> confirm; - if (confirm != 'y') { - std::cout << "Restore cancelled." << std::endl; - return false; - } // run the restore script std::cout << "OK, here goes..." << std::endl; diff --git a/templates/dropshell-agent/shared/_autocommands.sh b/templates/dropshell-agent/shared/_autocommands.sh index 4e92163..ff19ba3 100644 --- a/templates/dropshell-agent/shared/_autocommands.sh +++ b/templates/dropshell-agent/shared/_autocommands.sh @@ -1,5 +1,7 @@ #!/bin/bash +MYID=$(id -u) +MYGRP=$(id -g) _autocommandrun_volume() { command="$1" @@ -15,16 +17,16 @@ _autocommandrun_volume() { docker volume rm ${volume_name} ;; backup) - local backup_file="$3" + local backup_folder="$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 . + docker run --rm -v ${volume_name}:/volume -v ${backup_folder}:/backup debian bash -c "tar -czvf /backup/backup.tgz -C /volume . && chown -R $MYID:$MYGRP /backup" ;; restore) - local backup_file="$3" + local backup_folder="$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 + docker run --rm -v ${volume_name}:/volume -v ${backup_folder}:/backup debian bash -c "tar -xzvf /backup/backup.tgz -C /volume --strip-components=1" ;; esac } @@ -43,14 +45,14 @@ _autocommandrun_path() { rm -rf ${path} ;; backup) - local backup_file="$3" + local backup_folder="$3" echo "Backing up path ${path}" - tar -czvf ${backup_file} -C ${path} . + tar -czvf ${backup_folder}/backup.tgz -C ${path} . ;; restore) - local backup_file="$3" + local backup_folder="$3" echo "Restoring path ${path}" - tar -xzvf ${backup_file} -C ${path} --strip-components=1 + tar -xzvf ${backup_folder}/backup.tgz -C ${path} --strip-components=1 ;; esac } @@ -66,14 +68,17 @@ _autocommandrun_file() { rm -f ${value} ;; backup) - local backup_file="$3" + local backup_folder="$3" echo "Backing up file ${value}" - cp ${value} ${backup_file} + # get filename from path + local filename=$(basename ${value}) + cp ${value} ${backup_folder}/${filename} ;; restore) - local backup_file="$3" + local backup_folder="$3" echo "Restoring file ${value}" - cp ${backup_file} ${value} + local filename=$(basename ${value}) + cp ${backup_folder}/${filename} ${value} ;; esac } @@ -92,9 +97,11 @@ _autocommandparse() { local command="$1" shift - local temp_path="$2" + local temp_path="$1" shift + echo "autocommandparse: command=$command temp_path=$temp_path" + # Extract the backup file and temp path (last two arguments) local args=("$@") local arg_count=${#args[@]} @@ -111,18 +118,27 @@ _autocommandparse() { local key="${pair%%=*}" local value="${pair#*=}" - local bfile="${temp_path}/${key}_${value}.tgz" + # create backup folder unique to key/value. + local bfolder="${key}_${value}" + + # remove any non-alphanumeric characters, that aren't dash or underscore from the bfile + targetpath="" + if [ ! "$temp_path" == "-" ]; then + bfolder=$(echo "$bfolder" | tr -cd '[:alnum:]_-') + mkdir -p ${temp_path}/${bfolder} + targetpath="${temp_path}/${bfolder}" + fi # Key must be one of volume, path or file case "$key" in volume) - _autocommandrun_volume "$command" "$value" "$bfile" + _autocommandrun_volume "$command" "$value" "$targetpath" ;; path) - _autocommandrun_path "$command" "$value" "$bfile" + _autocommandrun_path "$command" "$value" "$targetpath" ;; file) - _autocommandrun_file "$command" "$value" "$bfile" + _autocommandrun_file "$command" "$value" "$targetpath" ;; *) _die "Unknown key $key passed to auto${command}. We only support volume, path and file." @@ -148,12 +164,13 @@ autobackup() { local temp_path="$1" shift - [ -f "$backup_file" ] || _die "Backup file $backup_file does not exist" + [ -f "$backup_file" ] && _die "Backup file $backup_file already exists" [ -d "$temp_path" ] || _die "Temp path $temp_path does not exist" local backup_temp_path="$temp_path/backup" mkdir -p "$backup_temp_path" + echo "_autocommandparse [backup] [$backup_temp_path] [$@]" _autocommandparse backup "$backup_temp_path" "$@" tar zcvf "$backup_file" -C "$backup_temp_path" . diff --git a/templates/test_template.sh b/templates/test_template.sh index 38c0e7c..4a873e6 100755 --- a/templates/test_template.sh +++ b/templates/test_template.sh @@ -48,7 +48,7 @@ title "Backing up service" ds backup localhost $SERVICE_NAME || die "Failed to backup service" title "Restoring service" -ds restore localhost $SERVICE_NAME || die "Failed to restore service" +ds restore localhost $SERVICE_NAME latest || die "Failed to restore service" title "Checking status" ds status localhost $SERVICE_NAME || die "Failed to check status"