Skip to content

Instantly share code, notes, and snippets.

@kudaliar032
Last active October 24, 2022 04:12
Show Gist options
  • Save kudaliar032/057b189f5f79f53eeac23af3e288649e to your computer and use it in GitHub Desktop.
Save kudaliar032/057b189f5f79f53eeac23af3e288649e to your computer and use it in GitHub Desktop.
Windows Server Backup Batch Script
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET AppName=wordpress
SET AppPath=C:\wamp\apache2\htdocs\%AppName%
SET BackupPath=C:\Backup\wordpress
SET InstallLocationOf7Zip=%ProgramFiles%\7-Zip
SET RetentionDays=3
REM Get Date Now
FOR /f "tokens=1,2,3,4 delims=/ " %%a IN ('date /t') DO (
SET FileDate=%%d%%b%%c
)
SET exe7Zip=%InstallLocationOf7Zip%\7z.exe
SET dirTempBackup=%TEMP%\backup\%FileDate%\%AppName%
SET BackupFileName=%FileDate%-%AppName%.zip
SET BackupFileDestination=%BackupPath%\%BackupFileName%
REM Validation.
IF NOT EXIST "%exe7Zip%" (
ECHO 7-Zip is not installed in the location: %InstallLocationOf7Zip%
ECHO Please update the directory where 7-Zip is installed.
GOTO End
)
IF EXIST "%BackupFileDestination%" (
ECHO Backup file exist.
ECHO Please check your backup!
GOTO End
)
REM Validation end.
REM Copy backup application to temp dir
ECHO Starting to copy files.
XCOPY "%AppPath%" "%dirTempBackup%" /v /c /i /g /h /q /r /y /e
ECHO Done copying files.
ECHO.
REM Compress files using 7-Zip in a lower priority process.
ECHO Compressing backed up files. (New window)
START "Compressing Backup. DO NOT CLOSE" /belownormal /wait "%exe7Zip%" a -tzip -r -mx5 "%BackupFileDestination%" "%dirTempBackup%\"
ECHO Done compressing backed up files.
ECHO.
REM All done. Clean up disk.
ECHO Cleaning up.
IF EXIST "%dirTempBackup%" RMDIR /s /q "%dirTempBackup%"
ECHO.
REM Keep retention backup
FORFILES /P "%BackupPath%" /S /D -%RetentionDays% /C "CMD /C DEL @file"
:End
ECHO Exited.
ECHO.
ENDLOCAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment