Jump to content

User:Darizotas/MediaWiki Backup Script for Windows

From mediawiki.org

Backup in Windows

[edit]

Following the example given by the script User:Flominator/Backup MW, I decided to write a new command line script (after doing it, it becomes in two scripts) in order to create backups in Windows, from now on it'll be named Wiki-WinBackup. Wiki-WinBackup creates backups from:

  1. Database. Where is located most of the critical information. It worths mentioning that MySQL 5.0.x is used as backend. Therefore, the script will use the MySQL Dump command line tool.
  2. Files. As you will see, the files to be included can be configured easily. By default:
    • LocalSettings.php.
    • images directory.
    • extensions directory.

On top of that, this script will create full XML backups as well.

Requirements

[edit]

The following scripts require to have installed:

Tested in Windows XP, Windows 2003 Server R2, Windows 2000 Advanced Server.

Architecture

[edit]

The Wiki-Backup architecture is based on the following files:

  • A #Text file that contains the files and directories to be added in the zip file.
  • #Helper script, which is responsible of:
    • Creating the MySQL Dump file and create a zip file containing the previous dump file and the given list of files/directories included in the #Text file to add.
    • Deleting the obsolete backup files.
  • #Main script, which is responsible to create the XML Dump and invoke the #Helper script.

Text file

[edit]

The text file will contain the complete path to the files/directories to be added in the backup zip file. One file/directory per line, following the 7-zip file list format. For example,

"MediaWiki-Folder\LocalSettings.php"
"MediaWiki-Folder\images\"
"MediaWiki-Folder\extensions\"

If you are using a MediaWiki version prior to 1.16, then you might want to add "MediaWiki-Folder\AdminSettings.php". Also uncomment the lines with AdminSettings.php in the batch file.

Where MediaWiki-Folder is your Mediawiki installation folder. Remember the path must be quoted whenever it contains spaces.

Helper script

[edit]

As a result of executing this script (let's name it zip-backup.cmd), a numbered zip file is created. Therefore, zip-backup will perform the following tasks:

  • Create the MySQL dump file
  • Create the zip file with the given contents and the MySQL dump file
  • Delete the obsolete files. It is understood by an obsolete file the one which sequence number exceeds the maximum to keep.

Let's see how to use it:

zip-backup output-prefix output-path list-files string-conn [max-allowed]

Where,

output-prefix Prefix of the zip filename. It's recommended not use spaces, if not, it must be quoted. Eg.: mybackup
output-path Path where to save the zip file. It must be quoted, whenever contains spaces. Eg.: "C:\Program Files\"
list-files Complete path to the filename that contains the files/directories to include in the zip file. This file must followed the 7-zip file list format
string-conn String connection with the following format 'schema@user:pass'
max-allowed (Optional) Maximum number of backups to keep.

Example:

zip-backup "MyBackup" "C:\My Backups\" "C:\Program Files\ListMyBackup.txt" "schema@user:password" 10

This will create C:\My Backups\MyBackup-1.zip file containing the files/directories given at C:\Program Files\ListMyBackup.txt. And only the last 10 created zip files will be kept. Now, let's see the source code. Just copy it in a text file and rename it to zip-backup.cmd.

:: Creates a zip file from the list file given and from the MySQL dump file.
:: The created zip file will be named with the given preffix and
:: a sequence number. Eg.:
::   <backup>-<sequence_number>.zip
::
:: This script will also delete those obsolete zip files, that is
:: those which sequence number exceeds the maximum allowed.
::
:: Copyright (C) 2009  Dario Borreguero
:: 
:: This program is free software: you can redistribute it and/or modify
:: it under the terms of the GNU General Public License as published by
:: the Free Software Foundation, either version 3 of the License, or
:: (at your option) any later version.
:: 
:: This program is distributed in the hope that it will be useful,
:: but WITHOUT ANY WARRANTY; without even the implied warranty of
:: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
:: GNU General Public License for more details.
:: 
:: You should have received a copy of the GNU General Public License
:: along with this program.  If not, see <http://www.gnu.org/licenses/>.

@echo off
cls
echo zip-backup  Copyright (C) 2009  Dario Borreguero
echo This program comes with ABSOLUTELY NO WARRANTY; This is free software, 
echo and you are welcome to redistribute it under certain conditions;
echo See http://www.gnu.org/licenses/.
echo.
echo Starting zip-backup %date% - %time:~0,8%

:: Output preffix
if "%~1"=="" goto :ERR_USE
:: Output path
if "%~2"=="" goto :ERR_USE
:: File list
if "%~3"=="" goto :ERR_USE
:: Database string connection
if "%~4"=="" goto :ERR_USE
:: Maximum allowed
if "%~5"=="" (
  set /A old=0
) else (
  set /A old=%5
)

setlocal enabledelayedexpansion

:: 7-zip exists
set z7="C:\Program Files\7-Zip\7z.exe"
if not exist %z7% (
  ECHO 7-zip can't be located at %z7%
  goto :END
)
:: mysqldump exists
set mysqldump="C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump.exe"
if not exist %mysqldump% (
  ECHO mysqldump can't be located at %mysqldump%
  goto :END
)
echo [%time:~0,8%] Creating dump file...
:: Retrieves user/pass and schema.
for /F "tokens=1,2 delims=@" %%F in ("%4") do (
  set schema=%%F
  for /F "tokens=1,2 delims=:" %%H in ("%%G") do set usr=-u %%H -p%%I
)
set options=--verbose --quick --opt -C -e
:: MysQL backup
%mysqldump% %options% %usr% --databases %schema% > "%~2\%1.sql"
if %errorlevel% neq 0 goto :end
echo [%time:~0,8%] Dump file done

:: Last sequence number.
set /A k=0
for /F "tokens=2 delims=-." %%A IN ('dir /B "%~2\%1-*.zip"') do if %%A gtr !k! set /A k=%%A
set /A k=%k%+1

:: Compressing
echo [%time:~0,8%] Creating zip file...
call %z7% a -tzip "%~2\%1-%k%.zip" @%3 "%~2\%1.sql"
if %errorlevel% neq 0 if %errorlevel% neq 1 goto :END
del /Q /F "%~2\%1.sql"
echo [%time:~0,8%] Zip file done

:: Delete obsolete zip files
if %old% gtr 0 (
  echo [%time:~0,8%] Deleting obsolete files...
  set /A max=%k%-%old%
  for /L %%B in (!max!,-1,1) do (
    if exist "%~2\%1-%%B.zip" (
      echo Deleting %~2\%1-%%B.zip
      del /Q /F "%~2\%1-%%B.zip"
    )
  )
  echo [%time:~0,8%] Deletion done
)
goto :END

:ERR_USE
echo Usage:
echo zip-backup output-preffix output-path list-files string-conn [max-allowed]
echo output-preffix Preffix of the zip filename. It's recommended not use spaces,
echo                if not, it must be quoted. Eg.: mybackup
echo output-path    Path where to save the zip file. It must be quoted, whenever
echo                contains spaces. Eg.: "C:\Program Files\"
echo list-files     Complete path to the filename that contains the 
echo                files/directories to include in the zip file. This file must 
echo                followed the 7-zip file list format
echo string-conn    String connection with the following format 'schema@user:pass'
echo max-allowed    (Optional) Maximum number of backups to keep.
echo.
echo Example:
echo zip-backup "MyBackup" "C:\My Backups\" "C:\Program Files\ListMyBackup.txt" 10
echo.
echo This will create 'C:\My Backups\MyBackup-1.zip' file containing the 
echo files/directories given at 'C:\Program Files\ListMyBackup.txt'. And only the
echo last 10 created zip files will be kept.
pause
goto :END
:END
echo End zip-backup %date% - %time:~0,8%

Main script

[edit]

The main script (let's name it wiki-backup.cmd) uses #Helper script to create and maintain the backup files. This script is just responsible to create the XML Backup and invoke the previous script. Let's see how to use it:

wiki-backup output-preffix output-path list-files string-conn [max-allowed]

Obviously, the needed parameters are exactly the same than the ones for #Helper script. Said that, let's see the code. As well as for the #Helper script just copy the source code in a text file and rename it as wiki-backup.cmd.

:: Creates a full Mediawiki backup in a zip file.
:: The backup contains:
:: - Files and directories listed in the given file.
:: - XML Dump.
:: - MySQL Dump.
:: This script requires zip-backup.cmd (in the same directory) to create 
:: the zip backup file and to deleted the obsoleted backups.
::
:: Copyright (C) 2009  Dario Borreguero
:: 
:: This program is free software: you can redistribute it and/or modify
:: it under the terms of the GNU General Public License as published by
:: the Free Software Foundation, either version 3 of the License, or
:: (at your option) any later version.
:: 
:: This program is distributed in the hope that it will be useful,
:: but WITHOUT ANY WARRANTY; without even the implied warranty of
:: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
:: GNU General Public License for more details.
:: 
:: You should have received a copy of the GNU General Public License
:: along with this program.  If not, see <http://www.gnu.org/licenses/>.

@echo off
cls
echo wiki-backup  Copyright (C) 2009  Dario Borreguero
echo This program comes with ABSOLUTELY NO WARRANTY; This is free software, 
echo and you are welcome to redistribute it under certain conditions;
echo See http://www.gnu.org/licenses/.
echo.
echo Starting wiki-backup %date% - %time:~0,8%

:: Output preffix
if "%~1"=="" goto :ERR_USE
:: Output path
if "%~2"=="" goto :ERR_USE
:: File list
if "%~3"=="" goto :ERR_USE
:: Database string connection
if "%~4"=="" goto :ERR_USE
:: Maximum allowed
if "%~5"=="" (
  set /A old=0
) else (
  set /A old=%5
)

:: PHP exists
set php="C:\PHP\php.exe"
if not exist %php% (
  ECHO PHP can't be located at %php%
  goto :END
)

:: DumpBackup exists
set wikiroot="MediaWiki-Folder"
set dump=%wikiroot%\maintenance\dumpBackup.php
if not exist %dump% (
  ECHO %dump% file does not exist
  goto :END
)

:: AdminSettings.php exists
:: http://www.mediawiki.org/wiki/Manual:DumpBackup.php
::removed_in_MediaWiki_1.16 if not exist %wikiroot%\AdminSettings.php (
::removed_in_MediaWiki_1.16   ECHO AdminSettings.php can't be located at %wikiroot%
::removed_in_MediaWiki_1.16   goto :END
::removed_in_MediaWiki_1.16 )

echo [%time:~0,8%] Creating XML Dump file...
%php% -d error_reporting=E_ERROR %dump% --full > "%~2\%1.xml"
if %errorlevel% neq 0 goto :END
echo [%time:~0,8%] XML Dump file done!

:: Creates the file list complete
set filelist="%~dp0zip-backup-complete_wiki.txt"
for /F %%a in (%3) do echo %%a >> %filelist%
echo "%~2\%1.xml" >> %filelist%
call zip-backup.cmd %1 %2 %filelist% %4 %old%
del /Q /F %filelist%
del /Q /F "%~2\%1.xml"
goto :END

:ERR_USE
echo Usage:
echo wiki-backup output-preffix output-path list-files string-conn [max-allowed]
echo output-preffix Preffix of the zip filename. It's recommended not use spaces,
echo                if not, it must be quoted. Eg.: mybackup
echo output-path    Path where to save the zip file. It must be quoted, whenever
echo                contains spaces. Eg.: "C:\Program Files\"
echo list-files     Complete path to the filename that contains the 
echo                files/directories to include in the zip file. This file must 
echo                followed the 7-zip file list format
echo string-conn    String connection with the following format 'schema@user:pass'
echo max-allowed    (Optional) Maximum number of backups to keep.
echo.
echo Example:
echo wiki-backup "MyBackup" "C:\My Backups\" "C:\Program Files\ListMyBackup.txt" 10
echo.
echo This will create 'C:\My Backups\MyBackup-1.zip' file containing the 
echo files/directories given at 'C:\Program Files\ListMyBackup.txt'. And only the
echo last 10 created zip files will be kept.
pause
:END
echo End wiki-backup %date% - %time:~0,8%

Warning: Please, notice you must set the environment variables wikiroot (see line set wikiroot="MediaWiki-Folder" in wiki-backup.cmd) and php (see line set php="C:\PHP\php.exe" in wiki-backup.cmd) to the values correct for your machine.

Automating the backups

[edit]

Finally, it's very important to automate the execution of Wiki-WinBackup. This can be accomplished by creating a Windows scheduled task (Start Menu -> Control Panel -> Scheduled tasks).

Recovery

[edit]

Not yet implemented.


If true then where is the value of this back process? — Preceding unsigned comment added by 12.145.89.10 (talkcontribs) 2009-10-15T21:16:42‎

There's always the option to restore manually... 193.244.33.47 09:43, 5 August 2011 (UTC)
If you want to backup the content of your Wiki every day, you will know the value. Tony Mach (talk) 11:05, 26 August 2014 (UTC)

See also

[edit]