#!/bin/bash

LOGFILENAME="dup_backup.log"
LOGDIR="/mnt/user/logs/Duplicity/${LOGFILENAME}"
scriptname="duplicity_backup.sh"
# check if we are the only local instance
if [[ "`pidof -x ${scriptname} -o %PPID`" ]]; then
        echo "This script is already running with PID `pidof -x $(basename $0) -o %PPID`" >> "${LOGDIR}"
        exit
fi
 

# start your script here
SRCNAMES="Apps Audiobooks Backup Docs Ebooks Ebooks-Other Manga Music Photos myscripts Public Software system"
#SRCNAMES="Ebooks-Other"
#SRCNAMES="Apps"
VERBOSITY=4
DOCKERDIR="/mnt/user/appdata/duplicity_ket_backup"
CACHEDIR="${DOCKERDIR}/.cache:/home/duplicity/.cache/duplicity"
ARCHIVEDIR="${DOCKERDIR}/archive"
SRCDIR="/mnt/user/"
DSTDIR="/mnt/user/Google/gdrive/Backup/Ket/Duplicity"
KEYDIR="${DOCKERDIR}/.gnupg:/home/duplicity/.gnupg"
BOTODIR="${DOCKERDIR}/.boto:/home/duplicity/.boto:ro"
PASSPHRASE="fjK8UH3hVvNimTvkmMMt7DhesJ"

for SRCNAME in $SRCNAMES
do
  echo " " >> "${LOGDIR}"
  echo "****************************************************" >> "${LOGDIR}"
  echo "$(date "+%d/%m/%Y %T") Starting ${SRCNAME} Backup" >> "${LOGDIR}"
  echo "****************************************************" >> "${LOGDIR}"
  echo " "
  docker run --rm \
-v "${SRCDIR}/${SRCNAME}:/src:ro" \
-v "${DSTDIR}/${SRCNAME}:/dst" \
-v "${ARCHIVEDIR}:/archive" \
-v "${CACHEDIR}" \
-v "${KEYDIR}" \
-v "${BOTODIR}" \
-v "${LOGDIR}:/logs" \
-v "/etc/localtime:/etc/localtime:ro" \
-e "TZ=Europe/Paris" \
-e "PASSPHRASE=${PASSPHRASE}" \
wernight/duplicity duplicity incr \
--allow-source-mismatch \
--full-if-older-than=36M \
--log-file "/logs" \
--verbosity ${VERBOSITY} \
--name="${SRCNAME}" \
--archive-dir="/archive" \
/src file://dst
  echo "****************************************************" >> "${LOGDIR}"
  echo "$(date "+%d/%m/%Y %T") End ${SRCNAME} Backup" >> "${LOGDIR}"
  echo "****************************************************" >> "${LOGDIR}"
  echo " " >> "${LOGDIR}"
done
