Skip to content

Commit

Permalink
Add logging (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Minituff authored Oct 18, 2023
1 parent 1e0d5a7 commit addeb84
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 91 deletions.
21 changes: 12 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ RUN apk add bash rsync tzdata dos2unix jq
# Copy all necessary files into the container (from /pkg in the repository to /app in the container)
COPY pkg app

# Move the entrypoint script to the root directory for ease of access
RUN mv app/entry.sh /entry.sh
# Make the entire /app folder executable
RUN chmod +x /app

# Make the script executable
RUN chmod +x app/backup.sh && dos2unix app/backup.sh
# Make the all files in the /app folder Unix format
RUN find /app -type f -print0 | xargs -0 dos2unix

# Make the entry script executable
RUN chmod +x entry.sh && dos2unix entry.sh
# Move the entrypoint script to the root directory for ease of access
RUN mv app/entry.sh /entry.sh

# Nautical Version (for example "v0.2.1") or "main" if not set
ARG NAUTICAL_VERSION="main"
Expand All @@ -32,9 +32,6 @@ ENV REPORT_FILE="true"
# Run the backup immediately on start
ENV BACKUP_ON_START="false"

# Log each rsync command to console before running (useful for debugging)
ENV LOG_RSYNC_COMMANDS="false"

# Use the default rsync args "-raq" (recursive, archive, quiet)
ENV USE_DEFAULT_RSYNC_ARGS="true"

Expand All @@ -44,5 +41,11 @@ ENV RSYNC_CUSTOM_ARGS=""
# Require the Docker Label `nautical-backup.enable=true` to be present on each contianer or it will be skipped.
ENV REQUIRE_LABEL="false"

# Set the default log level to INFO
ENV LOG_LEVEL="INFO"

# Set the default log level for the repot file to INFO
ENV REPORT_FILE_LOG_LEVEL="INFO"

# Run the entry script and pass all variables to it
ENTRYPOINT [ "bash", "-c", "exec ./entry.sh \"${@}\"", "--"]
23 changes: 16 additions & 7 deletions docs/arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,27 @@ Will immediatly perform a backup when the container is started in addition to th
BACKUP_ON_START=true
```

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

Log each `rsync` command to console before running <small>(useful for debugging)</small>
> **Default**: INFO
> **Default**: false
> **Options**: DEBUG, INFO, WARN, ERROR
```properties
LOG_RSYNC_COMMANDS=true
LOG_LEVEL=INFO
```
You should see something like this in the Nautical contianer logs:
```console
rsync -ahq --exclude='*.log' --exclude='*.txt' /app/source/watchtower/ /app/destination/watchtower/

## Report Log Level
Set the log level for the generated report file.
Only used if the repot file is [enabled](#report-file).

> **Default**: INFO
> **Options**: DEBUG, INFO, WARN, ERROR
```properties
REPORT_FILE_LOG_LEVEL=INFO
```

## Use Default rsync Arguments
Expand Down
3 changes: 2 additions & 1 deletion docs/labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Remember, these labels can be added to any container <small> (other than Nautica
labels:
- "nautical-backup.enable=true"
- "nautical-backup.stop-before-backup=true"
- "nautical-backup.rsync-custom-args= " # Disable custom rsync args
```

=== "Docker Compose Example 2"
Expand Down Expand Up @@ -204,7 +205,7 @@ nautical-backup.rsync-custom-args=--exclude='*.log' --exclude='*.txt'

!!! note "This label will *override* the global setting applied through [Enviornment Variables](./arguments.md)"
* *Any value* will override the global rsync arguemnts configured through [global settings](./arguments.md#custom-rsync-arguments).
* A <small>(empty)</small> value of `"nautical-backup.rsync-custom-args="` will ^^cancel^^ any [global setting](./arguments.md#custom-rsync-arguments) for this container only.
* A value of <small>(space)</small> `"nautical-backup.rsync-custom-args= "` will ^^cancel^^ any [global setting](./arguments.md#custom-rsync-arguments) for this container only.
* Not setting the label value will use the [global setting](./arguments.md#custom-rsync-arguments).

<small>🔄 Not setting a label is the same action as the [Custom rsync Arguments](./arguments.md#custom-rsync-arguments) variable, but applied only to this container.</small>
85 changes: 31 additions & 54 deletions pkg/backup.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

echo "Starting backup script..."
source /app/logger.sh # Use the logger script
logThis "Starting backup..."

# Convert the string back to an array
if [ ! -z "$CONTAINER_SKIP_LIST_STR" ]; then
Expand Down Expand Up @@ -39,55 +40,31 @@ containers=$(docker ps --no-trunc --format="{{.ID}}:{{.Names}}")
number_of_containers=$(echo "$containers" | wc -l)
number_of_containers=$((number_of_containers - 1)) # Subtract 1 to exclude the container running the script

echo "Processing $number_of_containers containers..."
logThis "Processing $number_of_containers containers..."

# Define the name for the report file
report_file="Backup Report - $(date +'%Y-%m-%d').txt"

# Remove previous report files

# Initialize the current report file with a header
if [ "$REPORT_FILE" = "true" ]; then
rm -f "$DEST_LOCATION/Backup Report - "*.txt
# Initialize the current report file with a header
echo "Backup Report - $(date)" >"$DEST_LOCATION/$report_file"
fi

DEFAULT_RSYNC_ARGS="-ahq"
default_rsync_args="-ahq"
if [ "$USE_DEFAULT_RSYNC_ARGS" = "false" ]; then
logThis "Disabling default rsync arguments ($DEFAULT_RSYNC_ARGS)" "DEBUG"
default_rsync_args=""
fi

# Global variables to hold rsync arguments
custom_args=""
if [ ! -z "$RSYNC_CUSTOM_ARGS" ]; then
logThis "Adding custom rsync arguments ($RSYNC_CUSTOM_ARGS)" "DEBUG"
custom_args="$RSYNC_CUSTOM_ARGS"
fi

# Get arguments:
# -s = skips
# -d = override DEST_LOCATION
while getopts ":s:d:" opt; do
case $opt in
s) currs+=("$OPTARG") ;;
d) DEST_LOCATION=$OPTARG ;;
esac
done

# Merge the default skips with provided skips
currs=("${currs[@]}" "${SKIP_CONTAINERS[@]}")
containers_completed=0

# Assumes the container name is the exact same as the directory name
log_entry() {
local message="$1"
echo "$message"

if [ "$REPORT_FILE" = "true" ]; then
echo "$(date) - $message" >>"$DEST_LOCATION/$report_file"
fi
}

BackupContainer() {
local container=$1
Expand All @@ -96,7 +73,7 @@ BackupContainer() {
for skip in "${SKIP_STOPPING[@]}"; do
if [ "$skip" == "$container" ]; then
skip_stopping=1
log_entry "Skipping stopping of $container as it's in the SKIP_STOPPING list."
logThis "Skipping stopping of $container as it's in the SKIP_STOPPING list." "DEBUG"
break
fi
done
Expand All @@ -105,81 +82,81 @@ BackupContainer() {
labels=$(docker inspect --format '{{json .Config.Labels}}' $id)

if echo "$labels" | grep -q '"nautical-backup.stop-before-backup":"false"'; then
log_entry "Skipping stopping of $container because of label."
logThis "Skipping stopping of $container because of label." "DEBUG"
skip_stopping=1
fi

local src_dir="$SOURCE_LOCATION/$container"
if [ ! -z "${override_source_dirs[$container]}" ]; then
src_dir="$SOURCE_LOCATION/${override_source_dirs[$container]}"
log_entry "Overriding source directory for $container to ${override_source_dirs[$container]}"
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"
log_entry "Overriding source directory for $container to $new_src_dir from label"
logThis "Overriding source directory for $container to $new_src_dir from label" "DEBUG"
fi

local dest_dir="$DEST_LOCATION/$container"
if [ ! -z "${override_dest_dirs[$container]}" ]; then
dest_dir="$DEST_LOCATION/${override_dest_dirs[$container]}"
log_entry "Overriding destination directory for $container to ${override_dest_dirs[$container]}"
logThis "Overriding destination directory for $container to ${override_dest_dirs[$container]}" "DEBUG"
fi

if echo "$labels" | grep -q '"nautical-backup.override-destination-dir"'; then
new_destination_dir=$(echo "$labels" | jq -r '.["nautical-backup.override-destination-dir"]')
dest_dir="$DEST_LOCATION/$new_destination_dir"
log_entry "Overriding destination directory for $container to $new_destination_dir from label"
logThis "Overriding destination directory for $container to $new_destination_dir from label" "DEBUG"
fi

if [ -d "$src_dir" ]; then
if [ $skip_stopping -eq 0 ]; then
log_entry "Stopping $container..."
logThis "Stopping $container..."
docker stop $container 2>&1 >/dev/null
if [ $? -ne 0 ]; then
log_entry "Error stopping container $container. Skipping backup for this container."
logThis "Error stopping container $container. Skipping backup for this container." "ERROR"
return
fi
fi

if echo "$labels" | grep -q '"nautical-backup.use-default-rsync-args":"true"'; then
echo "Using default rsync args ($DEFAULT_RSYNC_ARGS) for $container"
logThis "Using default rsync args ($DEFAULT_RSYNC_ARGS) for $container" "DEBUG"
default_rsync_args=$DEFAULT_RSYNC_ARGS
elif echo "$labels" | grep -q '"nautical-backup.use-default-rsync-args":"false"'; then
echo "Not using default rsync args ($DEFAULT_RSYNC_ARGS) for $container"
logThis "Not using default rsync args ($DEFAULT_RSYNC_ARGS) for $container" "DEBUG"
default_rsync_args=""
fi

if echo "$labels" | grep -q '"nautical-backup.rsync-custom-args"'; then
new_custom_rsync_args=$(echo "$labels" | jq -r '.["nautical-backup.rsync-custom-args"]')
custom_args=$new_custom_rsync_args
echo "Using custom rsync args for $container"
logThis "Using custom rsync args for $container" "DEBUG"
fi

log_entry "Backing up $container data..."
if [ "$LOG_RSYNC_COMMANDS" = "true" ]; then
echo rsync $default_rsync_args $custom_args $src_dir/ $dest_dir/
fi
logThis "Backing up $container data..."
logThis "RUNNING: 'rsync $default_rsync_args $custom_args $src_dir/ $dest_dir/'" "DEBUG"

# Run rsync
eval rsync $default_rsync_args $custom_args $src_dir/ $dest_dir/

if [ $? -ne 0 ]; then
log_entry "Error copying data for container $container. Skipping backup for this container."
logThis "Error copying data for container $container. Skipping backup for this container." "ERROR"
fi

if [ $skip_stopping -eq 0 ]; then
log_entry "Starting $container container..."
logThis "Starting $container container..."
docker start $container 2>&1 >/dev/null
if [ $? -ne 0 ]; then
log_entry "Error restarting container $container. Please check manually!"
logThis "Error restarting container $container. Please check manually!" "ERROR"
return
fi
fi

log_entry "$container completed."
logThis "$container completed."
((containers_completed++))
else
log_entry "Directory $src_dir does not exist. Skipping"
logThis "Directory $src_dir does not exist. Skipping" "WARN"
fi
}

Expand All @@ -198,29 +175,29 @@ for entry in $containers; do
labels=$(docker inspect --format '{{json .Config.Labels}}' $id)

if echo "$labels" | grep -q '"nautical-backup.enable":"true"'; then
echo "Enabling $name based on label."
logThis "Enabling $name based on label." "DEBUG"
skip=0 # Do not skip the container
elif echo "$labels" | grep -q '"nautical-backup.enable":"false"'; then
echo "Skipping $name based on label."
logThis "Skipping $name based on label." "DEBUG"
skip=1 # Add the container to the skip list
elif [ "$REQUIRE_LABEL" = "true" ]; then
if [ "$id" != "$SELF_CONTAINER_ID" ]; then
echo "Skipping $name as 'nautical-backup.enable=true' was not found and REQUIRE_LABEL is true."
echo "Skipping $name as 'nautical-backup.enable=true' was not found and REQUIRE_LABEL is true." "DEBUG"
fi
fi

for cur in "${SKIP_CONTAINERS[@]}"; do
if [ "$cur" == "$name" ]; then
skip=1
echo "Skipping $name based on name."
echo "Skipping $name based on name." "DEBUG"
break
fi
if [ "$cur" == "$id" ]; then
skip=1
if [ "$cur" == "$SELF_CONTAINER_ID" ]; then
break # Exclude self from logs
fi
echo "Skipping $name based on ID $id."
echo "Skipping $name based on ID $id." "DEBUG"
break
fi
done
Expand All @@ -232,4 +209,4 @@ done

containers_skipped=$((number_of_containers - containers_completed))

echo "Success. $containers_completed containers backed up! $containers_skipped skipped."
logThis "Success. $containers_completed containers backed up! $containers_skipped skipped." "INFO"
Loading

0 comments on commit addeb84

Please sign in to comment.