Skip to content

Instantly share code, notes, and snippets.

@leekelleher
Created April 13, 2012 17:33
Show Gist options
  • Save leekelleher/2378617 to your computer and use it in GitHub Desktop.
Save leekelleher/2378617 to your computer and use it in GitHub Desktop.
Personal batch script to quickly back-up my MSSQL database and wwwroot files, (typically for use with Umbraco).
:: ###### BACK-UP JOB ######
@ECHO OFF
:: Set the constants
SET BACKUP_DIR=C:\path\to\_backup
SET TEMP_DIR=%BACKUP_DIR%\TEMP
SET TODAY=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%
SET ARCHIVE_DIR=%BACKUP_DIR%\%TODAY%
SET WWWROOT_NAME="ProjectName"
SET WWWROOT_SOURCE=C:\path\to\wwwroot
SET WWWROOT_TEMP=%TEMP_DIR%\wwwroot
SET MSSQL_NAME=localDatabaseName
SET MSSQL_TEMP=%TEMP_DIR%\%MSSQL_NAME%.bak
:: Create a new archive/back-up folder
ECHO Creating the archive folder '%TODAY%'
ECHO.
MKDIR %ARCHIVE_DIR%
ECHO Done.
ECHO.
::
:: ###### FILESYSTEM BACK-UP #####
::
:: Copy the wwwroot to temporary folder
ECHO Copying the 'wwwroot' to the temporary folder %WWWROOT_TEMP%
ECHO.
ROBOCOPY %WWWROOT_SOURCE% %WWWROOT_TEMP% /COPYALL /E /Z /XF *.log
ECHO Done.
ECHO.
:: 7z the wwwroot in the archive folder
ECHO Using 7-Zip to compress the %WWWROOT_TEMP% folder
ECHO.
"C:\Program Files\7-Zip\7z.exe" a -t7z -r %ARCHIVE_DIR%\%WWWROOT_NAME%.7z %WWWROOT_TEMP%
ECHO Done.
ECHO.
:: Delete the temporary wwwroot folder
ECHO Removing the temporary %WWWROOT_TEMP% folder
ECHO.
RMDIR /S /Q %WWWROOT_TEMP%
ECHO Done.
ECHO.
::
:: ###### MS-SQL BACK-UP #####
::
:: Back-up the MS-SQL database to the temporary folder
ECHO Backing-up the MS-SQL database to '%MSSQL_TEMP%'
ECHO.
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" -S .\SQL2008 -E -Q "BACKUP DATABASE %MSSQL_NAME% TO DISK='%MSSQL_TEMP%' WITH FORMAT"
:: TODO: The other MS-SQL databases
ECHO Done.
ECHO.
:: 7z the MS-SQL back-up file in the archive folder
ECHO Using 7-Zip to compress the MS-SQL database '%MSSQL_TEMP%'
ECHO.
"C:\Program Files\7-Zip\7z.exe" a -t7z %ARCHIVE_DIR%\%MSSQL_NAME%.7z %MSSQL_TEMP%
ECHO Done.
ECHO.
:: Delete the MS-SQL back-up file from the temporary folder
ECHO Deleting the MS-SQL back-up file '%MSSQL_TEMP%'
ECHO.
DEL /Q %MSSQL_TEMP%
ECHO Done.
ECHO.
::
:: ###### HOUSE-CLEANING #####
::
:: Delete the temporary folder
ECHO Deleting the temporary folder '%TEMP_DIR%'
ECHO.
DEL /Q %TEMP_DIR%
ECHO Done.
ECHO.
ECHO.
PAUSE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment