Rsync script
De wikiGite
Rsync est disponible sur la plupart des systèmes. Le script ci-dessous tiré du script Rdiff-backup est utile quand ce dernier ne peut pas être utilisé.
TODO : ajouter une sauvegarde des fichiers supprimés par --delete (--backup --backup-dir, voir http://www.demongeot.biz/tutos/Rsync.html)
Rsyncbackup.sh
cat rsyncbackup.sh
#!/bin/bash
# Backup Script (RSYNC)
# FSo 12/2011
# V 1.0
# from rdbackup.sh 2.3 (RDIFF-BACKUP)
# différences RDIFF-BACKUP/RSYNC:
# syntaxe "exclude", avec le "=" et le retrait du basename
# pas de rétention, donc pas de cleanup
if [ $# -gt 0 ]
then
# let use the given full path config (.conf) file
. $1
else
# Application specific
. $(dirname $0)/rsyncbackup.conf
fi
while read line
do
REMOTE_DIR="$REMOTE_DIR $line"
shift
done < $LIST
if [ ! -e $APPLICATION_DIR/$PID_FILE ]
then
# Script pre-backup
if [ -f $PRESCRIPT ]
then
$PRESCRIPT
if [ $? -ne 0 ]
then
exit 1
fi
fi
if [ $MYSQLDUMP -eq 1 ]
then
for DB in $MYDB
do
mv $APPLICATION_DIR/mysqldump_$DB.sql.gz $APPLICATION_DIR/mysqldump_$DB.sql.gz.0 2>> $APPLICATION_DIR/$LOG_FILE.0
mysqldump $DB $MYSQLDUMP_OPTS --password=$MYSQL_PASSWORD > $APPLICATION_DIR/mysqldump_$DB.sql 2>> $APPLICATION_DIR/$LOG_FILE.0
gzip $APPLICATION_DIR/mysqldump_$DB.sql 2>> $APPLICATION_DIR/$LOG_FILE.0
done
fi
echo $$ > $APPLICATION_DIR/$PID_FILE
/usr/bin/logger "Sauvegarde rsync $SERVERNAME"
if [ -e $APPLICATION_DIR/$LOG_FILE ]
then
mv $APPLICATION_DIR/$LOG_FILE $APPLICATION_DIR/$LOG_FILE.0
fi
echo "Sauvegarde rsync $SERVERNAME" > $APPLICATION_DIR/$LOG_FILE
#Boucle de backup
ERROR_FLAG=0
ERROR=0
ERRORS="ERREURS: "
for rdir in $(echo $REMOTE_DIR)
do
if [ ! "x$EXCLUDE" == "x" ]
then
EXCLUDE_OPT=""
while read line
do
if <nowiki>[[ $line =~ ^$rdir ]]</nowiki>
then
EX=$(basename $line $rdir)
EXCLUDE_OPT="$EXCLUDE_OPT --exclude=/$EX"
fi
shift
done < $EXCLUDE
fi
# rsync
date >> $APPLICATION_DIR/$LOG_FILE
echo >> $APPLICATION_DIR/$LOG_FILE
echo RSYNC BACKUP OF $rdir >> $APPLICATION_DIR/$LOG_FILE
if [ $LOCAL -eq 1 ]
then
echo rsync $RSYNC_OPTS $EXCLUDE_OPT ${rdir}/ ${BCK_DIR}$rdir/ >> $APPLICATION_DIR/$LOG_FILE
rsync $RDIFF_OPTS $EXCLUDE_OPT ${rdir}/ ${BCK_DIR}$rdir/ >> $APPLICATION_DIR/$LOG_FILE 2>&1
else
echo rsync $RSYNC_OPTS $EXCLUDE_OPT ${rdir}/ $SERVERUSR@$SERVERIP:${BCK_DIR}$rdir/ >> $APPLICATION_DIR/$LOG_FILE
rsync $RSYNC_OPTS $EXCLUDE_OPT ${rdir}/ $SERVERUSR@$SERVERIP:${BCK_DIR}$rdir/ >> $APPLICATION_DIR/$LOG_FILE 2>&1
fi
ERROR=$?
if [ $ERROR -ne 0 ]
then
ERRORS="$ERRORS $BASE=$ERROR:"
ERROR_FLAG=1
fi
done
if [ $ERROR_FLAG -eq 1 ]
then
(echo $ERRORS
date
echo Voir $APPLICATION_DIR/$LOG_FILE
grep -i warning $APPLICATION_DIR/$LOG_FILE
grep -i error $APPLICATION_DIR/$LOG_FILE
)| mail -s "ERREUR BACKUP SUR $SERVERNAME $(date +%d/%m)" $MAIL_ADMIN
else
(date
ls -l $APPLICATION_DIR/$LOG_FILE
df -k $APPLICATION_DIR
)| mail -s "Sauvegarde $SERVERNAME $(date +%d/%m) terminee" $MAIL_ADMIN
fi
date >> $APPLICATION_DIR/$LOG_FILE
/usr/bin/logger "FIN rsync $SERVERNAME"
rm -f $APPLICATION_DIR/$PID_FILE
else
(echo "$HOSTNAME:$APPLICATION_DIR/$PID_FILE existe : abandon de $0") | mail -s "Erreur sur backup $SERVERNAME" $MAIL_ADMIN
fi
Fichier de configuration ; rsyncbackup.conf
cat rsyncbackup.conf
# Rsyncbackup configuration file
MAIL_ADMIN=support@systea.net
SERVERNAME=$(hostname)
# LOCAL = 1 : backup to local directory (or NFS mount or SMB mount)
# LOCAL = 0 : backup to a remote server (via SSH)
LOCAL=0
# IP, port, user of backup server (if remote)
SERVERIP=192.168.200.206
SERVERPORT=22
SERVERUSR=ephoto_ubp
# Path where are rdbackup.sh, .conf, .list and .exclude
SCRIPT_DIR=$(dirname $0)
# Specific application path
APPLICATION_DIR=/home/.sites/28/site1
# Or :
#APPLICATION_DIR=$SCRIPT_DIR
LIST=$SCRIPT_DIR/rsyncbackup.list
EXCLUDE=$SCRIPT_DIR/rsyncbackup.exclude
# LOG and other files
LOG_FILE=rsync.log
PID_FILE=rsync.pid
# Options
RSYNC_OPTS="-avz --delete"
# Script to launch BEFORE backup
PRESCRIPT=
# BACKUP DESTINATION DIRECTORY (local or on remote server)
BCK_DIR=/backup/ephotoubp
# DUMP MYSQL
# Change to MYSQLDUMP=0 to disable
MYSQLDUMP=1
MYSQL_PASSWORD=mysql0nyX
# MYDB=<mysql_db_name> # Or :
MYDB=$(mysql --execute "show databases\G" --password=$MYSQL_PASSWORD | grep -v row | sed -e 's/Database: //g' | grep -v mysql | grep -v information_schema | grep -v performance_schema)
MYSQLDUMP_OPTS="--routines"