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 [[ ! -d logfile_path ]] && mkdir -p $logfile_path
# 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 logfile_name="$(date +%Y-%m-%d--%Hh%M)_borgbase.log"
export HOME=/root logfile="$logfile_path/$logfile_name"
# Suite l'erreur "Attempting to access a previously unknown unencrypted repository!"
# Apparue le 28/05/2020
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 # Logging destination management
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$logfile 2>&1 | tee -a $logfile
# Préparation de la sauvegarde du nextcloud : << 'EXPLANATION'
docker exec -tiu www-data nextcloud_app_1 php occ maintenance:mode --on 2>&1 | tee -a $log_file https://serverfault.com/questions/103501/how-can-i-fully-log-all-bash-scripts-actions
sleep 1m 2>&1 | tee -a $log_file exec 3>&1 4>&2
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
# Dump de la base de données Postgres render_time () {
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 local T=$1
# Restaurer la BDD local H=$((T/60/60%24))
# docker exec nextcloud_db_1 pg_restore -U postgres -d nexcloud /backups/db_dump.sql local M=$((T/60%60))
local S=$((T%60))
local r=''
[[ $H > 0 ]] && r=$(printf '%d hours ' $H)
[[ $M > 0 ]] && r+=$(printf '%d minutes ' $M)
[[ $H > 0 || $M > 0 ]] && r+=$(printf 'and ')
r+=$(printf '%d seconds' $S)
echo $r
}
/usr/local/bin/borg create \ afficher () {
--one-file-system \ local STARS="***************************************************************************"
--stats \ if [[ $2 != "notime" ]]
--compression lz4 \ then printf '%s\n\t%s elapsed -- %s \n\t%s\n%s\n' "$STARS" "$(render_time $SECONDS)" "$(date +%c)" "$1" "$STARS"
::{now:%Y-%m-%d--%Hh%M} \ else printf '%s\n\t%s\n%s\n' "$STARS" "$1" "$STARS"
/volume1/dir1 \ fi
/volume1/dir2 \ }
/volume1/nextcloud \
--exclude /volume1/nextcloud/db \
2>&1 | tee -a $log_file
# ne pas sauvegarder les fichiers du conteneur postgres
#--exclude /volume1/nextcloud/db \
# Fin de la sauvegarde
docker exec -tiu www-data nextcloud_app_1 php occ maintenance:mode --off 2>&1 | tee -a $log_file
printf '\n%s\n%s\n%s\n' "$STARS" "PRUNE" "$STARS" 2>&1 | tee -a $log_file nextcloud_maintenance () {
docker exec -u www-data nextcloud_app_1 php occ maintenance:mode --$1
[[ $1 = "on" ]] && sleep 1m
}
# Keep 31 end of day, 0 additional end of week archives, and 0 end of month archive file_exists_or_explain () {
/usr/local/bin/borg prune -v --list --stats --keep-daily=31 --keep-weekly=0 --keep-monthly=0 2>&1 | tee -a $log_file if [[ ! -f $1 ]]
printf '\n%s\n' '--- Fin de la sauvegarde ---' 2>&1 | tee -a $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" "borg info" "$STARS" >> $log_file
# Pour le mail envoyé en fin par le Task Scheduler Synology
/usr/local/bin/borg info borgserver:borgrepo >> $log_file
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) nextcloud_bkup () {
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 # borg init --encryption=repokey --storage-quota 200G
# Calcul du temps d'exécution du script afficher "Début de la sauvegarde du $(date +%c)" "notime"
end_time=$(date +%s)
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 "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).