Jump to content

User:Megam0rf/WikiBackup

From mediawiki.org
(Redirected from User:Megam0rf)

script originally created by User:Duesentrieb: [1]
first modified by User:Kaotic to add the ability for the script to place the wiki into read only mode during the database dump and then restore access after the dump has been completed.

I added a few lines to circumvent Kaotic's problem with ?> at the end of the LocalSettings.php which had to be removed prior to running the script.

Note* Be sure to replace --user=XXXX --password=XXXX with your user/pass, unless your credentials are in .my.cnf.

#!/bin/sh
 
####################################################################
#                                                                  #
# Basic Backup Script for MediaWiki.                               #
# Created by Daniel Kinzler, brightbyte.de, 2008                   #
#                                                                  #
# This script may be freely used, copied, modified and distributed #
# under the sole condition that credits to the original author     #
# remain intact.		   	   			     #
#				   				     #
# 1st Mod: http://www.mediawiki.org/wiki/User:Kaotic               #
# 2nd Mod: http://www.mediawiki.org/wiki/User:Megam0rf             #
#							             #
# This script comes without any warranty, use it at your own risk. #
#                                                                  #
####################################################################
 
###############################################
# CHANGE THESE OPTIONS TO MATCH YOUR SYSTEM ! #
###############################################
 
wikidb="wikidb"                             	# the database your wiki stores data in
mysqlopt="--user=XXXX --password=XXXX"          # usually empty if username and password are provided in your .my.cnf
 
wikidir=/var/lib/mediawiki                     	# the directory mediawiki is installed in
backupdir=~/bak                             	# the directory to write the backup to
 
##################
# END OF OPTIONS #
##################
 
timestamp=`date +%Y-%m-%d`
 
####################################
# Put the wiki into Read-only mode #
####################################
 
echo
echo "Putting the wiki in Read-only mode..."

maintmsg="\$wgReadOnly = 'Dumping Database, Access will be restored shortly';"

grep "?>" "$wikidir"/LocalSettings.php > /dev/null
if [ $? -eq 0 ];
then
	sed -i "s/?>/$maintmsg?>/ig" "$wikidir"/LocalSettings.php
else
	echo "$maintmsg?>" >> "$wikidir"/LocalSettings.php
fi 

####################################
 
dbdump="$backupdir/wiki-$timestamp.sql.gz"
filedump="$backupdir/wiki-$timestamp.files.tgz"
xmldump="$backupdir/wiki-$timestamp.xml.gz"

 
echo
echo "Wiki backup:\n-------------"
echo " Database:  $wikidb\n Directory: $wikidir\n Backup to: $backupdir"
echo "\ncreating database dump \t$dbdump..."
mysqldump --default-character-set=latin1 $mysqlopt "$wikidb" | gzip > "$dbdump" || exit $?
 
echo "creating file archive \t$filedump..."
cd "$wikidir"
tar --exclude .svn -zcf "$filedump" . || exit $?

echo "creating XML dump \t$xmldump..."
cd "$wikidir/maintenance"
php -d error_reporting=E_ERROR dumpBackup.php --full | gzip > "$xmldump" || exit $?

##########################################
# Put the wiki back into read/write mode #
##########################################
 
echo
echo "Bringing the wiki out of Read-only mode..."
 
sed -i "s/$maintmsg?>/?>/ig" "$wikidir"/LocalSettings.php

##########################################
 
echo
echo "Done!"
echo "Files to copy to a safe place:"
echo "$dbdump,"
echo "$filedump,"
echo "$xmldump"

#######
# END #
#######