\chapter{Sauvegarde avec Borg}
\label{chap:script_borg}

\begin{lstlisting}[language=bash, caption={
    Exemple de script de sauvegarde avec \textit{Borg Backup}.
}]
#!/bin/bash

# Borg Options
BORG_PASSPHRASE='My passphrase'
BORG_REPO="srv.ecm:/repo/backpup/"
export BORG_REPO
export BORG_PASSPHRASE

DUMP_FOLDER="/var/backup/containers_databases"

printf "\t-> backup myapp database: "
if ! docker exec myapp-db sh -c 'musqldump -u app_db --password="s3cr3t"' | \
    bzip2 > ${DUMP_FOLDER}/myapp_db.archive.sqlz;
then
    printf "Error when dumping myapp-db from myapp-mariadb\n"
fi

printf "\nBackup with borg on %s: \n" "$BORG_REPO"

# Sauvegarde du volume de l'application et du dossier contenant la BDD exportée
if borg create -s --progress ::`date "+%Y.%m.%d"` \
    $DB_CURRENT /var/lib/docker/volumes/docker_files_*;
then
    printf "Borg backup succeed!\n"
else
    printf "Borg backup failed!\n"
fi

printf "\nPrune borg repository %s: \n"
borg prune                          \
    --list                          \
    --show-rc                       \
    --keep-daily    7               \
    --keep-weekly   4               \
    --keep-monthly  6               

exit 0

\end{lstlisting}
