Skip to content

Commit

Permalink
Add Mirror Source Directory Name to Destination (#55)
Browse files Browse the repository at this point in the history
* Add Mirror Source Directory Name to Destination
  • Loading branch information
Minituff authored Oct 20, 2023
1 parent 30141ac commit 011311c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 27 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \"${@}\"", "--"]
16 changes: 16 additions & 0 deletions docs/arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <small>(without any [overrides](#override-source-directory))</small>, 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 <small> (which is the default) </small>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
```

<small>🔄 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.</small>

## Console Log Level
Set the console log level for the container.

Expand Down
15 changes: 15 additions & 0 deletions docs/labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,21 @@ nautical-backup.override-destination-dir=new_folder_name

<small>🔄 This is the same action as the [Override Destination Directory](./arguments.md#override-destination-directory) variable, but applied only to this container.</small>

## Mirror Source Directory Name to Destination
Mirror the source folder name to the destination folder name. By default <small>(without any [overrides](#override-source-directory-name))</small>, 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<small> (which is the default) </small>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
```

<small>🔄 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.</small>

## Use Default rsync Arguments
Use the default `rsync` arguemnts `-raq` <small>(recursive, archive, quiet)</small>

Expand Down
23 changes: 16 additions & 7 deletions pkg/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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"
Expand Down
54 changes: 34 additions & 20 deletions pkg/entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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[@]}")
Expand All @@ -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
Expand All @@ -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,..."
Expand All @@ -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

Expand All @@ -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

0 comments on commit 011311c

Please sign in to comment.