Censorship banner.png

Backup strutture

From Wiki - Hipatia
Jump to: navigation, search

This page is meant to collect a short reference on how to backup our structures and sites, for site administrators.

 Italiano

Wiki

For a complete reference please refer to: http://www.mediawiki.org/wiki/Manual:Backing_up_a_wiki

Sample working script

This is a script which can be used to backup the mediawiki installation. Programs needed are: bash, grep, mysqldump, php, tar, gzip,

#!/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: Kaotic                                                #
# 2nd Mod: 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 #
#######

Gallery

  1. Change your directory to the directory you want to dump things to:
user@gnu:~> files/g2_backup 
  1. . Use mysqldump (man mysqldump is available):
user@gnu:~/files/g2_backup> mysqldump --add-drop-table -h mysqlhostserver -u mysqlusername -p databasename (tablename tablename tablename) | bzip2 -c > g2.bak.sql.bz2
Enter password: (enter your mysql password)
user@gnu~/files/g2_backup>

Example:

mysqldump --add-drop-table -h g2.example.net -u dbocodex -p dbg2 | bzip2 -c > g2.bak.sql.bz2
Enter password: my-password
user@gnu~/files/g2_backup>

The bzip2 -c after the | (pipe) means the backup is compressed on the fly. It does in one line the same thing that these two commands do:

mysqldump --add-drop-table -h g2.example.net -u dbocodex -p dbg2 > g2.bak.sql
bzip2 g2.bak.sql

To email the backup:

mysqldump --add-drop-table -h g2.example.net -u dbocodex -p dbg2 |  gzip -c | uuencode gallery.sql.gz | mail -s "Gallery Database Backup" email@example.net