#!/bin/bash
# {{ ansible_managed }}

LOG_FILE="/var/log/rclone/rclone-$(date +%Y-%m-%d).log"

{% if backups is defined and 'containers' in backups %}
{% if containers is defined %}
{% for container in containers %}
{% if container.volumes is defined %}
{% for volume in container.volumes %}
{% if volume|regex_replace(':.*') not in backups_ignore %}
echo "Stopping '{{ container.name }}' service..." | ts >> "$LOG_FILE"
systemctl stop {{ container.name }}
echo "Service '{{ container.name }}' stopped." | ts >> "$LOG_FILE"
echo "Starting backup of '{{ container.name }}:{{ volume | regex_replace(':.*') }}'." | ts >> "$LOG_FILE"
/usr/bin/rclone copy {{ volume | regex_replace(':.*') }}/ b2-container-data:current/{{ ansible_hostname }}/{{ container.name }}/{{ volume | regex_replace(':.*') | basename }} \
      --backup-dir=b2-container-data:old/{{ ansible_hostname }}/{{ container.name }}/{{ volume | regex_replace(':.*') | basename }} \
      --suffix=-$(date +%Y-%m-%d) \
      --fast-list \
      --suffix-keep-extension \
      --exclude "*log" \
      --log-file="$LOG_FILE" -v
if [[ $? > 0 ]]; then
  curl \
    -H "Title: rclone backup error" \
    -H "Priority: urgent" \
    -H "Tags: warning" \
    -d "Backup of {{ container.name }}:{{ volume | regex_replace(':.*') }} on $(hostname) had an error. Please check logs at $LOG_FILE" \
    https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
fi
echo "Finished backup of '{{ container.name }}:{{ volume | regex_replace(':.*') }}'." | ts >> "$LOG_FILE"
echo "Starting '{{ container.name }}' service..." | ts >> "$LOG_FILE"
systemctl start {{ container.name }}
echo "Service '{{ container.name }}' started." | ts >> "$LOG_FILE"

{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %} 
{% endif %}


{% if ansible_hostname == 'tvheadend' %}
## Photos 
PHOTOS_LOG="/tmp/b2-photos.log"
> $PHOTOS_LOG
echo "Starting backup of photos..." | ts >> "$LOG_FILE"
#/usr/bin/rclone copy /mnt/nas/private/Photos b2-photos:current --backup-dir=b2-photos:old --suffix=-$(date +%Y-%m-%d) --fast-list --suffix-keep-extension --exclude "*DS_Store" --log-file=$PHOTOS_LOG | ts | tee -a "$LOG_FILE"
/usr/bin/rclone copy /mnt/nas/private/Photos b2-photos:current --fast-list --immutable --exclude "*DS_Store" --log-file=$PHOTOS_LOG -v | ts | tee -a "$LOG_FILE"

if [[ $? > 0 ]]; then
  curl \
    -H "Title: rclone backup error" \
    -H "Priority: urgent" \
    -H "Tags: warning" \
    -d "Backup of Photos on $(hostname) had an error. Please check logs at $LOG_DIR/$(basename -- $PHOTOS_LOG)" \
    https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
fi

mv "$PHOTOS_LOG" "$LOG_DIR"

echo "Finished backing up photos" | ts >> "$LOG_FILE"


# Data
DATA_LOG="/tmp/b2-data.log"
> $DATA_LOG
echo "Starting backup of data..." | ts >> "$LOG_FILE"
/usr/bin/rclone copy /mnt/nas/private/Data b2-data:current --backup-dir=b2-data:old --suffix=-$(date +%Y-%m-%d) --fast-list --suffix-keep-extension --exclude "*DS_Store" --log-file=$DATA_LOG -v | ts | tee -a "$LOG_FILE"

if [[ $? > 0 ]]; then
  curl \
    -H "Title: rclone backup error" \
    -H "Priority: urgent" \
    -H "Tags: warning" \
    -d "Backup of Data on $(hostname) had an error. Please check logs at $LOG_DIR/$(basename -- $DATA_LOG)" \
    https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
fi

mv "$DATA_LOG" "$LOG_DIR"

echo "Finished backing up data" | ts >> "$LOG_FILE"


# Music
MUSIC_LOG="/tmp/b2-music.log"
> $MUSIC_LOG
echo "Starting backup of music..." | ts >> "$LOG_FILE"
/usr/bin/rclone copy /mnt/nas/public b2-music:current --backup-dir=b2-music:old --suffix=-$(date +%Y-%m-%d) --fast-list --suffix-keep-extension --include=Music/** --include="Music Videos"/** --log-file=$MUSIC_LOG -v | ts | tee -a "$LOG_FILE"

if [[ $? > 0 ]]; then
  curl \
    -H "Title: rclone backup error" \
    -H "Priority: urgent" \
    -H "Tags: warning" \
    -d "Backup of Music on $(hostname) had an error. Please check logs at $LOG_DIR/$(basename -- $MUSIC_LOG)" \
    https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
fi

mv "$MUSIC_LOG" "$LOG_DIR"

echo "Finished backing up music" | ts >> "$LOG_FILE"

# Recipes
RECIPES_LOG="/tmp/b2-recipes.log"
> $RECIPES_LOG
echo "Starting backup of recipes..." | ts >> "$LOG_FILE"
/usr/bin/rclone copy /mnt/nas/public b2-data:current --backup-dir=b2-data:old --suffix=-$(date +%Y-%m-%d) --fast-list --suffix-keep-extension --include=Recipes/**  --log-file=$RECIPES_LOG -v | ts | tee -a "$LOG_FILE"

if [[ $? > 0 ]]; then
  curl \
    -H "Title: rclone backup error" \
    -H "Priority: urgent" \
    -H "Tags: warning" \
    -d "Backup of Recipes on $(hostname) had an error. Please check logs at $LOG_DIR/$(basename -- $RECIPES_LOG)" \
    https://ntfy.warchildstory.com/{{ ntfy_server_notifications }}
fi

mv "$RECIPES_LOG" "$LOG_DIR"

echo "Finished backing up recipes" | ts >> "$LOG_FILE"
{% endif %}