#!/bin/bash

## Variables
NOW=$(date +"%d-%m-%Y %T")
readonly NOW

## Destino del backup
readonly BACKUP_MOUNT=/sustancia/backup
readonly BACKUP_TARGET=${BACKUP_MOUNT}/maverick
readonly LOG="${BACKUP_TARGET}"/backup_maverick.log
readonly TARGET_SYSTEM_FILES=${BACKUP_TARGET}/system
readonly TARGET_FILES=${BACKUP_TARGET}/files
readonly TARGET_DOCKER=${BACKUP_TARGET}/docker
readonly TARGET_HOME=${BACKUP_TARGET}/home

## Backup de la carpeta home
readonly BACKUP_HOME=/home/sergio

## Backup de docker
readonly BACKUP_DOCKER=/home/sergio/docker
readonly BACKUP_DOCKER_VOLUMES=/var/volumes

## Monta la carpetas del nas
mount $BACKUP_MOUNT

## Comprueba si se ha montado la carpeta
if [ -f ${BACKUP_MOUNT}/not_mount ]; then
    printf "No se ha montado la carpeta de destino\n"
    exit 0
fi

## Empieza el proceso de backup
printf "\n\nCOMIENZA LA COPIA DE SEGURIDAD (%s)\n" "${NOW}" | tee -a "${LOG}"

## Backup de ficheros del sistema
printf "\n\n>> COPIANDO FICHEROS DEL SISTEMA\n" | tee -a "${LOG}"
mkdir -p "${TARGET_SYSTEM_FILES}/"
for i in /etc/cron.*; do
    rsync -avh --delete "$i" "${TARGET_SYSTEM_FILES}"/ | tee -a "${LOG}"
done
rsync -avh --delete /etc/fstab "${TARGET_SYSTEM_FILES}"/ | tee -a "${LOG}"
rsync -avh --delete /etc/crontab "${TARGET_SYSTEM_FILES}"/ | tee -a "${LOG}"

## Backup de ficheros
FILES=".gitconfig .git-credentials"
for FILE in $FILES; do
    printf "\n\n>> COPIANDO FICHEROS\n" | tee -a "${LOG}"
    if [ -f "${BACKUP_HOME}"/"${FILE}" ]; then
        mkdir -p "${TARGET_FILES}/"
        rsync -avh --delete "${BACKUP_HOME}"/"${FILE}" "${TARGET_FILES}/" | tee -a "${LOG}"
    fi
done

## Backup de las carpetas del directorio home
FOLDERS=".ssh"
for FOLDER in $FOLDERS; do
    if [ -d "${BACKUP_HOME}"/"${FOLDER}" ]; then
        printf "\n\n>> COPIANDO %s\n" "${FOLDER}" | tee -a "${LOG}"
        mkdir -p "${TARGET_HOME}"
        rsync -avh --delete "${BACKUP_HOME}"/"${FOLDER}" "${TARGET_HOME}" | tee -a "${LOG}"
    fi
done

## Backup de docker compose y volumenes
printf "\n\n>> DETENIENDO CONTENEDORES\n"
RUNNING_CONTAINERS=$(docker ps -q)
if [ -n "$RUNNING_CONTAINERS" ]; then
    echo "$RUNNING_CONTAINERS" | xargs docker stop | tee -a "${LOG}"
else
    printf "No hay contenedores en ejecución\n" | tee -a "${LOG}"
fi

FOLDER=DOCKER
if [ -d "${BACKUP_DOCKER}" ]; then
    printf "\n\n>> COPIANDO %s\n" "${FOLDER}" | tee -a "${LOG}"
    mkdir -p "${TARGET_DOCKER}/compose/"
    rsync -avh --delete "${BACKUP_DOCKER}/" "${TARGET_DOCKER}/compose/" | tee -a "${LOG}"
fi
FOLDER=DOCKER_VOLUMES
if [ -d "${BACKUP_DOCKER_VOLUMES}" ]; then
    printf "\n\n>> COPIANDO %s\n" "${FOLDER}" | tee -a "${LOG}"
    mkdir -p "${TARGET_DOCKER}/volumes/"
    rsync -avh --delete "${BACKUP_DOCKER_VOLUMES}/" "${TARGET_DOCKER}/volumes/" | tee -a "${LOG}"
fi

printf "\n\n>> ARRANCANDO CONTENEDORES\n"
if [ -n "$RUNNING_CONTAINERS" ]; then
    echo "$RUNNING_CONTAINERS" | xargs docker start | tee -a "${LOG}"
else
    printf "No hay contenedores para arrancar\n" | tee -a "${LOG}"
fi
