From 011311cdcbe50a4d8e4a84c9ea889a3a672b5482 Mon Sep 17 00:00:00 2001
From: James Tufarelli <8152401+Minituff@users.noreply.github.com>
Date: Fri, 20 Oct 2023 10:56:51 -0700
Subject: [PATCH] Add Mirror Source Directory Name to Destination (#55)
* Add Mirror Source Directory Name to Destination
---
Dockerfile | 4 ++++
docs/arguments.md | 16 ++++++++++++++
docs/labels.md | 15 +++++++++++++
pkg/backup.sh | 23 ++++++++++++++------
pkg/entry.sh | 54 +++++++++++++++++++++++++++++------------------
5 files changed, 85 insertions(+), 27 deletions(-)
diff --git a/Dockerfile b/Dockerfile
index 22a3045e..a44eeb87 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -50,5 +50,9 @@ ENV REPORT_FILE_LOG_LEVEL="INFO"
# Only write to the report file when backups run, not on initialization
ENV REPORT_FILE_ON_BACKUP_ONLY="true"
+# Mirrior the source directory name to the destination directory name
+# When true, and an source dir override is applied, then the destination directory will be same same as the new source directory
+ENV KEEP_SRC_DIR_NAME="true"
+
# Run the entry script and pass all variables to it
ENTRYPOINT [ "bash", "-c", "exec ./entry.sh \"${@}\"", "--"]
diff --git a/docs/arguments.md b/docs/arguments.md
index 7041526a..729297d9 100644
--- a/docs/arguments.md
+++ b/docs/arguments.md
@@ -165,6 +165,22 @@ Will immediatly perform a backup when the container is started in addition to th
BACKUP_ON_START=true
```
+
+## Mirror Source Directory Name to Destination
+Mirror the source folder name to the destination folder name. By default (without any [overrides](#override-source-directory)), this means both the `source` and `destination` folder names are the ^^same as the container name^^.
+
+When using a [source directory override](#override-source-directory), then the `KEEP_SRC_DIR_NAME=true` setting (which is the default) will mean the destination directory will be the same as the source directory, without using a [destination directory override](#override-destination-directory).
+
+If a [destination directory override](#override-destination-directory) is applied for a container, then the override ^^will^^ be used instead of mirrioring the source name, regardless of the `KEEP_SRC_DIR_NAME` setting.
+
+> **Default**: true
+
+```properties
+KEEP_SRC_DIR_NAME=false
+```
+
+🔄 This is the same action as the [Mirror Source Directory Name to Destination](./labels.md#mirror-source-directory-name-to-desitination) label, but applied globally.
+
## Console Log Level
Set the console log level for the container.
diff --git a/docs/labels.md b/docs/labels.md
index 0b6718c4..18f1d493 100644
--- a/docs/labels.md
+++ b/docs/labels.md
@@ -176,6 +176,21 @@ nautical-backup.override-destination-dir=new_folder_name
🔄 This is the same action as the [Override Destination Directory](./arguments.md#override-destination-directory) variable, but applied only to this container.
+## Mirror Source Directory Name to Destination
+Mirror the source folder name to the destination folder name. By default (without any [overrides](#override-source-directory-name)), this means both the `source` and `destination` folder names are the ^^same as the container name^^.
+
+When using a [source directory override](#override-source-directory-name), then the `nautical-backup.keep_src_dir_name=true` setting (which is the default) will mean the destination directory will be the same as the source directory, without using a [destination directory overrides](#override-destination-directory-name).
+
+If a [destination directory override](#override-destination-directory-name) is applied for a container, then the override ^^will^^ be used instead of mirrioring the source name, regardless of the `KEEP_SRC_DIR_NAME` setting.
+
+> **Default If Missing**: true
+
+```properties
+nautical-backup.keep_src_dir_name=false
+```
+
+🔄 This is the same action as the [Mirror Source Directory Name to Destination](./arguments.md#mirror-source-directory-name-to-desitination) variable, but applied only to this container.
+
## Use Default rsync Arguments
Use the default `rsync` arguemnts `-raq` (recursive, archive, quiet)
diff --git a/pkg/backup.sh b/pkg/backup.sh
index 9b2b2066..f69c5a12 100644
--- a/pkg/backup.sh
+++ b/pkg/backup.sh
@@ -45,7 +45,6 @@ logThis "Processing $number_of_containers containers..."
# Define the name for the report file
report_file="Backup Report - $(date +'%Y-%m-%d').txt"
-
DEFAULT_RSYNC_ARGS="-ahq"
default_rsync_args="-ahq"
if [ "$USE_DEFAULT_RSYNC_ARGS" = "false" ]; then
@@ -60,12 +59,10 @@ if [ ! -z "$RSYNC_CUSTOM_ARGS" ]; then
custom_args="$RSYNC_CUSTOM_ARGS"
fi
-
# Merge the default skips with provided skips
currs=("${currs[@]}" "${SKIP_CONTAINERS[@]}")
containers_completed=0
-
BackupContainer() {
local container=$1
@@ -87,18 +84,30 @@ BackupContainer() {
fi
local src_dir="$SOURCE_LOCATION/$container"
+ local src_name="$container"
if [ ! -z "${override_source_dirs[$container]}" ]; then
src_dir="$SOURCE_LOCATION/${override_source_dirs[$container]}"
+ src_name="${override_source_dirs[$container]}" # Override source name
logThis "Overriding source directory for $container to ${override_source_dirs[$container]}" "DEBUG"
fi
if echo "$labels" | grep -q '"nautical-backup.override-source-dir"'; then
- new_src_dir=$(echo "$labels" | jq -r '.["nautical-backup.override-source-dir"]')
- src_dir="$SOURCE_LOCATION/$new_src_dir"
- logThis "Overriding source directory for $container to $new_src_dir from label" "DEBUG"
+ src_name=$(echo "$labels" | jq -r '.["nautical-backup.override-source-dir"]') # Override source name
+ src_dir="$SOURCE_LOCATION/$src_name"
+ logThis "Overriding source directory for $container to $src_name from label" "DEBUG"
+ fi
+
+ # Start with the source directory as as the destination directory (unless overridden or disabled)
+ local dest_dir="$DEST_LOCATION/${src_name}"
+
+ if [ "$KEEP_SRC_DIR_NAME" = "false" ] || echo "$labels" | grep -q '"nautical-backup.keep_src_dir_name":"false"'; then
+ logThis "Setting destination directory for $container back to container name" "DEBUG"
+ dest_dir="$DEST_LOCATION/$container" # Set back to container name
+ fi
+ if echo "$labels" | grep -q '"nautical-backup.keep_src_dir_name":"true"'; then
+ dest_dir="$DEST_LOCATION/${src_name}"
fi
- local dest_dir="$DEST_LOCATION/$container"
if [ ! -z "${override_dest_dirs[$container]}" ]; then
dest_dir="$DEST_LOCATION/${override_dest_dirs[$container]}"
logThis "Overriding destination directory for $container to ${override_dest_dirs[$container]}" "DEBUG"
diff --git a/pkg/entry.sh b/pkg/entry.sh
index 498af823..c7f06b59 100644
--- a/pkg/entry.sh
+++ b/pkg/entry.sh
@@ -10,22 +10,21 @@ logThis "Using log level: $LOG_LEVEL" "DEBUG" "init"
logThis "Installing CRON schedule: $CRON_SCHEDULE in TZ: $TZ" "DEBUG" "init"
# Dump the current cron jobs to a temporary file
-crontab -l > tempcron
+crontab -l >tempcron
# Remove the existing cron job for your backup script from the file
sed -i '/\/app\/backup.sh/d' tempcron
# Add the new cron job to the file
-echo "$CRON_SCHEDULE bash /app/backup.sh" >> tempcron
+echo "$CRON_SCHEDULE bash /app/backup.sh" >>tempcron
# Install the new cron jobs
crontab tempcron
rm tempcron
# Variables
-export SOURCE_LOCATION=/app/source # Do not include a trailing slash
-export DEST_LOCATION=/app/destination # Do not include a trailing slash
-
+export SOURCE_LOCATION=/app/source # Do not include a trailing slash
+export DEST_LOCATION=/app/destination # Do not include a trailing slash
logThis "Verifying source directory..." "DEBUG" "init"
if [ ! -d "$SOURCE_LOCATION" ]; then
@@ -48,19 +47,19 @@ elif [ ! -w "$DEST_LOCATION" ]; then
exit 1
fi
-CONTAINER_SKIP_LIST=() # Containers to skips
+CONTAINER_SKIP_LIST=() # Containers to skips
# Function to populate the skip list array
process_csv() {
- local -n skip_list_ref=$1 # Use nameref to update the array passed as argument
- local skip_var=$2 # The environment variable containing the skip list
+ local -n skip_list_ref=$1 # Use nameref to update the array passed as argument
+ local skip_var=$2 # The environment variable containing the skip list
if [ ! -z "$skip_var" ]; then
# Remove quotes and leading/trailing whitespaces
local cleaned_skip_var=$(echo "$skip_var" | sed "s/'//g;s/\"//g" | tr -d ' ')
-
+
# Split by commas into an array
- IFS=',' read -ra ADDITIONAL_SKIPS <<< "$cleaned_skip_var"
+ IFS=',' read -ra ADDITIONAL_SKIPS <<<"$cleaned_skip_var"
# Add to the existing skip list
skip_list_ref=("${skip_list_ref[@]}" "${ADDITIONAL_SKIPS[@]}")
@@ -75,7 +74,6 @@ SKIP_STOPPING_LIST=()
process_csv CONTAINER_SKIP_LIST "$SKIP_CONTAINERS"
process_csv SKIP_STOPPING_LIST "$SKIP_STOPPING"
-
if [ ! -z "$SKIP_CONTAINERS" ]; then
logThis "SKIP_CONTAINERS: ${CONTAINER_SKIP_LIST[@]}" "DEBUG" "init"
fi
@@ -89,10 +87,16 @@ export SELF_CONTAINER_ID=$(cat /proc/self/cgroup | grep 'docker' | sed 's/^.*\//
# Add the self container ID to the default skips
CONTAINER_SKIP_LIST+=("$SELF_CONTAINER_ID")
-CONTAINER_SKIP_LIST_STR=$(IFS=,; echo "${CONTAINER_SKIP_LIST[*]}") # Convert the array to a string
+CONTAINER_SKIP_LIST_STR=$(
+ IFS=,
+ echo "${CONTAINER_SKIP_LIST[*]}"
+) # Convert the array to a string
export CONTAINER_SKIP_LIST_STR # Export the string
-SKIP_STOPPING_STR=$(IFS=,; echo "${SKIP_STOPPING[*]}") # Convert the array to a string
+SKIP_STOPPING_STR=$(
+ IFS=,
+ echo "${SKIP_STOPPING[*]}"
+) # Convert the array to a string
export SKIP_STOPPING_STR # Export the string
# Assuming OVERRIDE_SOURCE_DIR is passed as an environment variable in the format "container1:dir1,container2:dir2,..."
@@ -107,7 +111,7 @@ if [ ! -z "$OVERRIDE_DEST_DIR" ]; then
fi
export OVERRIDE_DEST_DIR
-if [ "$REPORT_FILE" = "false" ]; then
+if [ ! -z "$REPORT_FILE" ]; then
logThis "REPORT_FILE: $REPORT_FILE" "DEBUG" "init"
fi
@@ -116,26 +120,36 @@ if [ ! -z "$RSYNC_CUSTOM_ARGS" ]; then
logThis "RSYNC_CUSTOM_ARGS: $RSYNC_CUSTOM_ARGS" "DEBUG" "init"
fi
-if [ "$LOG_RSYNC_COMMANDS" = "true" ]; then
+if [ ! -z "$LOG_RSYNC_COMMANDS" ]; then
logThis "LOG_RSYNC_COMMANDS: $LOG_RSYNC_COMMANDS" "DEBUG" "init"
fi
-if [ "$USE_DEFAULT_RSYNC_ARGS" = "false" ]; then
+if [ ! -z "$USE_DEFAULT_RSYNC_ARGS" ]; then
logThis "USE_DEFAULT_RSYNC_ARGS: $USE_DEFAULT_RSYNC_ARGS" "DEBUG" "init"
fi
-if [ "$REQUIRE_LABEL" = "true" ]; then
+if [ ! -z "$REQUIRE_LABEL" ]; then
logThis "REQUIRE_LABEL: $REQUIRE_LABEL" "DEBUG" "init"
fi
-if [ "$BACKUP_ON_START" = "true" ]; then
+if [ ! -z "$KEEP_SRC_DIR_NAME" ]; then
+ logThis "KEEP_SRC_DIR_NAME: $KEEP_SRC_DIR_NAME" "DEBUG" "init"
+fi
+
+if [ ! -z "$LOG_LEVEL" ]; then
+ logThis "LOG_LEVEL: $LOG_LEVEL" "DEBUG" "init"
+fi
+
+if [ ! -z "$REPORT_FILE_LOG_LEVEL" ]; then
+ logThis "REPORT_FILE_LOG_LEVEL: $REPORT_FILE_LOG_LEVEL" "DEBUG" "init"
+fi
+
+if [ ! -z "$BACKUP_ON_START" ]; then
logThis "BACKUP_ON_START: $BACKUP_ON_START" "DEBUG" "init"
bash ./app/backup.sh
fi
logThis "Initialization complete. Awaiting CRON schedule: $CRON_SCHEDULE" "INFO" "init"
-
-
# Start cron and keep container running
/usr/sbin/crond -f -l 8