Friday, May 3, 2013

Backing up a Wiki (MediaWiki)

So a friend is working with a wiki and was asking about backing up and moving if necessary. So I thought I'd put out what I've done with mine. copy the script and name it backupwiki.sh or something.

Note that I got the bulk of this from here: http://www.mediawiki.org/wiki/User:Flominator/Backup_MW

#!/bin/bash
FNAME=`date +%Y-%m-%d`
mysqldump -u username --password=pwd --add-drop-table -B my_wiki > ${FNAME}.sql
zip -r /media/external1/bkup/wikibackup/${FNAME}.zip images/ ${FNAME}.sql LocalSettings.php extensions/
rm ${FNAME}.sql

cd /media/external1/bkup/wikibackup
 
#Count files in directory (hidden files (filename starts with a dot) are ignored) 
file_count=`ls | wc -l` 
 
#Do until there are more than or equal 6 files present
while [ $file_count -ge 6 ]
do
        #you can save deleted filenames in variable (e.g. for deleting files also in backup directory)
        #not recommended for filenames with space(s)
        del_files="${del_files} `ls | head -n 1`"
        #Delete alphabetically oldest file (ls sort by name is default)
        rm `ls | head -n 1` 
        #Count files again
        file_count=`ls | wc -l`
done

So what this does is dump the mysql database and copy the relevant files/directories you'll need.

Lets look at it in depth:

#!/bin/bash
FNAME=`date +%Y-%m-%d`
mysqldump -u username --password=pwd --add-drop-table -B my_wiki > ${FNAME}.sql

So here we are creating a variable FNAME with todays date in the format yyyy-mm-dd and then using mysqldump to dump the database to a sql file. Replace username and pwd with your corresponding mysql username/password. Also replace my_wiki with the name of your wiki database name.

Note that you want to make sure this file whateveryoucallit.sh is not readable by anyone but root; root also needs execute ability. Otherwise everyone can see your username/password for mysql. Not a smart thing.

zip -r /media/external1/bkup/wikibackup/${FNAME}.zip images/ ${FNAME}.sql LocalSettings.php extensions/
rm ${FNAME}.sql

Next we will create a zip file with FNAME variable so we know what date the backup is and will include in that the sql file and other relevant files and folders. You should create a directory to hold these backups and use the path to that in place of where I have /media/external1/bkup/wikibackup. (yes I use an external drive for my backups)

cd /media/external1/bkup/wikibackup
 
#Count files in directory (hidden files (filename starts with a dot) are ignored) 
file_count=`ls | wc -l` 
 
#Do until there are more than or equal 6 files present
while [ $file_count -ge 6 ]
do
        #you can save deleted filenames in variable (e.g. for deleting files also in backup directory)
        #not recommended for filenames with space(s)
        del_files="${del_files} `ls | head -n 1`"
        #Delete alphabetically oldest file (ls sort by name is default)
        rm `ls | head -n 1` 
        #Count files again
        file_count=`ls | wc -l`
done

This last part of the script needs to have you replace /media/external1/bkup/wikibackup with your path as well. It will make sure that we only keep 5 days of backups.

And last you want to edit root's crontab (I run it as root since I set the file to only be readable/executable by root.) and add this script. If you set it up to run every day, you'll have the last 5 days for backups. If you set it to once a week, you'll have the last 5 weeks backups. Your choice. This is a simple script and you can also use this script one time if you need to backup to move your wiki to another host.

Hope this helps...