Sur serveur de sauvegarde, géneration clés SSH :
#ssh-keygen -t rsa
#cat /root/.ssh/id_rsa.pub | ssh root@serveurdistant "test -d .ssh || mkdir -m 0700 .ssh; cat - >>.ssh/authorized_keys"
Paramétrage du script :
#!/bin/sh
export PATH=$PATH:/bin:/usr/bin:/usr/local/bin
#CONFIG :
CURRENTPWD="/home/backup1"
USER="root"
HOST="serveurabackuper"
SOURCES="/etc /var/www /home/backupsql /root"
EXCLUDES=$CURRENTPWD/excludes
BACKUPDIR=$CURRENTPWD/backup
DESTINATION=$BACKUPDIR/current
ARCHIVES=$BACKUPDIR/archives
CMDLISTPACKAGES="dpkg -l"
TODAY=`date +%Y-%m-%d`
ROTATEDAYS=7
#END CONFIG
RSYNCOPTS="--force --ignore-errors --delete-excluded --exclude-from=$EXCLUDES
--delete --backup --backup-dir=$ARCHIVES/$TODAY -zaRv"
#Generer la liste des paquets installes sur HOST
ssh $USER@$HOST "$CMDLISTPACKAGES" > $BACKUPDIR/${HOST}_ListPackages.txt
if [ ! -d $DESTINATION ]
then
install -d $DESTINATION
fi
if [ ! -d $ARCHIVES/$TODAY ]
then
install -d $ARCHIVES/$TODAY
fi
touch $ARCHIVES/$TODAY/logfile.log
echo "---------------------------------" >> $ARCHIVES/$TODAY/logfile.log
echo "Demarrage synchro ${HOST}:" >> $ARCHIVES/$TODAY/logfile.log
date >> $ARCHIVES/$TODAY/logfile.log
echo "---------------------------------" >> $ARCHIVES/$TODAY/logfile.log
rsync -e "ssh -l $USER" $HOST:"$SOURCES" $RSYNCOPTS $DESTINATION >> $ARCHIVES/$TODAY/logfile.log
echo "Fin synchro:" `date` >> $ARCHIVES/$TODAY/logfile.log
echo "---------------------------------" >> $ARCHIVES/$TODAY/logfile.log
find $ARCHIVES/ -maxdepth 1 -mtime +$ROTATEDAYS -type d -exec rm -rf {} \;
Note : le fichier "excludes" est comporte les élémenst que l’on ne veut pas sauvgarder sous la forme :
/root/mesecrets
/root/backups/*.gz
/var/www/siteprod/var/cache/*
/var/www/sitestest
Mettre en crontab
Sauvegarde MySQL côté serveur à sauvegarder…
#!/bin/sh
export PATH=$PATH:/bin:/usr/bin:/usr/local/bin
#CONFIG
TODAY=`date +%Y-%m-%d`
REP_BASE=/home/backupsql/
REP_BACKUP=$REP_BASE$TODAY
ROTATEDAYS=7
#END CONFIG
if [ ! -d $REP_BACKUP ]
then
install -d $REP_BACKUP
fi
#SAUVEGARDE BASES MYSQL
for mybase in base1 base2 base3; do
mysqldump -u root -prootpass -h localhost --opt $mybase | gzip -9 > $REP_BACKUP/$mybase.`date +%d-%m-%Y`.sql.gz
done
find $REP_BASE -maxdepth 1 -mtime +$ROTATEDAYS -type d -exec rm -rf {} \;
Tags
Infos