Update docs/Savaneprod/2020-12-04-borg-backup.md
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
jf
2025-04-27 08:33:45 +02:00
parent c57cb0414e
commit 1010c0df9e

View File

@@ -23,79 +23,142 @@ Ensuite il suffit de raffraîchir la liste des paquets disponibles et Borg Appar
```bash ```bash
#!/bin/bash #!/bin/bash
# borg init --encryption=keyfile-blake2 /volume1/Backup/borgrepolzma/ SECONDS=0
SCRIPT_PATH="$(dirname $(readlink -f $BASH_SOURCE))"
alias borg="/usr/local/bin/borg"
#Formatage et mise en page
STARS='***************************************************************************'
# Pour le calcul d'exécutoin du script BORG_VARIABLES="/volume1/root_only/Backup/nextcloud/BORG_VARIABLES.secret"
start_time=$(date +%s) if [[ ! -f $BORG_VARIABLES ]]
then
printf "Create the BORG_VARIABLES.secret file."
exit 2
fi
source $BORG_VARIABLES
log_file='/volume1/Backup/logs/'$(date +%Y-%m-%d--%Hh%M)'_borgbase.log' : << 'BORG_VARIABLES.secret'
main_dir=""
export BORG_RSH='' # ssh options ex: 'ssh -i /.ssh/id_rsa'
export BORG_REPO='borgserver:/borgrepo'
export BORG_PASSCOMMAND="cat $main_dir/BORG_PASSPHRASE.secret"
export BORG_BASE_DIR="$main_dir/BORG_BASE_DIR"
directories_to_backup=""
directories_to_exlude=""
logfile_path="$main_dir/logs"
BORG_VARIABLES.secret
echo $(date +%d\ %b\ %Y\ %H:%M:%S) 2>&1 | tee -a $log_file
# Définition des variables d'environnement pour simplifier les commandes #######################################################
# export BORG_RSH=<clef privée pour sauvegarder sur le serveur distant>
# export BORG_REPO='borgserver:/borgrepo'
export BORG_REPO='borgserver:borgrepo'
export BORG_CACHE_DIR='<emplacement du cache de Borg>'
# Cron n'a pas de $HOME [[ ! -d logfile_path ]] && mkdir -p $logfile_path
export HOME=/root
# Suite l'erreur "Attempting to access a previously unknown unencrypted repository!" logfile_name="$(date +%Y-%m-%d--%Hh%M)_borgbase.log"
# Apparue le 28/05/2020 logfile="$logfile_path/$logfile_name"
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes
printf '\n%s\n%s\n%s\n' "$STARS" "Début de la sauvegarde " "$STARS" 2>&1 | tee -a $log_file
# Préparation de la sauvegarde du nextcloud # Logging destination management
docker exec -tiu www-data nextcloud_app_1 php occ maintenance:mode --on 2>&1 | tee -a $log_file exec 3>&1 4>&2
sleep 1m 2>&1 | tee -a $log_file trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$logfile 2>&1 | tee -a $logfile
# Dump de la base de données Postgres : << 'EXPLANATION'
docker exec -i nextcloud_db_1 /usr/bin/pg_dump -U nextcloud nextcloud | gzip -9 > /volume1/nextcloud/db_dump.sql.gz 2>&1 | tee -a $log_file https://serverfault.com/questions/103501/how-can-i-fully-log-all-bash-scripts-actions
# Restaurer la BDD exec 3>&1 4>&2
# docker exec nextcloud_db_1 pg_restore -U postgres -d nexcloud /backups/db_dump.sql Saves file descriptors so they can be restored to whatever they were before redirection or used themselves to output to whatever they were before the following redirect.
trap 'exec 2>&4 1>&3' 0 1 2 3
Restore file descriptors for particular signals. Not generally necessary since they should be restored when the sub-shell exits.
exec 1>log.out 2>&1
Redirect stdout to file log.out then redirect stderr to stdout. Note that the order is important when you want them going to the same file. stdout must be redirected before stderr is redirected to stdout.
From then on, to see output on the console (maybe), you can simply redirect to &3. For example,
echo "$(date) : part 1 - start" >&3
will go to wherever stdout was directed, presumably the console, prior to executing line 3 above.
EXPLANATION
/usr/local/bin/borg create \ render_time () {
--one-file-system \ local T=$1
--stats \ local H=$((T/60/60%24))
--compression lz4 \ local M=$((T/60%60))
::{now:%Y-%m-%d--%Hh%M} \ local S=$((T%60))
/volume1/dir1 \ local r=''
/volume1/dir2 \ [[ $H > 0 ]] && r=$(printf '%d hours ' $H)
/volume1/nextcloud \ [[ $M > 0 ]] && r+=$(printf '%d minutes ' $M)
--exclude /volume1/nextcloud/db \ [[ $H > 0 || $M > 0 ]] && r+=$(printf 'and ')
2>&1 | tee -a $log_file r+=$(printf '%d seconds' $S)
# ne pas sauvegarder les fichiers du conteneur postgres echo $r
#--exclude /volume1/nextcloud/db \ }
# Fin de la sauvegarde afficher () {
docker exec -tiu www-data nextcloud_app_1 php occ maintenance:mode --off 2>&1 | tee -a $log_file local STARS="***************************************************************************"
if [[ $2 != "notime" ]]
then printf '%s\n\t%s elapsed -- %s \n\t%s\n%s\n' "$STARS" "$(render_time $SECONDS)" "$(date +%c)" "$1" "$STARS"
else printf '%s\n\t%s\n%s\n' "$STARS" "$1" "$STARS"
fi
}
printf '\n%s\n%s\n%s\n' "$STARS" "PRUNE" "$STARS" 2>&1 | tee -a $log_file
# Keep 31 end of day, 0 additional end of week archives, and 0 end of month archive nextcloud_maintenance () {
/usr/local/bin/borg prune -v --list --stats --keep-daily=31 --keep-weekly=0 --keep-monthly=0 2>&1 | tee -a $log_file docker exec -u www-data nextcloud_app_1 php occ maintenance:mode --$1
printf '\n%s\n' '--- Fin de la sauvegarde ---' 2>&1 | tee -a $log_file [[ $1 = "on" ]] && sleep 1m
}
printf '\n%s\n%s\n%s\n' "$STARS" "borg info" "$STARS" >> $log_file file_exists_or_explain () {
# Pour le mail envoyé en fin par le Task Scheduler Synology if [[ ! -f $1 ]]
/usr/local/bin/borg info borgserver:borgrepo >> $log_file then
printf '%s does not exist.\n%s\n' "$1" "$2"
exit 2
fi
}
printf '\n%s\n%s\n%s\n' "$STARS" "...Suppression des logs de plus de 200 jours..." "$STARS" 2>&1 | tee -a $log_file
find /volume1/Backup/logs -mtime +60 -exec rm -f {} \; 2>&1 | tee -a $log_file
timestamp=$(date +%s)
TZ='Europe/Paris' printf '%s\nNous sommes le %(%d %b %Y)T et il est %(%H:%M:%S)T.\n%s\n' "$STARS" $timestamp $timestamp "$STARS" 2>&1 | tee -a $log_file
# Calcul du temps d'exécution du script nextcloud_bkup () {
end_time=$(date +%s) # borg init --encryption=repokey --storage-quota 200G
exec_time=$(($end_time - $start_time))
printf '\n%s\n'"$STARS"
TZ='Europe/Paris' printf "L'exécution du script a duré %(%Hh%Mm%Ss)T\n" $exec_time 2>&1 | tee -a $log_file
afficher "Début de la sauvegarde du $(date +%c)" "notime"
afficher "Borg info"
borg info
# afficher "Borg delete"
# /usr/local/bin/borg delete --glob-archive '*'
afficher "Mise en maintenance du nextcloud"
nextcloud_maintenance "on"
afficher "Dump de la base de données Postgres..."
docker exec nextcloud_db_1 /usr/bin/pg_dump -U nextcloud nextcloud | gzip > /volume1/nextcloud/db_dump.sql.gz
# Restaurer la BDD
# docker exec nextcloud_db_1 pg_restore -U postgres -d nexcloud /backups/db_dump.sql
afficher "Sauvegarde BorgBase..."
borg create \
--stats \
--compression lz4 \
::{now:%Y-%m-%d--%Hh%M} \
$directories_to_backup \
--exclude $directories_to_exclude
afficher "Fin de la maintenance nextcloud."
nextcloud_maintenance "off"
afficher "Borg prune"
# Keep 7 end of day, 0 additional end of week archives, and 6 end of month archive
borg prune -v --list --stats --keep-daily=15 --keep-weekly=0 --keep-monthly=3
afficher "Fin de borg prune"
afficher "Borg info"
# Pour le mail envoyé en fin par le Task Scheduler Synology
borg info
}
# Removes everyting from the repo
#/usr/local/bin/borg delete --glob-archive '*'
nextcloud_bkup
printf "La sauvegarde a mis %s à s'exécuter." "$(render_time $SECONDS)"
exit 1
``` ```
Le service qui me permet d'externaliser facilement les sauvegardes, est [BorgBase](https://borgbase.com). Le service qui me permet d'externaliser facilement les sauvegardes, est [BorgBase](https://borgbase.com).