Skip to content

Instantly share code, notes, and snippets.

@jazzl0ver
Created March 25, 2020 10:48
Show Gist options
  • Save jazzl0ver/0b8795c47de983b9f62fa429f771492b to your computer and use it in GitHub Desktop.
Save jazzl0ver/0b8795c47de983b9f62fa429f771492b to your computer and use it in GitHub Desktop.
Backup Windows SystemState along with custom folders to a shared storage
@echo off
rem -- put this script into the task scheduler and redirect output to a log file:
rem -- c:\utils\ad_backup.bat > c:\utils\ad_backup.log 2>&1
rem -- path to remote storage
set SHARE=\\filer-1\sysstates
rem -- what to backup
set PATH2BACKUP=c:\utils C:\Windows\System32\dns\backup
rem -- comment this out if no system state backup needed
rem -- otherwise, specify the separate drive letter (not a system drive)
rem set SYSSTATEDRIVE=D:
rem -- number of days to store previous backups
set KEEPDAYS=10
rem -- do not modify below this line ------------------------------------------
for /f "delims=" %%# in ('powershell get-date -format "{yyyyMMdd}"') do set fdate=%%#
set ARCH=backup_%fdate%_%COMPUTERNAME%.zip
set PASS=""
echo %date%%time%
if not exist O:\ (goto mapit) else (goto runit)
:mapit
rem net use O: /delete
net use O: %SHARE%
:runit
if not exist O:\ goto end
if exist %SYSSTATEDRIVE% goto sysstate
:zip
@forfiles /P O:\ /M backup_*.zip /D -%KEEPDAYS% /C "cmd /c del @file" 2>&1
if %PASS% == "" ("C:\Program Files\7-Zip\7z.exe" a -tzip -mx=1 -w%TEMP% O:\%ARCH% %PATH2BACKUP%) else ("C:\Program Files\7-Zip\7z.exe" a -tzip -mx=1 -w%TEMP% -p%PASS% O:\%ARCH% %PATH2BACKUP%)
:end
net use O: /delete
echo %date%%time%
@echo on
@exit
:sysstate
rmdir /q /s %SYSSTATEDRIVE%\WindowsImageBackup
wbadmin start systemstatebackup -backuptarget:%SYSSTATEDRIVE% -quiet
goto zip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment