User:Flominator/Backup MW


The following combination of scripts I used in order to backup a MediaWiki from a linux machine and to copy it to a windows machine running XAMPP. Feel free to copy them and put them into the root folder of your MediaWiki installation (where your LocalSettings.php is stored as well).[edit]

This file packs a database dump, the contents of the folders images and extensions and LocalSettings.php into a zip file name with today's date.

FNAME=`date +%Y-%m-%d`
mysqldump --database DATABASE_NAME -u USERNAME -pPASSWORD --add-drop-table -B > ${FNAME}.sql
zip -r ./backup/${FNAME}.zip images/ ${FNAME}.sql LocalSettings.php extensions/
rm ${FNAME}.sql

Bonus (not required): Keep only five backup files and delete the rest

# careful here .. if this fails you'll delete files from the current directory instead.
# Some checking might be in order, especially if you run this script from somewhere else
cd backup

#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 ]
        #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`


This file creates a backup using and enables the user to download the backup via http.

 $filename = strftime("%y-%m-%d");
 header("Content-Type: application/octet-stream");
 header("Content-Disposition: attachment; filename=WIKINAME_".$filename.'.zip');
 echo readfile("backup/".$filename.".zip");


This file is used to import the wiki at the windows machine. Just drag the file downloaded from backup.php on a link to import.bat or start it with import FILENAME. You also need the tool unzip.exe from for this.

@echo off
SET drive=C:
SET exefolder=%drive%\xampp
SET folder=%exefolder%\htdocs\wikifolder
SET temp=%folder%\temp
SET username=root
SET password=proot
SET database=wikidb

MD %temp%
ECHO Copy %1 to %temp%
COPY %1  %temp%

CD %exefolder%
ECHO Changing current directory to %folder%
CD %folder%
ECHO Deleting old folder images
RD /s /q images

ECHO Deleting old folder extensions
RD /s /q extensions

ECHO Unzipping file by using [ unzip]
CD temp
FOR /F "delims=!" %%n in ('dir *.zip /b') DO (unzip -o  %%n -d "%folder%"
 del %%n

CD ..
ECHO Deleting old database
%exefolder%\mysql\bin\mysql -u %username% -p%password% -e "drop database %database%;" 

ECHO Creating database
%exefolder%\mysql\bin\mysql -u %username% -p%password% -e "create database %database%;" 

FOR /F "delims=!" %%m in ('dir *.sql /b') DO (echo Importing from %%m
 %exefolder%\mysql\bin\mysql -u %username% -p%password% -D %database% < %%m
 del %%m

Warning: Make sure that there are no other ZIP or SQL files within the root directory of the wiki

If you have broken imagelinks, and are not able to create thumbnails afterwards, simply edit LocalSettings.php and enable $wgHashedUploadDirectory:

 $wgHashedUploadDirectory = true;