MonScoDocEssai/tools/backups/backup_to_remote_server.sh

96 lines
2.6 KiB
Bash
Raw Normal View History

2020-09-26 16:19:37 +02:00
#!/bin/bash
# Script effectuant un backup des principaux repertoires
# du serveur ScoDoc vers une machine distante.
# utilise rsync (pour ne cpier que les fichiers modifies)
# et ssh (pour se connecter de faon securisee).
#
# L'utilisateur root du serveur scodoc (qui execute ce script)
# doit pouvoir se conencter directement (sans mot de passe) sur
# la machine distante (installer les cles ssh necessaires).
#
# A adapter a vos besoins. Utilisation a vos risques et perils.
#
2021-09-11 11:07:53 +02:00
# E. Viennet, 2002, 2021
2020-09-26 16:19:37 +02:00
# Installation:
# 1- Installer rsync:
# apt-get install rsync
# 2- mettre en place un job cron:
# par exemple copier ce script dans /etc/cron.daily/
# (et le rendre executable avec chmod +x ...)
# -------------------- CONFIGURATION A ADAPTER
remotehost=XXXX # nom ou IP du serveur de sauvegarde
destdir=/home/SAU-SCODOC # repertoire sur serveur de sauvegarde
logfile=/var/log/rsynclog # log sur serveur scodoc
# A qui envoyer un mail en cas d'erreur de la sauvegarde:
SUPERVISORMAIL=emmanuel.viennet@example.com
2021-09-11 11:07:53 +02:00
CALLER=$(basename $0)
MACHINE=$(hostname -s)
2020-09-26 16:19:37 +02:00
# -----------------------------------------------------
# ----------------------------------
# Subroutine to terminate abnormally
# ----------------------------------
terminate()
{
2021-09-11 11:07:53 +02:00
dateTest=$(date)
2020-09-26 16:19:37 +02:00
mail -s "Attention: Probleme sauvegarde ScoDoc" $SUPERVISORMAIL <<EOF
The execution of script $CALLER was not successful on $MACHINE.
Look at logfile $logfile"
$CALLER terminated, exiting now with rc=1."
EOF
# repeat message for logs...
echo "The execution of script $CALLER was not successful on $MACHINE."
echo
echo "Look at logfile $logfile"
echo
echo "$CALLER terminated, exiting now with rc=1."
2021-09-11 11:07:53 +02:00
dateTest=$(date)
2020-09-26 16:19:37 +02:00
echo "End of script at: $dateTest"
echo ""
exit 1
}
# --------------------------------------
# Subroutine to mirror a dir using rsync (mirror on REMOTE HOST)
# Variables:
# remotehost : hostname on which is the mirror
# srcdir : directory to mirror on local host
# destdir : directory on remotehost where to put the copy
# logfile : filename to log actions
# --------------------------------------
rsync_mirror_to_remote()
{
2021-09-11 11:07:53 +02:00
echo "--------------- mirroring $MACHINE:$srcdir to $remotehost:$destdir" >> $logfile 2>&1
echo "starting at $(date)" >> $logfile 2>&1
rsync -vaze ssh --delete --rsync-path=/usr/bin/rsync "$srcdir" "$remotehost:$destdir" >> $logfile 2>&1
2020-09-26 16:19:37 +02:00
if [ $? -ne 0 ]
then
echo Error in rsync: code=$?
terminate
fi
2021-09-11 11:07:53 +02:00
echo "ending at $(date)" >> $logfile 2>&1
2020-09-26 16:19:37 +02:00
echo "---------------" >> $logfile 2>&1
}
# ----------- REPERTOIRES A SAUVEGARDER:
for srcdir in /etc /home /root /opt /usr/local /var; do
rsync_mirror_to_remote
done