Manual:Backing up a wiki/Duesentrieb's backup script

From Linux Web Expert

Script originally created by User:Duesentrieb: MediaWiki backup

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.
User:Megam0rf 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.
User:Robkam edited option for a default character set, and set it to binary, and other minor tweaks.
User:Dangoakachan fork a new backup script, pass options in command line, clone it from github.
Modified by Christian Estelmann: added '-e' to echo-commands to enable interpretation of backslash escapes
User:GregRundlett command line version compatible with ~/.my.cnf and includes VCS content fork me on github

Note* Be sure to replace --user=XXXX --password=XXXX with your user/pass, unless your credentials are in .my.cnf.
Check your wiki's LocalSettings.php for the value of parameter --default-character-set. If this is wrong the wiki database may fail to restore properly.

#!/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             #
# 3rd Mod: http://www.mediawiki.org/wiki/User:Robkam               #
# 4th Mod: Christian Estelmann                                     #
#							           #
# 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
chrset="--default-character-set=binary"         # latin1, utf8, binary, etc. Check your wiki's LocalSettings.php 

##################
# 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 -e "Wiki backup:\n-------------"
echo -e " Database:  $wikidb\n Directory: $wikidir\n Backup to: $backupdir"
echo -e "\ncreating database dump \t$dbdump..."
mysqldump $chrset $mysqlopt "$wikidb" | gzip -9 > "$dbdump" || exit $?
 
echo -e "creating file archive \t$filedump..."
cd "$wikidir"
tar --exclude-vcs -zcf "$filedump" . || exit $?

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

##########################################
# Put the wiki back into read/write mode #
##########################################
 
echo
echo "Bringing the wiki out of Read-only mode..."

grep "?>" "$wikidir"/LocalSettings.php > /dev/null
if [ $? -eq 0 ];
then
        sed -i "s/$maintmsg?>/?>/ig" "$wikidir"/LocalSettings.php
else
        sed -i "s/$maintmsg//ig" "$wikidir"/LocalSettings.php
fi
 
##########################################
 
echo
echo "Done!"
echo "Files to copy to a safe place:"
echo "$dbdump,"
echo "$filedump,"
echo "$xmldump"

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