@ECHO OFF REM Zip and backup files with 7-zip REM Author: Katevaoglou Stylianos, Center of Informatics and New Technologies of Samos (KEPLHNET Samou) REM License: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International REM Attribution link: http://dide.sam.sch.gr/keplinet REM Enter the correct values at the 5 following lines that start with SET command REM Don't use a trailing backslash (\) in folder names REM Don't use opening and closing quotation marks (") REM You can use folder names with spaces REM Don't use an extension (e.g. zip) in the backupFileName variable REM Be careful of leading and trailing spaces. They are considered part of the path, name etc. REM At least one old backup will be kept. Values bellow 1 are meaningless in the numberOfOldBackups variable REM Exit Codes: REM 0 = Completed Successfully REM 1 = Backup operation aborted by user before doing anything REM 2 = 7z.exe has not been found REM 3 = Source folder was not found REM 4 = Destination folder was not found REM 5 = Some error occurred during deleting old backup files REM 6 = Some error occurred during renaming old backup files REM 7 = Some error occurred during copying old backup files REM 8 = Some error occurred during backing up with 7-Zip REM Initializing local environment SETLOCAL EnableDelayedExpansion SETLOCAL EnableExtensions REM Variables (edit as needed) SET zipFile=C:\Program Files\7-Zip\7z.exe SET sourceFolder=C:\My Data SET destFolder=H:\My Backup SET numberOfOldBackups=4 SET backupFileName=Backup REM Print info ECHO ============================================================================== ECHO Zip and backup files with 7-zip ECHO ============================================================================== ECHO Your data will be copied in zip file format in a file named %backupFileName%.zip ECHO A custom 7-zip update method will be used to update the %backupFileName%.zip file ECHO Note that concerning files already existing in the destination folder: ECHO -Existing %backupFileName%.zip will be renamed to %backupFileName%_OLD_1.zip IF %numberOfOldBackups% GEQ 2 ( ECHO -Existing %backupFileName%_OLD_1.zip will be renamed to %backupFileName%_OLD_2.zip etc. ECHO -Exisitng %backupFileName%_OLD_%numberOfOldBackups%.zip will be deleted without any further confirmation ) ECHO ============================================================================== ECHO 7z.exe file position = %zipFile% ECHO Source folder = %sourceFolder% ECHO Destination folder = %destFolder% ECHO ============================================================================== REM Assume everything will go OK SET backupResultCode=0 SET backupResultText=Backup completed successfully REM If you do NOT want a confirmation message to appear comment the six following lines (just add REM command in front of each line) SET /P continue=If you are sure enter Y or y... IF "%continue:~0,1%" NEQ "y" IF "%continue:~0,1%" NEQ "Y" ( SET backupResultCode=1 SET backupResultText=ABBORTED: Backup operation aborted by user before doing anything GOTO exit ) IF NOT EXIST "%zipFile%" ( SET backupResultCode=2 SET backupResultText=ERROR: 7z.exe has not been found GOTO exit ) IF NOT EXIST "%sourceFolder%" ( SET backupResultCode=3 SET backupResultText=ERROR: Source folder was not found GOTO exit ) IF NOT EXIST "%destFolder%" ( SET backupResultCode=4 SET backupResultText=ERROR: Destination folder was not found GOTO exit ) ECHO ============================================================================== ECHO Processing... ECHO ============================================================================== ECHO ============================================================================== ECHO Deleting the oldest backup and renaming the rest (if any)... ECHO ============================================================================== REM %%n is counting from %numberOfOldBackups% DOWNTO 2 FOR /L %%n IN (%numberOfOldBackups%,-1,2) DO ( REM !next! is one less that %%n REM Use !next! for delayed expansion to work SET /A next=%%n-1 IF EXIST "%destFolder%\%backupFileName%_OLD_!next!.zip" ( IF EXIST "%destFolder%\%backupFileName%_OLD_%%n.zip" ( ECHO Deleting %destFolder%\%backupFileName%_OLD_%%n.zip DEL /F /Q "%destFolder%\%backupFileName%_OLD_%%n.zip" ) IF EXIST "%destFolder%\%backupFileName%_OLD_%%n.zip" ( SET backupResultText=ERROR: Some error occurred during deleting old backup files. Check error messages above SET backupResultCode=5 GOTO exit ) ECHO Renaming %destFolder%\%backupFileName%_OLD_!next!.zip to %destFolder%\%backupFileName%_OLD_%%n.zip RENAME "%destFolder%\%backupFileName%_OLD_!next!.zip" %backupFileName%_OLD_%%n.zip IF !ERRORLEVEL! NEQ 0 ( rem TYPE temp.txt SET backupResultText=ERROR: Some error occurred during renaming old backup files. Check error messages above SET backupResultCode=6 GOTO exit ) ) ) IF EXIST "%destFolder%\%backupFileName%.zip" ( ECHO Copying %destFolder%\%backupFileName%.zip to %destFolder%\%backupFileName%_OLD_1.zip XCOPY /Q /Y "%destFolder%\%backupFileName%.zip" "%destFolder%\%backupFileName%_OLD_1.zip*" IF %ERRORLEVEL% NEQ 0 ( SET backupResultText=ERROR: Some error occurred during copying old backup files. Check error messages above SET backupResultCode=7 GOTO exit ) ) REM Starting the real zipping and backup process ECHO ============================================================================== ECHO Zipping and backing up... ECHO ============================================================================== REM Explaining parameters of 7z.exe command REM u = Method is update REM -w = Working directory is OS temp folder REM -tzip = Type of archive is zip REM -mx=5 = Compression level set to normal REM -y = Don't ask anything. Assume yes to all REM -up0q0r2x2y2z1w2 = Specify update method. Mirror files to archive. All changes from files (deletions, additions, newer files, even older files) are passed to the archive REM See http://sevenzip.sourceforge.jp/chm/cmdline/index.htm for further info about 7z.exe command line arguments "%zipFile%" u -w -tzip -mx=5 -y -up0q0r2x2y2z1w2 "%destFolder%\%backupFileName%.zip" "%sourceFolder%\*" IF %ERRORLEVEL% NEQ 0 ( SET backupResultText=ERROR: Some error occurred during backing up with 7-Zip. Check error messages above SET backupResultCode=8 GOTO exit ) :exit ECHO ============================================================================== ECHO %backupResultText% ECHO ============================================================================== ECHO End of execution. Goodbye REM Finish using local environment ENDLOCAL ECHO on @EXIT /B %backupResultCode%