Friday, March 1, 2013

Back-Up a Moodle Installation and Upload it to an FTP Server

Are you a Moodle administrator (on Windows) who is looking for a batch file to automate the process of taking a back-up of your Moodle server and uploading that back-up to an FTP server? Look no further; you have come to the right place!

Simply download the batch file below on the machine hosting your Moodle server, modify it with your information (i.e. paths to the relevant directories, etc.), then run the batch file. The batch file is hosted on Rapidshare.com and can be downloaded from there.




For the batch file to work, I make the following assumptions:
  1. You are running your Moodle server in a Windows environment, and you have administrator access on the machine hosting your Moodle server.
  2. You are using MySQL Server as your database server.
  3. You have installed 7-Zip (http://www.7-zip.org/) on the machine hosting your Moodle server.
The full source code (with detailed comments) of the batch file is as follows:

@echo off

REM ======================== DEFINE SETTINGS AS REQUIRED ========================
REM Set timestamp in DDMMYYYY format
set TIMESTAMP=%date:~7,2%%date:~4,2%%date:~10,4%
REM Set the location of the 7-Zip executable
set ZIP="C:\Program Files\7-Zip\7z.exe"
REM Set the output folder where the zip file will be stored (temporarily)
set OUTPUT_FOLDER="C:\outputfolder"
REM Set location of the moodledata folder
set MOODLE_DATA="D:\moodledata"
REM Set name of zip file
set ARCHIVE=%OUTPUT_FOLDER%\moodle_backup_%TIMESTAMP%.zip
REM Set location of where your Moodle site resides
set MOODLE_CODE="C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\moodle"
REM Set location of the mysqldump executable
set DB_MYSQLDUMP="C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe"
REM Set the database parameters: -u<username> -p<password> <database_name>
set DB_PARAMETERS=-uusername -ppassword moodledb
REM Set name of SQL dump file
set DB_DUMP_FILE="%OUTPUT_FOLDER%\moodledb_%TIMESTAMP%.sql"
REM Set the location of your FTP server (IP address is also fine)
set FTP_SERVER=myftp.network.com
REM Set FTP username - the user should have write access
set FTP_USER=ftpuser
REM Set FTP user's password
set FTP_PASSWORD=ftppassword
REM Set the FTP path to where you want to copy the archive file starting from the FTP root
set FTP_DESTINATION_DIRECTORY="/path/to/FTP/destination/"

REM ================= DO NOT MODIFY ANYTHING BELOW THIS LINE ==================
REM Backup Moodle data directory
%ZIP% a -tzip %ARCHIVE% %MOODLE_DATA%
REM Backup the Moodle code
%ZIP% a -tzip %ARCHIVE% %MOODLE_CODE%
REM Backup the Moodle database
%DB_MYSQLDUMP% %DB_PARAMETERS% > %DB_DUMP_FILE%
%ZIP% a -tzip %ARCHIVE% %DB_DUMP_FILE%
REM Upload via FTP
echo user %FTP_USER%> %OUTPUT_FOLDER%\ftpcmd.dat
echo %FTP_PASSWORD%>> %OUTPUT_FOLDER%\ftpcmd.dat
echo bin>> %OUTPUT_FOLDER%\ftpcmd.dat
echo cd %FTP_DESTINATION_DIRECTORY%>> %OUTPUT_FOLDER%\ftpcmd.dat
echo put %ARCHIVE%>> %OUTPUT_FOLDER%\ftpcmd.dat
echo quit>> %OUTPUT_FOLDER%\ftpcmd.dat
ftp -n -s:%OUTPUT_FOLDER%\ftpcmd.dat %FTP_SERVER%
REM Clean up
del %OUTPUT_FOLDER%\%DB_DUMP_FILE%
del %OUTPUT_FOLDER%\ftpcmd.dat
del %OUTPUT_FOLDER%\%ARCHIVE%

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete