Skip to content

Instantly share code, notes, and snippets.

@nramsbottom
Created May 13, 2014 12:31
Show Gist options
  • Select an option

  • Save nramsbottom/c146a39d7d980e44f6de to your computer and use it in GitHub Desktop.

Select an option

Save nramsbottom/c146a39d7d980e44f6de to your computer and use it in GitHub Desktop.
Simple backup script using 7zip
@REM Requires the following tools
@REM Blat <http://sourceforge.net/projects/blat>
@REM Dttm <https://gist.github.com/nramsbottom/2296fffdf49fcb824fe4>
@REM Curl <http://curl.haxx.se/>
@SETLOCAL
@REM Set this to 1 to show all script output on the console
@REM which will help when debugging.
@SET DEBUG_MODE=0
@IF %DEBUG_MODE% NEQ 1 (
@ECHO OFF
@CLS
)
REM Get current date in the format "yyyyMMdd" in all lowercase
dttm.exe yyyyMMdd /lowercase > today.txt
SET /P DTTM=<today.txt
DEL today.txt
REM Does the specified configuration exist?
IF EXIST %1.config.cmd (
REM Execute it.
CALL %1.config.cmd
) else (
REM Cannot proceed without configuration.
ECHO Configuration for '%1' not found.
PAUSE
EXIT /B 10
)
SET PATH=%PATH%;%SEVENZIP_INSTALL_PATH%
7z.exe a -t7z -o"%OUTPUT_DIRECTORY%" -w"%WORKING_DIRECTORY%" "%OUTPUT_FILENAME%" "%INPUT_SPECIFICATION%" 2>&1> backup.log
IF %ERRORLEVEL% NEQ 0 GOTO EndErr
:EndOk
ECHO Finished with no error.
GOTO End
:EndErr
ECHO Backup failed. Sending email...
blat.exe backup.log -to %NOTIFY_EMAIL_RECIPIENT% -serverSMTP %NOTIFY_EMAIL_SMTPSERVER% -f %NOTIFY_EMAIL_ORIGINATOR% -subject "%NOTIFY_EMAIL_SUBJECT%" 2>&1> NUL
IF %ERRORLEVEL% NEQ 0 (
ECHO Failed to send notification email.
PAUSE
EXIT /B 20
)
GOTO End
:End
ENDLOCAL
REM *** BEGIN CONFIGURATION ***
SET NOTIFY_EMAIL_RECIPIENT=myaddress@nospam.example.org
SET NOTIFY_EMAIL_ORIGINATOR=originator@nospam.exampe.org
SET NOTIFY_EMAIL_SUBJECT=Backup failed
SET NOTIFY_EMAIL_SMTPSERVER=smtp.server.example.org
SET WORKING_DIRECTORY=%TEMP%
SET INPUT_SPECIFICATION=C:\Users
SET SEVENZIP_INSTALL_PATH=c:\Program Files\7-Zip
SET OUTPUT_FILENAME_SUFFIX=UserBackup
SET OUTPUT_DIRECTORY=D:\Backups
SET OUTPUT_FILENAME=%DTTM%_%OUTPUT_FILENAME_SUFFIX%.7z
REM *** END CONFIGURATION ***
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment