[server-admins] backup-wiki.sh script
Jonathan Marsden
jmarsden at fastmail.fm
Mon Jul 27 00:22:01 MST 2009
Jonathan Marsden wrote:
> I'll put together a quick shell script to:
>
> (a) back up the wiki
> (b) run yum update
> (c) run the update.php script
Here is a script that backs up the mediawiki (database, database-as-xml,
and files). In case email wraps it horribly, this file is also at
/home/jmarsden/backup-wiki.sh on www.crosswire.org.
This script is reasonably tested and working. It makes no attempt to
rotate the backups, nor to rsync them offsite, etc. which a "real"
backup system would do. I'd expect you/we are using rsnapshot or
something similar to do regular offsite backups?
A smaller simpler script that uses this one to do (a), (b) and (c) above
will follow.
Jonathan
#!/bin/bash
# backup-wiki.sh - Backs up a mediawiki.
# Database and files. Optional parameter is backup directory.
#
# Author: Jonathan Marsden <jmarsden at fastmail.fm>
# Date: 2009-07-26
# Global config variables
DEFAULTBACKUPDIR="/usr/local/backup/mediawiki"
BACKUPDIR=${1:-"$DEFAULTBACKUPDIR"} # Where to put backups
TIMESTAMP=$(/bin/date +%F_%H%M%S)
WIKIDIR="/usr/share/mediawiki" # Location of wiki files
locals="$WIKIDIR"/LocalSettings.php
admins="$WIKIDIR"/AdminSettings.php
xmlscript="maintenance/dumpBackup.php"
# Extract database info from PHP settings files
DBserver=$(grep '^\$wgDBserver' $locals |cut -d = -f2 |cut -d\" -f2)
DBname=$(grep '^\$wgDBname' $locals |cut -d = -f2 |cut -d\" -f2)
DBauser=$(grep '^\$wgDBadminuser' $admins |cut -d = -f2 |cut -d\' -f2)
DBapw=$(grep '^\$wgDBadminpassword' $admins |cut -d = -f2 |cut -d\' -f2)
# Create backup filenames
DBFILE="$BACKUPDIR"/$(hostname)-"$DBname"-"$TIMESTAMP".sql.gz
XMLFILE="$BACKUPDIR"/$(hostname)-"$DBname"-"$TIMESTAMP".xml.gz
TARFILE="$BACKUPDIR"/$(hostname)-"$DBname"-"$TIMESTAMP".tar.gz
# Make sure we have a backup directory & protect from prying eyes
mkdir -p "$BACKUPDIR"
chmod 0700 "$BACKUPDIR"
umask 077 # So backup files are not readable by others
# Create list of options for mysqldump
MYSQLOPTS=" -h $DBserver -u $DBauser -p$DBapw --add-drop-table "
# Back up the mediawiki database
mysqldump $MYSQLOPTS "$DBname" |gzip -9 >"$DBFILE" || exit $?
# Back up wiki as XML
(cd "$WIKIDIR" && php "$xmlscript" --full)|gzip -9 >"$XMLFILE" ||exit $?
# Back up wiki filesystem as a tarball
tar zcCf "$WIKIDIR" "$TARFILE" . || exit $?
More information about the server-admins
mailing list