This commit is contained in:
@ -38,6 +38,7 @@ CURRENT_EXIT_CODE=0
|
||||
load_dotenv(){
|
||||
local file_path=$1
|
||||
if [ -f "${file_path}" ]; then
|
||||
# shellcheck source=/dev/null
|
||||
source "${file_path}"
|
||||
fi
|
||||
}
|
||||
@ -69,9 +70,9 @@ function run_command() {
|
||||
load_dotenv "${service_path}/config/.template_info.env"
|
||||
|
||||
# update the main variables.
|
||||
CONFIG_PATH="${service_path}/config"
|
||||
SERVICE="${SERVICE_NAME}"
|
||||
DOCKER_CLI_HINTS=false
|
||||
export CONFIG_PATH="${service_path}/config"
|
||||
export SERVICE="${SERVICE_NAME}"
|
||||
export DOCKER_CLI_HINTS=false
|
||||
|
||||
set +a
|
||||
|
||||
|
@ -41,18 +41,18 @@ _create_and_start_container() {
|
||||
local run_cmd="$1"
|
||||
local container_name="$2"
|
||||
|
||||
if _is_container_exists $container_name; then
|
||||
_is_container_running $container_name && return 0
|
||||
_start_container $container_name
|
||||
if _is_container_exists "$container_name"; then
|
||||
_is_container_running "$container_name" && return 0
|
||||
_start_container "$container_name"
|
||||
else
|
||||
$run_cmd
|
||||
fi
|
||||
|
||||
if ! _is_container_running $container_name; then
|
||||
if ! _is_container_running "$container_name"; then
|
||||
_die "Container ${container_name} failed to start"
|
||||
fi
|
||||
|
||||
ID=$(_get_container_id $container_name)
|
||||
ID=$(_get_container_id "$container_name")
|
||||
echo "Container ${container_name} is running with ID ${ID}"
|
||||
}
|
||||
|
||||
@ -119,32 +119,32 @@ _get_container_status() {
|
||||
|
||||
# Starts an existing, stopped container.
|
||||
_start_container() {
|
||||
_is_container_exists $1 || return 1
|
||||
_is_container_running $1 && return 0
|
||||
docker start $1
|
||||
_is_container_exists "$1" || return 1
|
||||
_is_container_running "$1" && return 0
|
||||
docker start "$1"
|
||||
}
|
||||
|
||||
# Stops a running container.
|
||||
_stop_container() {
|
||||
_is_container_running $1 || return 0;
|
||||
docker stop $1
|
||||
_is_container_running "$1" || return 0;
|
||||
docker stop "$1"
|
||||
}
|
||||
|
||||
# Stops (if needed) and removes a container.
|
||||
_remove_container() {
|
||||
_stop_container $1
|
||||
_is_container_exists $1 || return 0;
|
||||
docker rm $1
|
||||
_stop_container "$1"
|
||||
_is_container_exists "$1" || return 0;
|
||||
docker rm "$1"
|
||||
}
|
||||
|
||||
# Prints the logs for a container.
|
||||
_get_container_logs() {
|
||||
if ! _is_container_exists $1; then
|
||||
if ! _is_container_exists "$1"; then
|
||||
echo "Container $1 does not exist"
|
||||
return 1
|
||||
fi
|
||||
|
||||
docker logs $1
|
||||
docker logs "$1"
|
||||
}
|
||||
|
||||
# Checks if listed environment variables are set; calls _die() if any are missing.
|
||||
|
@ -12,26 +12,26 @@ _autocommandrun_volume() {
|
||||
|
||||
case "$command" in
|
||||
create)
|
||||
if docker volume ls | grep -q ${volume_name}; then
|
||||
if docker volume ls | grep -q "${volume_name}"; then
|
||||
echo "Volume ${volume_name} already exists - leaving unchanged"
|
||||
return
|
||||
fi
|
||||
echo "Creating volume ${volume_name}"
|
||||
docker volume create ${volume_name}
|
||||
docker volume create "${volume_name}"
|
||||
;;
|
||||
destroy)
|
||||
echo "Destroying volume ${volume_name}"
|
||||
docker volume rm ${volume_name}
|
||||
docker volume rm "${volume_name}"
|
||||
;;
|
||||
backup)
|
||||
echo "Backing up volume ${volume_name}"
|
||||
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"
|
||||
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)
|
||||
echo "Restoring volume ${volume_name}"
|
||||
docker volume rm ${volume_name}
|
||||
docker volume create ${volume_name}
|
||||
docker run --rm -v ${volume_name}:/volume -v ${backup_folder}:/backup debian bash -c "tar -xzvf /backup/backup.tgz -C /volume --strip-components=1"
|
||||
docker volume rm "${volume_name}"
|
||||
docker volume create "${volume_name}"
|
||||
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
|
||||
}
|
||||
@ -48,14 +48,16 @@ _autocommandrun_path() {
|
||||
return
|
||||
fi
|
||||
echo "Creating path ${path}"
|
||||
mkdir -p ${path}
|
||||
mkdir -p "${path}"
|
||||
;;
|
||||
destroy)
|
||||
echo "Destroying path ${path}"
|
||||
local path_parent=$(dirname ${path})
|
||||
local path_child=$(basename ${path})
|
||||
local path_parent;
|
||||
path_parent=$(dirname "${path}")
|
||||
local path_child;
|
||||
path_child=$(basename "${path}")
|
||||
if [ -d "${path_parent}/${path_child}" ]; then
|
||||
docker run --rm -v ${path_parent}:/volume debian bash -c "rm -rfv /volume/${path_child}" || echo "Failed to destroy path ${path}"
|
||||
docker run --rm -v "${path_parent}":/volume debian bash -c "rm -rfv /volume/${path_child}" || echo "Failed to destroy path ${path}"
|
||||
else
|
||||
echo "Path ${path} does not exist - nothing to destroy"
|
||||
fi
|
||||
@ -63,7 +65,7 @@ _autocommandrun_path() {
|
||||
backup)
|
||||
echo "Backing up path ${path}"
|
||||
if [ -d "${path}" ]; then
|
||||
docker run --rm -v ${path}:/path -v ${backup_folder}:/backup debian bash -c "tar -czvf /backup/backup.tgz -C /path . && chown -R $MYID:$MYGRP /backup"
|
||||
docker run --rm -v "${path}":/path -v "${backup_folder}":/backup debian bash -c "tar -czvf /backup/backup.tgz -C /path . && chown -R $MYID:$MYGRP /backup"
|
||||
else
|
||||
echo "Path ${path} does not exist - nothing to backup"
|
||||
fi
|
||||
@ -73,9 +75,9 @@ _autocommandrun_path() {
|
||||
echo "Backup file ${backup_folder}/backup.tgz does not exist - nothing to restore"
|
||||
else
|
||||
echo "Clearing existing data in path ${path}"
|
||||
docker run --rm -v ${path}:/path debian bash -c "rm -rfv /path/{*,.*}"
|
||||
docker run --rm -v "${path}":/path debian bash -c "rm -rfv /path/{*,.*}"
|
||||
echo "Restoring path ${path} from backup file ${backup_folder}/backup.tgz"
|
||||
tar -xzvf ${backup_folder}/backup.tgz -C ${path} --strip-components=1
|
||||
tar -xzvf "${backup_folder}/backup.tgz" -C "${path}" --strip-components=1
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
@ -88,31 +90,36 @@ _autocommandrun_file() {
|
||||
|
||||
case "$command" in
|
||||
create)
|
||||
filepath_parent=$(dirname ${filepath})
|
||||
filepath_child=$(basename ${filepath})
|
||||
if [ ! -d "${filepath_parent}" ]; then
|
||||
echo "Parent directory ${filepath_parent} of ${filepath_child} does not exist - creating"
|
||||
mkdir -p ${filepath_parent}
|
||||
local file_parent;
|
||||
file_parent=$(dirname "${filepath}")
|
||||
local file_name;
|
||||
file_name=$(basename "${filepath}")
|
||||
if [ ! -d "${file_parent}" ]; then
|
||||
echo "Parent directory ${file_parent} of ${file_name} does not exist - creating"
|
||||
mkdir -p "${file_parent}"
|
||||
fi
|
||||
;;
|
||||
destroy)
|
||||
rm -f ${filepath}
|
||||
rm -f "${filepath}"
|
||||
;;
|
||||
backup)
|
||||
echo "Backing up file ${filepath}"
|
||||
local file_parent=$(dirname ${filepath})
|
||||
local file_name=$(basename ${filepath})
|
||||
local file_parent;
|
||||
file_parent=$(dirname "${filepath}")
|
||||
local file_name;
|
||||
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"
|
||||
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 file_name=$(basename ${filepath})
|
||||
rm -f ${filepath} || die "Unable to remove existing file ${filepath}, restore failed."
|
||||
cp ${backup_folder}/${file_name} ${filepath} || die "Unable to copy file ${backup_folder}/${file_name} to ${filepath}, restore failed."
|
||||
local file_name;
|
||||
file_name=$(basename "${filepath}")
|
||||
rm -f "${filepath}" || die "Unable to remove existing file ${filepath}, restore failed."
|
||||
cp "${backup_folder}/${file_name}" "${filepath}" || die "Unable to copy file ${backup_folder}/${file_name} to ${filepath}, restore failed."
|
||||
;;
|
||||
esac
|
||||
}
|
||||
@ -153,9 +160,10 @@ _autocommandparse() {
|
||||
local value="${pair#*=}"
|
||||
|
||||
# create backup folder unique to key/value.
|
||||
local bfolder=$(echo "${key}_${value}" | tr -cd '[:alnum:]_-')
|
||||
local bfolder;
|
||||
bfolder=$(echo "${key}_${value}" | tr -cd '[:alnum:]_-')
|
||||
local targetpath="${backup_temp_path}/${bfolder}"
|
||||
mkdir -p ${targetpath}
|
||||
mkdir -p "${targetpath}"
|
||||
|
||||
# Key must be one of volume, path or file
|
||||
case "$key" in
|
||||
@ -191,7 +199,7 @@ databackup() {
|
||||
|
||||
|
||||
mkdir -p "$BACKUP_TEMP_PATH"
|
||||
echo "_autocommandparse [backup] [$BACKUP_TEMP_PATH] [$@]"
|
||||
echo "_autocommandparse [backup] [$BACKUP_TEMP_PATH]" "$@"
|
||||
_autocommandparse backup "$BACKUP_TEMP_PATH" "$@"
|
||||
|
||||
tar zcvf "$BACKUP_FILE" -C "$BACKUP_TEMP_PATH" .
|
||||
@ -201,7 +209,7 @@ datarestore() {
|
||||
_check_required_env_vars "BACKUP_FILE" "TEMP_DIR"
|
||||
BACKUP_TEMP_PATH="$TEMP_DIR/restore"
|
||||
|
||||
echo "_autocommandparse [restore] [$BACKUP_TEMP_PATH] [$@]"
|
||||
echo "_autocommandparse [restore] [$BACKUP_TEMP_PATH]" "$@"
|
||||
|
||||
mkdir -p "$BACKUP_TEMP_PATH"
|
||||
tar zxvf "$BACKUP_FILE" -C "$BACKUP_TEMP_PATH" --strip-components=1
|
||||
|
Reference in New Issue
Block a user