User:Kaotic/WikiBackup

script originally created by User:Duesentrieb: [1]

I've added 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.

if your LocalSettings.php has a ?> at the end be sure to remove it as this script simply adds the read only option to the end of the file then removes the last line of the file afterwords, I know this is probably not the best way to accomplish this and would welcome any suggestions to streamline this even further. Thanks!

See the discussion page for a suggestion using wgReadOnlyFile.

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.                                                   #
#                                                                  #
# 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/www/                           # 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..."

echo "\$wgReadOnly = 'Dumping Database, Access will be restored shortly';" >> "$wikidir"/LocalSettings.php

####################################

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

echo
echo "Wiki backup. Database: $wikidb; Directory: $wikidir; Backup to: $backupdir"
echo
echo "creating database dump $dbdump..."
mysqldump --default-character-set=latin1 $mysqlopt "$wikidb" | gzip > "$dbdump" || exit $?

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

echo
echo "creating file archive $filedump..."
cd "$wikidir"
tar --exclude .svn -zcf "$filedump" . || exit $?

##########################################
# Put the wiki back into read/write mode #
##########################################

echo
echo "Bringing the wiki out of Read-only mode..."

head -n-1 "$wikidir"/LocalSettings.php > "$backupdir"/LocalSettings.bak
cat "$backupdir"/LocalSettings.bak > "$wikidir"/LocalSettings.php
rm "$backupdir"/LocalSettings.bak

##########################################

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

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