Backup du SeRoM Wiki

De SeRoM - Wiki
Aller à : navigation, rechercher
Draft Draft / Brouillon
Cette page/chapitre est un brouillon, pas un article ou une règle, et peut être incomplète ou non-fiable! Vous êtes invités à faire des suggestions en page de discussion.

Intro

Environement

Debian

Back-up

PHP site

 tar rvf /root/bkp/mediawiki-backup.tar /var/www/html/mediawiki

If like me you have some sym link in your mediawiki directory, don't forget to use the '-h' option of tar.

 tar rvfh /root/bkp/mediawiki-backup.tar /var/www/html/mediawiki

The database

 mysqldump -p wikidb > /root/bkp/mediawiki.sql
 tar rvf /root/bkp/mediawiki-backup.tar /root/bkp/mediawiki.sql

Compression

 bzip2 /root/bkp/mediawiki-backup.tar

Backup automatically via scripts

We will take a backup of our mediawiki (the PHP wite + SQL dump). And send a copy to an other remote host. On this remote host we will proceed to a rotation of the archives.

On the localhost

The basic script

Copy this Script (bakup-mediawiki.sh)

 #!/bin/bash
 
 #############
 #
 # v 1.1
 #
 # Out Error:
 #	1 - SSH failed
 # 	2 - Nothing to backup! ${WIKI_WEB_DIR} does not exist.
 #	3 - MySQL Dump failed
 
 
 exec > /dev/null
 
 # Definition of Variables
 ERR_NUM=0
 BKP_DIR="/root/bkp"
 BKP_DIR_ROOT=$(echo "${BKP_DIR}" | cut -c 2-)
 WIKI_WEB_DIR="/var/www/serom/"
 WIKI_WEB_DIR_ROOT=$(echo "${WIKI_WEB_DIR}" | cut -c 2-)
 
 DB_SERV=$(grep "wgDBserver" ${WIKI_WEB_DIR}/LocalSettings.php | cut -d\" -f2)
 DB_NAME=$(grep "wgDBname" ${WIKI_WEB_DIR}/LocalSettings.php | cut -d\" -f2)
 DB_LOGIN=$(grep "wgDBuser" ${WIKI_WEB_DIR}/LocalSettings.php | cut -d\" -f2)
 DB_PASS=$(grep '^\$wgDBpassword' ${WIKI_WEB_DIR}/LocalSettings.php | cut -d\" -f2)
 
 SSH_HOST="backup-serveur.monDomaine.net"
 SSH_LOGIN="backup-user"
 SSH_DIR="/home/bakcup-user/backup"
 
 
 if [ ! -d ${BKP_DIR} ]; then
 	# Create the backup directory
 	mkdir -p ${BKP_DIR};
 else
 	# Take a copy of the previous Archive
 	rm -f ${BKP_DIR}/mediawiki-backup.tar.bz2.old
 	mv ${BKP_DIR}/mediawiki-backup.tar.bz2 ${BKP_DIR}/mediawiki-backup.tar.bz2.old
 fi
 
 if [ ! -d ${WIKI_WEB_DIR} ]; then
 	ERR_NUM=2
 	echo "Nothing to backup! ${WIKI_WEB_DIR} does not exist."
 	exit ${ERR_NUM}
 fi
 	
 # Take a copy of the PHP site directory
 cd /; tar rvfh ${BKP_DIR}/mediawiki-backup.tar ${WIKI_WEB_DIR_ROOT}
 
 # Take a SQL Dump of the MySQL Base and add the SQL Dump to the archive
 mysqldump --host=${DB_SERV} --user=${DB_LOGIN} --password=${DB_PASS} ${DB_NAME} > ${BKP_DIR}/mediawiki.sql
 cp ${BKP_DIR}/mediawiki.sql /tmp/
 MySQL_RET_CODE=${?}
 if [ ${MySQL_RET_CODE} != 0 ]; then
 	ERR_NUM=3
 	echo "MySQL Dump failed! (return code of MySQL: ${MySQL_RET_CODE})"
 	exit ${ERR_NUM}
 fi
 cd /; tar rvf ${BKP_DIR}/mediawiki-backup.tar ${BKP_DIR_ROOT}/mediawiki.sql
 
 # Compress the archive
 bzip2 ${BKP_DIR}/mediawiki-backup.tar
 
 # Send the archive on an other host
 if [ $(ssh ${SSH_LOGIN}@${SSH_HOST} uname) ]; then
 	scp ${BKP_DIR}/mediawiki-backup.tar.bz2 ${SSH_LOGIN}@${SSH_HOST}:${SSH_DIR}/
 else
 	ERR_NUM=1
 	echo "SSH connection to scp the backup failed!"
 	exit ${ERR_NUM}
 fi
 
 # Clean the files
 rm -f ${BKP_DIR}/mediawiki-backup.tar
 rm -f ${BKP_DIR}/mediawiki.sql
 
 exit ${ERR_NUM}


And put it in executable mode:

 chmod bakup-mediawiki.sh

Scheduled by Cron

Edit this file /etc/cron.daily/backup-mediawiki

 #!/bin/sh
 
 /root/ScriptsDev/backup-mediawiki/backup-mediawiki.sh
 EXITVALUE=$?
 if [ $EXITVALUE != 0 ]; then
     /usr/bin/logger -t bkp-mediawiki "ALERT exited abnormally with [$EXITVALUE]"
 fi
 exit 0

And put it in executable mode:

 chmod /etc/cron.daily/backup-mediawiki

On the remote host

We create a logrotate config in /home/bakcup-user/backup/bkprotate.conf

 # see "man logrotate" for details
 weekly
 rotate 4
 notifempty
 compress
 delaycompress
 
 # system-specific logs may be also be configured here.
 #####################################################
 #
 # Back-up rotate - MediaWiki from ServerXY
 #
 #####################################################
 #Daily
 /home/bakcup-user/backup/mediawiki/mediawiki-backup.tar.bz2 {
       daily
       rotate 7
       nocompress
 #     create 600 user group
 }
 
 #Weekly
 /home/bakcup-user/backup/mediawiki/mediawiki-backup.tar.bz2.7 {
       weekly
       rotate 4
       nocompress
 }
 
 #Monthly
 /home/bakcup-user/backup/mediawiki/mediawiki-backup.tar.bz2.7.4 {
       monthly
       nocompress
       rotate 12
 }

Edit this file /home/bakcup-user/backup/bkprotate.sh

 #!/bin/sh
 
 BKP_DIR="/home/bakcup-user/backup"
 
 /usr/sbin/logrotate -f $BKP_DIR/bkprotate.conf
 EXITVALUE=$?
 if [ $EXITVALUE != 0 ]; then
     /usr/bin/logger -t bkp-rotate "ALERT exited abnormally with [$EXITVALUE]"
 fi
 exit 0

Change the right to executable.

 chmod +x /home/bakcup-user/backup/bkprotate.sh

And now add this into the crontab.

 00 07 * * * /home/bakcup-user/backup/bkprotate.sh

Restore

Ref