@ECHO OFF SETLOCAL ENABLEEXTENSIONS :: ******************************************************************** :: IMPORTANT: This script is not Officially supported in any way ! :: Use it at your own risk !!! :: ******************************************************************** :: NAME : 7Backup.bat :: DESCRIPTION : This script will help you automate the backup process :: of your data using 7zip compression program. :: When job is done a detailed report is produced. :: OS : Microsoft Windows 2000 (tested) :: Microsoft Windows XP (tested) :: Microsoft Windows Vista (tested) :: Microsoft Windows 2003 (tested) :: REQUIREMENTS : 7zip (http://www.7-zip.org/download.html) :: Junction v1.05 (http://technet.microsoft.com/en-us/sysinternals/bb896768.aspx) :: NTFS File System :: -------------------------------------------------------------------- :: Give credit to the following contributors: :: (please do not remove - add your name if you contribute) :: :: * Andrea Lanfranchi - Anlan (http://www.anlan.com) :: * www.dostips.com :: :: -- Version History – :: XX.XXX YYYYMMDD Author Description SET version=01.000-beta &:20091104 Anlan First release SET version=01.001-beta &:20091208 Anlan First release :: !! For a new version entry, copy the last entry down and modify Date, Author and Description SET version=%version: =% :: :: 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 2 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, write to the Free Software :: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA :: :: Those who do not know how to use snail mail: The GPL is here: :: http://www.gnu.org/licenses/gpl.html :: ==================================================================== :: VARIABLES - You can hardcode values here if you do not want to :: to pass them by command line. :: If you have to enter paths values which contain spaces :: DO NOT ENCLOSE THEM IN QUOTES !! :: -------------------------------------------------------------------- :: BK-TYPE :: Type of backup to perform. Possible values are : :: FULL : This option performs the archiving of all files in the :: selection paths regardless the state of their archive attribute. :: At successfull archiving operation all archive attributes :: for all archived files are lowered. :: DIFF : This option is generally used to perform the archiving of :: files in the selection paths that have changed after a previous :: backup (archive attribute set). :: At successfull archiving operation all archive attributes :: for all archived files are lowered. :: INCR : This option is generally used to perform the archiving of :: files in the selection paths that have changed after a previous :: full backup (archive attribute set). :: At successfull archiving operation all archive attributes :: for all archived files are left unchanged. SET BK-TYPE= :: BK-WORKDIR :: Which is the working directory to use. This value is very important as :: this path will be used to create the root point for junctions to :: selection directories. If it's not specified and it's not passed by :: command line arguments then we'll try to assume as working directory the :: path specified in the TEMP environment variable. SET BK-WORKDIR= :: BK-WORKDRIVE :: This value holds the drive letter to use for substitution of working :: directory. This trick is necessary to prevent the appearance of paths :: with an overall length exceeding 256 chars. Default setting is W but :: you can change this value using proper command line switch SET BK-WORKDRIVE=W :: BK-DIRLIST :: This is the pointer to the file containing the list of directories to :: back-up. That file have to be a normal text file and each directory should :: appear on a line of it's own (one directory per line). :: PAY ATTENTION !! Each selected directory will generate a junction point in :: working directory. If you select a directory which is above the working :: directory you'll create a recursion loop in selection with unwanted and :: dangerous results. ALWAYS CHECK YOUR WORKING DIRECTORY IS OUT OF THE :: SELECTION PATH !! :: :: CRITICAL NOTE !! IF, FOR ANY REASON, THE PROCEDURE SHOULD TERMINATE :: UNEXPECTEDLY, IT MAY HAPPEN THAT ALL JUNCTION POINTS IN WORKING DIRECTORY :: REMAIN UNDELETED. DO NOT DELETE JUNCTION POINTS WITH WITH EXPLORER AS IT :: TRAVERSES ALL JUNCTIONS THEREFORE DELETING ALL FILES IN SELECTION DIRECTORIES ! :: YOU ARE WARNED !! SET BK-DIRLIST= :: BK-DESTDIR :: This is the folder that will contain the backup archive. Consider this path :: as a sort of library that will contain all of your backups. The real archive name :: will be generated automatically. No quotes even if the value contains spaces. SET BK-DESTDIR= :: BK-ARCHIVEPREFIX :: This variable contains the prefix to use in archive name generation. :: By default this is equal to the COMPUTERNAME environment variable but :: you can choose whichever value you want with no spaces and no quotes. :: The suffix can help you mantain groups of backups and to handle :: retain policy, as described later. :: Archive name creation template is the following: :: %BK-ARCHIVEPREFIX%-%BK-TYPE%-YYYYMMDD-HH-mm.%BK-ARCHIVETYPE% SET BK-ARCHIVEPREFIX=%COMPUTERNAME% :: BK-ARCHIVETYPE :: This variable contains the type of archive you want to create. :: Value can be any of the supported values by 7zip for the -t switch :: except for bzip2 (which is not an archiver) :: :: 7z : default format with better compression :: gzip : very compatible with other programs :: zip : very compatible with other programs :: tar : Unix and linux compatible but only archiving (no compression) :: :: Archive name creation template is the following: :: %BK-ARCHIVEPREFIX%-%BK-TYPE%-YYYYMMDD-HH-mm.%BK-ARCHIVETYPE% SET BK-ARCHIVETYPE=7z :: BK-ROTATE :: This variable holds the number of historycal archive backups you :: want to keep on target media. An Empty value means no rotation :: will be performed after succesful archiving. :: For example: if you set this value to 3 then it means the 3 newest :: backups of the current type are kept on target media. :: For a "classic" 3 week incremental scheme we suggest to : :: set BK-ROTATION=3 for full backups (launched once a week) :: set BK-ROTATION=21 for incr backups (launched once per day) SET BK-ROTATE= :: BK-ARCHIVEPASSWORD :: Set this if you want to password protect your archive SET BK-ARCHIVEPASSWORD= :: BK-LOGFILE :: This variable holds the name of the file where to log operations :: By default it's stored in temp directory SET BK-LOG=%TEMP%\7Backup.log :: 7-zip executable path. :: If you have installed 7zip using standard path, then you do not need to :: change this SET BK-7ZBIN=%ProgramFiles%\7-zip\7z.exe :: 7-zip extra switches :: Here you can specify several switches supported by 7zip. :: Refer to 7zip documentation for a proper usage. :: By default we assume the following: :: -bd disable progress status :: -ssw archive locked files :: -mx9 maximum compression rate :: -mmt=on enable multithreading :: -mtc=on archive NTFS file infomations (not ACLS) :: -md=24m use 24m dictionary SET BK-7ZBINSW=-bd -ssw -mx9 -mmt=1 -md=24m -mtc=on :: Junction executable path. :: Find where Junction.exe is and set it here. If Junction.exe is in :: a directory within the PATH variable you can simply indicate :: junction.exe SET BK-JUNCTION=%ProgramFiles%\SysInternalsSuite\Junction.exe :: Wheter or not to log events to Computer's Application Event Log SET BK-EVENTLOG=1 :: Which is the event id to use when logging to event log SET BK-EVENTID=1000 :: -------------------------------------------------------------------- :: Preparing environment :: -------------------------------------------------------------------- TITLE 7backup (SET BK-STARTDIR=%CD%) (SET BK-DATETIME=) (SET BK-ARCHIVENAME=) :: -------------------------------------------------------------------- :: THIS IS WERE THE STUFF BEGINS :: -------------------------------------------------------------------- :: Before we can go any further we have to check 7zip is aboard and :: Junction is a valid executable IF NOT EXIST "%BK-7ZBIN%" ( ECHO. ECHO Unable to find and execute 7z executable. Are you sure you have downloaded it ? ECHO You can get it from http://www.7-zip.org/download.html ECHO. GOTO EOF ) SET _Test= FOR /F "tokens=*" %%a IN ('""%BK-7ZBIN%" | find /c /i "Igor Pavlov""') DO ( IF "%%a" NEQ "" (SET _Test=%%a) ) IF %_Test% LSS 1 ( ECHO. ECHO Unable to find and execute 7z executable. Are you sure you have downloaded it ? ECHO You can get it from http://www.7-zip.org/download.html ECHO. GOTO EOF ) IF NOT EXIST "%BK-JUNCTION%" ( ECHO. ECHO Unable to find and execute Junction.exe. Are you sure you have downloaded it ? ECHO You can get it from http://technet.microsoft.com/en-us/sysinternals/bb896768.aspx ECHO. GOTO EOF ) SET _Test= FOR /F "tokens=*" %%a IN ('""%BK-JUNCTION%" /accepteula | find /c /i "Mark Russinovic""') DO ( IF "%%a" NEQ "" (SET _Test=%%a) ) IF %_Test% LSS 1 ( ECHO. ECHO Unable to find and execute Junction.exe. Are you sure you have downloaded it ? ECHO You can get it from http://technet.microsoft.com/en-us/sysinternals/bb896768.aspx ECHO. GOTO EOF ) :Start TITLE 7backup - Init CALL :Init %* IF NOT "%ERRORLEVEL%"=="0" CALL :showUsage && GOTO EOF IF "%BK-EVENTLOG%"=="1" CALL :LogEvent INFORMATION %BK-EVENTID% "Application started at %DATE% %TIME%: %0 %*" :: Check Backup Type is defined IF NOT DEFINED BK-TYPE ( CALL :Trace ERROR %BK-EVENTID% "Backup type is not defined. Try --type" ) ELSE ( CALL :toLower BK-TYPE IF /I "%BK-TYPE%"=="copy" GOTO CheckWorkdir IF /I "%BK-TYPE%"=="full" GOTO CheckWorkdir IF /I "%BK-TYPE%"=="diff" GOTO CheckWorkdir IF /I "%BK-TYPE%"=="incr" GOTO CheckWorkdir CALL :Trace ERROR %BK-EVENTID% "Backup type is not defined. Try --type" && GOTO EOF ) :CheckWorkdir :: Check Workdir is defined and exists IF NOT DEFINED BK-WORKDIR (SET BK-WORKDIR=%TEMP%) IF NOT DEFINED BK-WORKDIR IF EXIST C:\TEMP (SET BK-WORKDIR=C:\TEMP) IF NOT DEFINED BK-WORKDIR ( CALL :Trace ERROR %BK-EVENTID% "Unable to determine working directory. Try --workdir" GOTO EOF ) ELSE ( IF NOT EXIST "%BK-WORKDIR%" ( CALL :Trace ERROR %BK-EVENTID% "Working directory %BK-WORKDIR% does not exist" GOTO EOF ) ) :CheckWorkDrive :: Check Workdrive is defined and is not mapped to any device IF NOT DEFINED BK-WORKDRIVE ( CALL :Trace ERROR %BK-EVENTID% "Unable to determine work drive letter. Try --workdrive" GOTO EOF ) ELSE ( IF EXIST "%BK-WORKDRIVE%\." ( CALL :Trace ERROR %BK-EVENTID% "Work drive letter %BK-WORKDRIVE% in use" GOTO EOF ) ) :CheckDirList :: Check directory selection file is defined and exist IF NOT DEFINED BK-DIRLIST ( CALL :Trace ERROR %BK-EVENTID% "Missing directory selection file. Try --dirlist" GOTO EOF ) ELSE ( IF NOT EXIST "%BK-DIRLIST%" ( CALL :Trace ERROR %BK-EVENTID% "Directory selection file %BK-DIRLIST% does not exist" GOTO EOF ) ) :CheckDestDir :: Check Workdir is defined and exists IF NOT DEFINED BK-DESTDIR ( CALL :Trace ERROR %BK-EVENTID% "Unable to determine destination directory. Try --destdir" GOTO EOF ) ELSE ( IF NOT EXIST "%BK-DESTDIR%" ( CALL :Trace INFORMATION %BK-EVENTID% "Destination directory "%BK-DESTDIR%" does not exist. Creating ..." MD "%BK-DESTDIR%" > NUL CALL :sleep 1 IF NOT EXIST "%BK-DESTDIR%" CALL :Trace ERROR %BK-EVENTID% "Failed to create destination directory "%BK-DESTDIR%"." && GOTO EOF ) ) :CheckArchivePrefix :: Check Archive suffix is defined IF NOT DEFINED BK-ARCHIVEPREFIX CALL :Trace ERROR %BK-EVENTID% "Archive prefix not specified. Try --prefix" && GOTO EOF :CheckArchiveType :: Check Archive Type is defined IF NOT DEFINED BK-ARCHIVETYPE ( CALL :Trace ERROR %BK-EVENTID% "Archive type is not defined. Try --archivetype" ) ELSE ( CALL :toLower BK-ARCHIVETYPE IF /I "%BK-ARCHIVETYPE%"=="7z" GOTO CheckLogFile IF /I "%BK-ARCHIVETYPE%"=="gzip" GOTO CheckLogFile IF /I "%BK-ARCHIVETYPE%"=="zip" GOTO CheckLogFile IF /I "%BK-ARCHIVETYPE%"=="tar" GOTO CheckLogFile CALL :Trace ERROR %BK-EVENTID% "Archive type %BK-ARCHIVETYPE% is not supported. Try --archivetype" && GOTO EOF ) :CheckLogFile :: Check logfile is defined IF NOT DEFINED BK-LOG CALL :Trace ERROR %BK-EVENTID% "Log file not specified. Try --logfile" && GOTO EOF :CheckRotation IF NOT DEFINED BK-ROTATE SET BK-ROTATE=0 SET /A y=%BK-ROTATE% * 1 SET BK-ROTATE=%y% IF %BK-ROTATE% LSS 1 CALL :Trace ERROR %BK-EVENTID% "Rotation must be a positive number greater equal 1. Try --rotate" && GOTO EOF :: Calculate Date/Time stamp to apply to archive name (YYYYMMDD-HHmm) SET _Date=%DATE% & REM Retrieve system's date SET _Time=%TIME: =0% & REM Retrieve system's time ensuring leading zero IF "%_Date%A" LSS "A" (SET _NumTok=1-3) Else (SET _NumTok=2-4) :: Default Delimiter of TAB and Space are used FOR /F "TOKENS=2*" %%A IN ('REG QUERY "HKCU\Control Panel\International" /v iDate') DO SET _iDate=%%B FOR /F "TOKENS=2*" %%A IN ('REG QUERY "HKCU\Control Panel\International" /v sDate') DO SET _sDate=%%B IF %_iDate%==0 FOR /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B IN ("%_Date%") DO SET _fdate=%%D%%B%%C IF %_iDate%==1 FOR /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B IN ("%_Date%") DO SET _fdate=%%D%%C%%B IF %_iDate%==2 FOR /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B IN ("%_Date%") DO SET _fdate=%%B%%C%%D SET BK-DATETIME=%_fdate%-%_Time:~0,2%%_Time:~3,2% :: Compose the full archive name (SET BK-ARCHIVENAME=%BK-ARCHIVEPREFIX%-%BK-TYPE%-%BK-DATETIME%) :: Delete any previous file selection list IF EXIST "%BK-WORKDIR%\7Backup-filelist.txt" ERASE /Q "%BK-WORKDIR%\7Backup-filelist.txt" > NUL IF EXIST "%BK-LOG%" ERASE "%BK-LOG%" /Q > NUL :: Open new log ECHO ------------------------------------------------------------------------------ >> "%BK-LOG%" ECHO 7backup.bat ver. %version% by Anlan (http://www.anlan.com) >> "%BK-LOG%" ECHO Log File >> "%BK-LOG%" ECHO ------------------------------------------------------------------------------ >> "%BK-LOG%" ECHO Command : %* >> "%BK-LOG%" ECHO Time Stamp : %BK-DATETIME% >> "%BK-LOG%" ECHO Type : %BK-TYPE% >> "%BK-LOG%" ECHO Archive : %BK-ARCHIVENAME% >> "%BK-LOG%" ECHO Work Dir : %BK-WORKDIR% (%BK-WORKDRIVE%)>> "%BK-LOG%" ECHO Dest Dir : %BK-DESTDIR% >> "%BK-LOG%" ECHO Selection : %BK-DIRLIST% >> "%BK-LOG%" ECHO Rotation : %BK-ROTATE% >> "%BK-LOG%" ECHO ------------------------------------------------------------------------------ >> "%BK-LOG%" ECHO Selection Directories : >> "%BK-LOG%" TYPE "%BK-DIRLIST%" >> "%BK-LOG%" ECHO . >> "%BK-LOG%" ECHO ------------------------------------------------------------------------------ >> "%BK-LOG%" ECHO Creating root junction point : >> "%BK-LOG%" ECHO %BK-WORKDIR%\%BK-ARCHIVENAME% >> "%BK-LOG%" ECHO ------------------------------------------------------------------------------ >> "%BK-LOG%" :: Create the root that will contain junctions to any of the backup :: directories listed in the backup selection IF EXIST "%BK-WORKDIR%\%BK-ARCHIVENAME%" ( RMDIR /S /Q "%BK-WORKDIR%\%BK-ARCHIVENAME%" > NUL CALL :sleep 2 ) MKDIR "%BK-WORKDIR%\%BK-ARCHIVENAME%" > NUL IF NOT EXIST "%BK-WORKDIR%\%BK-ARCHIVENAME%" ( ECHO "Unable to create directory %BK-WORKDIR%\%BK-ARCHIVENAME%" >> %BK-LOG% CALL :Trace ERROR %BK-EVENTID% "Failed to create directory %BK-WORKDIR%\%BK-ARCHIVENAME% " && GOTO EOF ) :: Read the contents of selection file and create a junction for each one TITLE 7backup - Creating Junctions ... FOR /f "tokens=1-2 delims=:" %%a in ('TYPE "%BK-DIRLIST%"') do ( IF NOT "%%a"=="" IF NOT EXIST "%BK-WORKDIR%\%BK-ARCHIVENAME%\%%a" MKDIR "%BK-WORKDIR%\%BK-ARCHIVENAME%\%%a" IF NOT "%%b"=="" ( MKDIR "%BK-WORKDIR%\%BK-ARCHIVENAME%\%%a%%b" > NUL "%BK-JUNCTION%" %BK-WORKDIR%\%BK-ARCHIVENAME%\%%a%%b %%a:%%b > NUL ) ) :: Upon the choice of FULL/INCR/DIFF I have to set the selection pattern :: In each case we'll skip directories. :: For full backup we select files regardless their Archive attribute :: For diff/incr backup we select files with Archive attribute set IF /I "%BK-TYPE%"=="copy" SET BK-SEL-ATTRS=/A:-D IF /I "%BK-TYPE%"=="full" SET BK-SEL-ATTRS=/A:-D IF /I "%BK-TYPE%"=="incr" SET BK-SEL-ATTRS=/A:A-D IF /I "%BK-TYPE%"=="diff" SET BK-SEL-ATTRS=/A:A-D :: Prepare substitution path and move to it SUBST %BK-WORKDRIVE%: /D > NUL SUBST %BK-WORKDRIVE%: "%BK-WORKDIR%\%BK-ARCHIVENAME%" > NUL CD /D %BK-WORKDRIVE%:\ :: Create a list of files to be backed up TITLE 7backup - Selecting files ... FOR /f "tokens=* delims=" %%a in ('DIR /B /S %BK-SEL-ATTRS%') do ( CALL :strStrip4 "%%a" >> "%BK-WORKDIR%\7Backup-filelist.txt" || ECHO Could not select file "%%a" >> %BK-LOG% ) :: Paste Selection details in log file ECHO Selected files : >> "%BK-LOG%" TYPE "%BK-WORKDIR%\7Backup-filelist.txt" >> "%BK-LOG%" ECHO. ECHO ------------------------------------------------------------------------------ >> "%BK-LOG%" :ArchiveCompression :: Here is the real magic. Do the compression archiving !!! TITLE 7backup - Archiving ... IF NOT DEFINED BK-ARCHIVEPASSWORD ( "%BK-7ZBIN%" a %BK-7ZBINSW% -t%BK-ARCHIVETYPE% "%BK-DESTDIR%\%BK-ARCHIVENAME%.%BK-ARCHIVETYPE%" @"%BK-WORKDIR%\7Backup-filelist.txt" >> "%BK-LOG%" ) ELSE ( "%BK-7ZBIN%" a %BK-7ZBINSW% -t%BK-ARCHIVETYPE% -p%BK-ARCHIVEPASSWORD% "%BK-DESTDIR%\%BK-ARCHIVENAME%.%BK-ARCHIVETYPE%" @"%BK-WORKDIR%\7Backup-filelist.txt" >> "%BK-LOG%" ) :: Check error level IF %ERRORLEVEL% LEQ 1 GOTO :PostArchiving :: We do not execute post operations if something went wrong ECHO Something went wrong during archiving and compression. >> %BK_LOG% CALL :Trace ERROR %BK-EVENTID% "Something went wrong during the compression. Examine %BK-LOG%" GOTO RemoveJunctions :PostArchiving :: Sleep a couple of seconds to let disk caches asset CALL :sleep 2 :: After succesful operation reset archive bit on files TITLE 7backup - Clearing Archive bit on archived files ... IF /I "%BK-TYPE%"=="full" ( ECHO Resetting Archive bit on processed files >> "%BK-LOG%" FOR /f "tokens=* delims=" %%a in ('TYPE "%BK-WORKDIR%\7Backup-filelist.txt"') do ( ATTRIB -A %%a > NUL ) ) IF /I "%BK-TYPE%"=="incr" ( ECHO Resetting Archive bit on processed files >> "%BK-LOG%" FOR /f "tokens=* delims=" %%a in ('TYPE "%BK-WORKDIR%\7Backup-filelist.txt"') do ( ATTRIB -A %%a > NUL ) ) :DoRotation :: Perform rotation of archives upon user's selection IF NOT DEFINED BK-ROTATE ( ECHO No rotation defined. Leaving all archives in place >> %BK-LOG% GOTO RemoveJunctions ) :: List all archives of same type by their creation date descending and :: keep only the nth newest ECHO Rotating archives ... >> %BK-LOG% FOR /f "tokens=* delims=" %%a in ('DIR /B "%BK-DESTDIR%\%BK-ARCHIVEPREFIX%-%BK-TYPE%*.*" /O:-D /A:-D-R /T:C') do ( CALL :SubRotate "%BK-DESTDIR%\%%a" %%BK-ROTATE%% BK-ROTATE >> %BK-LOG% ) :RemoveJunctions :: Now get back to starting directory CD /D "%BK-STARTDIR%" :: Remove Substitution SUBST %BK-WORKDRIVE%: /D :: Remove Root Junction directory and sub-links TITLE 7backup - Removing Junctions ... RMDIR /S /Q "%BK-WORKDIR%\%BK-ARCHIVENAME%" > NUL :: Add to archive file the directory selection list and the log file itself IF NOT DEFINED BK-ARCHIVEPASSWORD ( "%BK-7ZBIN%" u %BK-7ZBINSW% -t%BK-ARCHIVETYPE% "%BK-DESTDIR%\%BK-ARCHIVENAME%.%BK-ARCHIVETYPE%" "%BK-DIRLIST%" "%BK-LOG%" > NUL ) ELSE ( "%BK-7ZBIN%" u %BK-7ZBINSW% -t%BK-ARCHIVETYPE% -p%BK-ARCHIVEPASSWORD% "%BK-DESTDIR%\%BK-ARCHIVENAME%.%BK-ARCHIVETYPE%" "%BK-DIRLIST%" "%BK-LOG%" > NUL ) :Complete :: Log the end of application TITLE 7backup - Complete ! IF "%BK-EVENTLOG%"=="1" CALL :LogEvent INFORMATION %BK-EVENTID% "Application completed at %DATE% %TIME%: %0 %*" :: -------------------------------------------------------------------- :: THIS IS WERE THE STUFF ENDS :: -------------------------------------------------------------------- GOTO EOF :: -------------------------------------------------------------------- :: Start Functions Library :: -------------------------------------------------------------------- :Init :: Parses the command line arguments an tries to set proper values :: at least one pair have to be passed IF "%1"=="" EXIT /b 1 :Init_001 (SET CMD-ARG=%1) (SET CMD-VAL=%2) IF NOT DEFINED CMD-ARG IF NOT DEFINED CMD-VAL GOTO Init_EOF CALL :toLower CMD-ARG IF NOT DEFINED CMD-VAL ( ECHO. ECHO Missing value for argument %CMD-ARG% EXIT /b 1 ) :: Look named pairs IF "%CMD-ARG%"=="--type" (SET BK-TYPE=%CMD-VAL:"=%) && GOTO Init_002 IF "%CMD-ARG%"=="--workdir" (SET BK-WORKDIR=%CMD-VAL:"=%) && GOTO Init_002 IF "%CMD-ARG%"=="--workdrive" (SET BK-WORKDRIVE=%CMD-VAL:"=%) && GOTO Init_002 IF "%CMD-ARG%"=="--dirlist" (SET BK-DIRLIST=%CMD-VAL:"=%) && GOTO Init_002 IF "%CMD-ARG%"=="--destdir" (SET BK-DESTDIR=%CMD-VAL:"=%) && GOTO Init_002 IF "%CMD-ARG%"=="--logfile" (SET BK-LOG=%CMD-VAL:"=%) && GOTO Init_002 IF "%CMD-ARG%"=="--prefix" (SET BK-ARCHIVEPREFIX=%CMD-VAL:"=%) && GOTO Init_002 IF "%CMD-ARG%"=="--archivetype" (SET BK-ARCHIVETYPE=%CMD-VAL:"=%) && GOTO Init_002 IF "%CMD-ARG%"=="--password" (SET BK-ARCHIVEPASSWORD=%CMD-VAL:"=%) && GOTO Init_002 IF "%CMD-ARG%"=="--rotate" (SET BK-ROTATE=%CMD-VAL:"=%) && GOTO Init_002 IF "%CMD-ARG%"=="--logevent" ( CALL :toLower CMD-VAL IF "%CMD-VAL%"=="1" SET BK-EVENTLOG=1 && GOTO Init_002 IF "%CMD-VAL%"=="0" SET BK-EVENTLOG=0 && GOTO Init_002 IF "%CMD-VAL%"=="true" SET BK-EVENTLOG=1 && GOTO Init_002 IF "%CMD-VAL%"=="false" SET BK-EVENTLOG=0 && GOTO Init_002 IF "%CMD-VAL%"=="yes" SET BK-EVENTLOG=1 && GOTO Init_002 IF "%CMD-VAL%"=="no" SET BK-EVENTLOG=0 && GOTO Init_002 ECHO. ECHO Unknown value [%CMD-VAL%] for argument %CMD-ARG% EXIT /b 1 ) :: If none of the above then the named parameter is invalid ECHO Unknown argument %CMD-ARG% && EXIT /b 1 :Init_002 SHIFT SHIFT GOTO Init_001 :Init_EOF SET CMD-ARG= SET CMD-VAL= EXIT /b 0 :Trace :: -- type [in] - Type of event to create [SUCCESS | ERROR | WARNING | INFORMATION] :: -- id [in] - Id of event :: -- des [in] - Description of the event ECHO %3 IF "%BK-EVENTLOG%"=="1" CALL :LogEvent %* EXIT /B 0 :LogEvent :: -- type [in] - Type of event to create [SUCCESS | ERROR | WARNING | INFORMATION] :: -- id [in] - Id of event :: -- des [in] - Description of the event EVENTCREATE /T %1 /ID %2 /L APPLICATION /SO "7Backup" /D %3 > NUL EXIT /B 0 :showUsage :: Displays the proper usage of the script ECHO. ECHO ----------------------------------------------------------------------- ECHO 7backup.bat ver. %version% by Anlan (http://www.anlan.com) ECHO Backup your files with 7zip ECHO ----------------------------------------------------------------------- ECHO Usage : 7backup.bat --dirlist ^ ECHO --type ^ ECHO --destdir ^ ECHO --rotate ^ ECHO [--archivetype ^< 7z ^| gzip ^| zip ^| tar ^>] ECHO [--password ^] ECHO [--prefix ^] ECHO [--workdir ^] ECHO [--logevent 0^|1^|true^|false] ECHO [--logfile ^] ECHO ----------------------------------------------------------------------- ECHO --dirlist File name containing the list of directories to back up ECHO. ECHO --type Type of backup to perform: ECHO FULL : All files and archive bit reset after archiving ECHO DIFF : Files with archive bit set. ECHO Archive bit reset after backup. ECHO INCR : Files with archive bit set. ECHO Archive bit left unchanged after backup. ECHO. ECHO --destdir Directory which will contain the archive. ECHO If it does not exist it will be created. ECHO Ensure it will be on a drive with sufficient space to ECHO contain all the data. Can be a UNC path. ECHO. ECHO --archivetype The type of archive you want to create. ECHO. ECHO --rotate This value must be a number and indicates the number of ECHO archives that should remain on disk after successful ECHO archiving. For example if you set --rotation 3 on a ECHO full archiving operation it means that the newest 3 full ECHO archives will be kept on disk while the oldest (if any) ECHO will be deleted. If this value is NOT set then ALL the ECHO generated archives will be kept on disk. Please be advised ECHO that in such case your target media will be likely get ECHO out of space soon. ECHO. ECHO --password Use this switch if you want to password protect your ECHO archive. No spaces in passwords please. ECHO. ECHO --prefix The prefix to use to generate the archive name. ECHO. ECHO --workdir Directory which will be used as working area. ECHO MUST have directory attributes and must be on an NTFS file ECHO system. MUST be on a local drive. If none is given then ECHO the procedure will try to use the one in environment's ECHO TEMP variable. ECHO. ECHO --workdrive Drive letter to use as substitution for workdir. ECHO Necessary to prevent the occurrence of excessive length paths ECHO. ECHO --logevent Whether or not to log relevant events in application's ECHO log. ECHO. ECHO --logfile Where to log backup operations ECHO ----------------------------------------------------------------------- EXIT /b 0 :toLower str -- converts uppercase character to lowercase :: -- str [in,out] - valref of string variable to be converted :$created 20060101 :$changed 20080219 :$categories StringManipulation :$source http://www.dostips.com if not defined %~1 EXIT /b for %%a in ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z" "Ä=ä" "Ö=ö" "Ü=ü") do ( call set %~1=%%%~1:%%~a%% ) EXIT /b 0 :SubRotate SET /a y=%2-1 IF %y% LSS 0 ( ECHO Deleting %1 ERASE /F /Q %1 > NUL ) IF "%~3" NEQ "" (set %~3=%y%) EXIT /b 0 :strStrip4 SETLOCAL ENABLEEXTENSIONS SETLOCAL DISABLEDELAYEDEXPANSION set "line=%1" echo "%line:~4% ENDLOCAL EXIT /b 0 :sleep seconds -- waits some seconds before returning :: -- seconds [in] - number of seconds to wait :$created 20060101 :$changed 20080219 :$source http://www.dostips.com FOR /l %%a in (%~1,-1,1) do (ping -n 2 -w 1 127.0.0.1 > NUL) EXIT /b 0 :: -------------------------------------------------------------------- :EOF ENDLOCAL TITLE