Update docs/Savaneprod/2020-12-04-borg-backup.md
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -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
|
||||||
|
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
|
||||||
|
|
||||||
|
render_time () {
|
||||||
|
local T=$1
|
||||||
|
local H=$((T/60/60%24))
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
afficher () {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
nextcloud_maintenance () {
|
||||||
|
docker exec -u www-data nextcloud_app_1 php occ maintenance:mode --$1
|
||||||
|
[[ $1 = "on" ]] && sleep 1m
|
||||||
|
}
|
||||||
|
|
||||||
|
file_exists_or_explain () {
|
||||||
|
if [[ ! -f $1 ]]
|
||||||
|
then
|
||||||
|
printf '%s does not exist.\n%s\n' "$1" "$2"
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
nextcloud_bkup () {
|
||||||
|
# borg init --encryption=repokey --storage-quota 200G
|
||||||
|
|
||||||
|
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
|
# Restaurer la BDD
|
||||||
# docker exec nextcloud_db_1 pg_restore -U postgres -d nexcloud /backups/db_dump.sql
|
# docker exec nextcloud_db_1 pg_restore -U postgres -d nexcloud /backups/db_dump.sql
|
||||||
|
|
||||||
/usr/local/bin/borg create \
|
afficher "Sauvegarde BorgBase..."
|
||||||
--one-file-system \
|
borg create \
|
||||||
--stats \
|
--stats \
|
||||||
--compression lz4 \
|
--compression lz4 \
|
||||||
::{now:%Y-%m-%d--%Hh%M} \
|
::{now:%Y-%m-%d--%Hh%M} \
|
||||||
/volume1/dir1 \
|
$directories_to_backup \
|
||||||
/volume1/dir2 \
|
--exclude $directories_to_exclude
|
||||||
/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
|
afficher "Fin de la maintenance nextcloud."
|
||||||
docker exec -tiu www-data nextcloud_app_1 php occ maintenance:mode --off 2>&1 | tee -a $log_file
|
nextcloud_maintenance "off"
|
||||||
|
|
||||||
printf '\n%s\n%s\n%s\n' "$STARS" "PRUNE" "$STARS" 2>&1 | tee -a $log_file
|
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"
|
||||||
|
|
||||||
# Keep 31 end of day, 0 additional end of week archives, and 0 end of month archive
|
afficher "Borg info"
|
||||||
/usr/local/bin/borg prune -v --list --stats --keep-daily=31 --keep-weekly=0 --keep-monthly=0 2>&1 | tee -a $log_file
|
|
||||||
printf '\n%s\n' '--- Fin de la sauvegarde ---' 2>&1 | tee -a $log_file
|
|
||||||
|
|
||||||
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
|
# Pour le mail envoyé en fin par le Task Scheduler Synology
|
||||||
/usr/local/bin/borg info borgserver:borgrepo >> $log_file
|
borg info
|
||||||
|
|
||||||
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)
|
# Removes everyting from the repo
|
||||||
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
|
#/usr/local/bin/borg delete --glob-archive '*'
|
||||||
|
|
||||||
# Calcul du temps d'exécution du script
|
nextcloud_bkup
|
||||||
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
|
|
||||||
|
|
||||||
|
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).
|
||||||
|
|||||||
Reference in New Issue
Block a user