User:Flominator/Backup MW

From MediaWiki.org

Jump to: navigation, search

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.

[edit] backup.sh

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

#!/bin/bash
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/ -x  backup/
rm ${FNAME}.sql

Bonus (not required): Keep only five backup files

#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

[edit] backup.php

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

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

[edit] import.bat

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 http://www.info-zip.org/UnZip.html 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%
 
%drive%
CD %exefolder%
xampp_stop
xampp_start
 
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 [http://www.info-zip.org/UnZip.html 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
)
PAUSE

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 set:

 [[$wgHashedUploadDirectory]] = true;
Personal tools