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