Skip to content

Instantly share code, notes, and snippets.

@redneck-f25
Created March 18, 2016 05:17
Show Gist options
  • Save redneck-f25/1f5931875b41fd9d0405 to your computer and use it in GitHub Desktop.
Save redneck-f25/1f5931875b41fd9d0405 to your computer and use it in GitHub Desktop.
Windows cmd-skript to create a volume shadow copy from command line
@echo off
setlocal enableextensions enabledelayedexpansion
set on_error=print_usage
call :main_%~1 %* 2>nul && exit /b || goto !on_error! %*
:print_usage
echo\Usage: %~n0 create ^<drive:^> ^<link^>
echo\ %~n0 remove ^<link^>
echo\
echo\Examples:
echo\ %~n0 create d: c:\snapshots\d_snap
echo\ %~n0 remove c:\snapshots\d_snap
exit /b
:on_error_exit
exit /b %ERRORLEVEL%
:main_create
set on_error=on_error_exit
if "$~2$~3"=="" goto print_usage %*
set "TAB= "
set "SHADOW_COPY_BASE=\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy"
set "shadow_id="
set "shadow_volume="
set "shadow_drive=%~2"
set "link=%~3"
>nul mklink /d "%link%" . && rmdir "%link%" || (
echo/Cannot create symlink.
exit /b 2
)
for /f "usebackq tokens=1,2,* delims=;=" %%a in ( `wmic shadowcopy call create "ClientAccessible"^,"%shadow_drive%\"` ) do (
set "k=%%a"
set "k=!k:%TAB%=!"
set "k=!k: =!"
set "v=%%b"
set "v=!v: =!"
if "!k!"=="ReturnValue" (
if not "!v!"=="0" (
echo\Cannot create snapshot ^(!k! = !v!^).
exit /b 1
)
) else if "!k!"=="ShadowID" (
set "shadow_id=!v:"=!"
break
)
)
if "%shadow_id"=="" (
echo\Cannot create snapshot.
exit /b 1
)
for /f "usebackq tokens=1,* delims=:" %%a in ( `vssadmin list shadows "/shadow=%shadow_id%"` ) do (
set "k=%%a"
set "k=!k:%TAB%=!"
set "k=!k: =!"
set "v=%%b"
set "v=!v: =!"
set "n=!v:%SHADOW_COPY_BASE%=!"
if not "!n!"=="!v!" (
set "shadow_volume=%SHADOW_COPY_BASE%!n!"
break
)
)
if "%shadow_volume%"=="" (
echo\Cannot find snapshot volume.
exit /b 1
)
mklink /d "%link%" "%shadow_volume%\" || (
vssadmin delete shadows "/shadow=%shadow_id%" /quiet
)
exit /b
:main_remove
set on_error=on_error_exit
if "$~2"=="" goto print_usage %*
set "shadow_id="
set "shadow_volume="
set "shadow_drive="
set "link=%~2"
if "%link:~-1%"=="\" set "link=%link:~0,-1%"
set "link_dir=%~dp2"
set "link_name=%~nx2"
if not exist "%link%" (
echo\File does not exist.
exit /b
)
for /f "usebackq tokens=1,2,3,4,*" %%a in ( `dir "%link_dir%"` ) do (
if "%%c:%%d"=="<SYMLINKD>:%link_name%" (
set "shadow_volume=%%e"
rem remove square brackets and trailing backslash
set "shadow_volume=!shadow_volume:~1,-2!"
)
)
if "%shadow_volume%"=="" (
echo\File is not a directory symlink.
exit /b 1
)
for /f "usebackq tokens=1,* delims=:" %%a in ( `vssadmin list shadows` ) do (
set "k=%%a"
set "k=!k:%TAB%=!"
set "k=!k: =!"
set "v=%%b"
set "v=!v: =!"
if "!v!"=="%shadow_volume%" (
set "shadow_id=!q!"
set "shadow_drive=!p:~1,2!"
break
)
set "q=!p!"
set "p=!v!"
)
if "%shadow_id%"=="" (
exit /b 1
)
>nul vssadmin delete shadows "/shadow=%shadow_id%" /quiet
rmdir "%link%"
echo\Done.
exit /b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment