Gestion des backup automatiques
Instalation
$ aptitude backup-manager
Configuration de backup-manager
Voir http://doc.ubuntu-fr.org/backup-manager pour les détails.
# Éditer le fichier de configuration $ nano /etc/backup-manager.conf # Where to store the archives export BM_REPOSITORY_ROOT="/backup/" # Paths without spaces in their name: export BM_TARBALL_DIRECTORIES=" /etc ... " # création d'un lien symbolique si 2 archives sont identiques export BM_ARCHIVE_PURGEDUPS="true" # ajout d'un script de post backup # It will be executed after the last action of backup-manager. export BM_POST_BACKUP_COMMAND="/etc/backup-manager-post.sh"
Creer le script de transfert vers le serveur de backup
$ nano /etc/backup-manager-post.sh #!/bin/bash DATE_NOW="$(date "%Y-%m-%d")" #---------------------- System Parameters RSYNC=/usr/bin/rsync SSH=/usr/bin/ssh #---------------------- Configuration Parameters RUSER="backup" RHOST="gwadanina.net_" RPORT="22" USER="backup" RPATH=/backup/backupdir/backup/ LPATH=/home/archive/ #----------- fichier de log TMP_LOG=/tmp/"backup_$DATE_NOW.log"; #----------- Mail Parameters EMAILS_ADRESSES="backup" EMAILS_SUBJECT="[RSYNC_BACKUP]"$DATE_NOW # transfert des fichiers vers le serveur distant echo "["$(date +%F-%T)"] - debut du tranfert sur le serveur $RHOST" > $TMP_LOG; su - $USER -c "$RSYNC -azvvv -e '$SSH -p $RPORT' $LPATH $RUSER@$RHOST:$RPATH"; # affiche la liste des nouveaux fichiers depuis 24 heures echo "["$(date +%F-%T)"] - liste des nouveaux fichiers" >> $TMP_LOG; su - $USER -c "$SSH -p $RPORT $RUSER@$RHOST 'find $RPATH -type f -mmin -1440 -exec ls -l {} \; | sort -k7'" >> $TMP_LOG; #---------------------- Send Mail --------------------# mail -s $EMAILS_SUBJECT $EMAILS_ADRESSES < $TMP_LOG; echo "["$(date +%F-%T)"] - send mail is done" >> $TMP_LOG; echo "["$(date +%F-%T)"] - your backup is complete!" >> $TMP_LOG; echo "" >> $TMP_LOG; echo $TMP_LOG;
Mettre à jour Cron
$crontab -e 0 2 */1 * * /etc/backup-manager.sh <code> ==== Creer le script de lancement ==== <code bash> $nano /etc/backup-manager.sh #!/bin/sh echo "Backup de la base de donnee" /etc/backup-manager-mysql.sh #---------------------- System Parameters --------------------# # Variables definition DATE_NOW="$(date +"%Y-%m-%d")" BACKUP_REPO_CONF="/backup/archives/config" BACKUP_REPO_SQL="/backup/archives/mysqlBackup/"$DATE_NOW"/" BACKUP_REPO_LAST="/backup/archives/last_backup" # Creation of working directory DEST_WORKING_CONF="$BACKUP_REPO_CONF/$DATE_NOW/" [ ! -d $DEST_WORKING_CONF ] && mkdir -p $DEST_WORKING_CONF || : # Creation of temporary log in working folder TMP_LOG=$BACKUP_REPO_LAST"/backup_$DATE_NOW.log"; echo "["$(date +%F-%T)"] - Today working directory is : " $DEST_WORKING_CONF > $TMP_LOG; echo "["$(date +%F-%T)"] - Get dpkg selection" > $TMP_LOG; dpkg --get-selections > $DEST_WORKING_CONF/dpkg_--get-selections.$DATE_NOW.txt if [ ! $? -eq 0 ] ; then echo "["$(date +%F-%T)"] - An error occurred while getting dpkg selections " >> $TMP_LOG; fi ### get the last backup of SQL and config echo "["$(date +%F-%T)"] - clean the repository" >> $TMP_LOG; rm -rf $BACKUP_REPO_LAST/* if [ ! $? -eq 0 ] ; then echo "["$(date +%F-%T)"] - An error occurred while cleaning repo "$BACKUP_REPO_LAST" " >> $TMP_LOG; fi echo "["$(date +%F-%T)"] - copy " $DEST_WORKING_CONF" to "$BACKUP_REPO_LAST"" >> $TMP_LOG; cp -R $DEST_WORKING_CONF $BACKUP_REPO_LAST/ if [ ! $? -eq 0 ] ; then echo "["$(date +%F-%T)"] - An error occurred while copy " $DEST_WORKING_CONF" to "$BACKUP_REPO_LAST" " >> $TMP_LOG; fi echo "["$(date +%F-%T)"] - copy " $BACKUP_REPO_SQL" to "$BACKUP_REPO_LAST"" >> $TMP_LOG; cp -R $BACKUP_REPO_SQL $BACKUP_REPO_LAST/ if [ ! $? -eq 0 ] ; then echo "["$(date +%F-%T)"] - An error occurred while copy " $BACKUP_REPO_SQL" to "$BACKUP_REPO_LAST" " >> $TMP_LOG; fi test -x /usr/sbin/backup-manager || exit 0 /usr/sbin/backup-manager
Créer le script de sauvegarde de MySql
$ touch /etc/backup-manager-mysql.sh $ chmod +x /etc/backup-manager-mysql.sh # édition du fichier $ nano /etc/backup-manager-mysql.sh
#!/bin/bash # BACKUP all MySQL databases # List the databases and a user backup # Indépendamment in a file. Sql then compresses # Files in a tar.gz # http://gwadanina.net #---------------------- System Parameters --------------------# # Variables définition MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" GZIP="$(which gzip)" MKDIR="$(which mkdir)" MYSQLCHECK="$(which mysqlcheck)" # Date of day DATE_NOW="$(date +"%Y-%m-%d")" # Location of backup file in local BACKUP_REPO="/backup/archives/mysqlBackup" KEEP_LOCAL_FILE=y # Number of days to retain a backup in local DAY_TO_KEEP_BACKUP_LOCAL=90 #---------------------- MySQL Parameters --------------------# #Configuring the connection to the mySQL database SQL_USER="user" SQL_USER_PASSWORD="password" SQL_SERVER_HOST="localhost" # Not to save this Databases list (separated by spaces) NOT_TO_SAVE="information_schema" #---------------------- Mail Parameters --------------------# IS_MAIL_TO_SEND=y # email addresses to send log extract, separated by a space EMAILS_ADRESSES="backup" EMAILS_SUBJECT="[MySQL_BACKUP]"$DATE_NOW # --------------------------------------------------------- # # End of Parameters # # --------------------------------------------------------- # # Creation of working directory DEST_WORKING="$BACKUP_REPO/$DATE_NOW/" [ ! -d $DEST_WORKING ] && $MKDIR -p $DEST_WORKING || : # Creation of temporary log in working folder TMP_LOG=$DEST_WORKING"sql_backup_$DATE_NOW.log"; echo "["$(date +%F-%T)"] - Today working directory is : " $DEST_WORKING > $TMP_LOG; # Backuping the database echo "["$(date +%F-%T)"] - Creating the list of all your databases..." >> $TMP_LOG; DATABASE_TABLE_LISTE="$($MYSQL -u$SQL_USER -h$SQL_SERVER_HOST -p$SQL_USER_PASSWORD -Bse 'show databases')" # If an error occurs in the list of databases if [ ! $? -eq 0 ] ; then echo "["$(date +%F-%T)"] - An error occurred while listing databases " >> $TMP_LOG; fi echo "["$(date +%F-%T)"] - List of all your databases..." $DATABASE_TABLE_LISTE >> $TMP_LOG; for BASE in $DATABASE_TABLE_LISTE do # analyse, checking tables. $MYSQLCHECK -u$SQL_USER -h$SQL_SERVER_HOST -p$SQL_USER_PASSWORD --auto-repair --optimize $BASE >> $TMP_LOG; if [ ! $? -eq 0 ] ; then echo "["$(date +%F-%T)"] - An error occurred analyse, checking " $BASE "tables." >> $TMP_LOG; fi skipdb=-1 if [ "$NOT_TO_SAVE" != "" ] ; then for i in $NOT_TO_SAVE do [ "$BASE" == "$i" ] && skipdb=1 || : echo "["$(date +%F-%T)"] - Skipping " $BASE >> $TMP_LOG; done fi # Creation of dump tables if [ "$skipdb" == "-1" ] ; then echo "["$(date +%F-%T)"] - Backing up " $BASE " databases... " >> $TMP_LOG; SQL_FILE_NAME=$DEST_WORKING$BASE.$DATE_NOW.sql.gz $MYSQLDUMP -u$SQL_USER -h$SQL_SERVER_HOST -p$SQL_USER_PASSWORD $BASE | $GZIP -f -9 > $SQL_FILE_NAME if [ ! $? -eq 0 ] ; then echo "["$(date +%F-%T)"] - An error occurred in backing up " $BASE "tables." >> $TMP_LOG; fi # size of the archive log and name FILESIZE=`ls -lh $SQL_FILE_NAME | awk '{print $5}'` echo "["$(date +%F-%T)"] - Generation of the archive for : "$SQL_FILE_NAME" ("$FILESIZE")" >> $TMP_LOG; fi done #---------- remove local old files as fo date----------# if [ "$KEEPLOCAL" = "y" ] ; then cd $BACKUP_REPO echo "["$(date +%F-%T)"] - Search and delete the archive with more than "$DAY_TO_KEEP_BACKUP_LOCAL" DAY" >> $TMP_LOG; find $BACKUPDIR -maxdepth 1 -type f -ctime +$DAY_TO_KEEP_BACKUP_LOCAL -exec rm -v {} \; >> $TMP_LOG ; if [ ! $? -eq 0 ] ; then echo "["$(date +%F-%T)"] - An error occurred Search and delete the archive" >> $TMP_LOG; fi fi #---------------------- Send Mail --------------------# if [ "$IS_MAIL_TO_SEND" = "y" ] ; then mail -s $EMAILS_SUBJECT $EMAILS_ADRESSES < $TMP_LOG; echo "["$(date +%F-%T)"] - Send mail is done" >> $TMP_LOG; fi echo "["$(date +%F-%T)"] - Your backup is complete!" >> $TMP_LOG; echo "" >> $TMP_LOG; # write log on output for script log echo $TMP_LOG;
Creation de certificat pour ce connecter sans mot de passe via ssh
# sur le serveur a sauvegarder $ ssh-keygen -t rsa # creer un repertoire sur le serveur cible $ ssh backup@gwadanina.net_ mkdir -p .ssh # ajouter les cles sur le serveur cible $ cat /home/current/.ssh/id_rsa.pub | ssh backup@gwadanina.net_ 'cat >> .ssh/authorized_keys2' # mettre les droits sur les certificats cible $ ssh backup@gwadanina.net_ 'chown -R backup:backup .ssh' # se connecter sans mot de passe $ ssh backup@gwadanina.net_
Configuration de rsync
# Installation $ aptitude install rsync # configuration $ nano /etc/default/rsync # Mettre la variable de false à true si l'on veux activer rsync en tant que service RSYNC_ENABLE=true # configuration du serveur $ nano /etc/rsyslog.conf
Script de Backup en local
#!/bin/sh RSYNC=/usr/bin/rsync RPATH=/home/backup/target folder LPATH=/home/backup/source folder $RSYNC -azv --force --ignore-errors --delete $LPATH $RPATH