使用NTDSUTIL删除超过30天的AD快照

时间:2014-02-07 13:03:39

标签: batch-file active-directory windows-server-2008 snapshot

我的域控制器正在运行Windows Server 2008。

我已经能够使用以下批处理命令与任务计划程序一起编写AD快照创建脚本。

@echo off
ntdsutil snapshot "activate instance ntds" create quit quit
exit

现在我想使用批处理文件删除超过30天的AD快照。在ntdsutil中有一个删除命令但是我在将删除操作放入for循环时遇到了麻烦。有没有办法获得“删除”引用的索引变量?

我想:

  1. 打开ntdsutil并输入快照上下文
  2. 在%s大于60的循环中运行删除%s
  3. 退出循环和ntdsutil
  4. 以下是ntds snapshot命令行界面的屏幕截图:

    ntdsutil: snapshot
    snapshot: ?
    
     ?                             - Show this help information
     Activate Instance %s          - Set "NTDS" or a specific AD LDS instance
                                     as the active instance.
     Create                        - Create a snapshot
     Delete %s                     - Delete snapshot with index or guid %s. Specify
     * to delete all snapshots
     Help                          - Show this help information
     List All                      - List snapshots
     List Mounted                  - List mounted snapshots
     Mount %s                      - Mount snapshot with index or guid %s
     Quit                          - Return to the prior menu
     Unmount %s                    - Unmount snapshot with index or guid %s. Specify
     * to unmount all mounted snapshots
    
    snapshot: list all
     1: 2014/02/06:15:58 {052e85ae-7379-4164-8b7c-1be14fea1754}
     2:   C: {bdfe5953-3d4f-4195-b94b-593f66c2bc1e}
    
     3: 2014/02/07:07:12 {82daa70b-ac7e-438c-80f5-78de8a4500f7}
     4:   C: {0a05a0ab-4faa-442f-a65a-88579c037e1a}
    
     5: 2014/02/07:07:27 {e761dded-b13c-4fac-b050-5b9d042cec73}
     6:   C: {2af92c27-765c-43e8-a384-1bf41b0634b9}
    
    snapshot: delete 5
    Snapshot {2af92c27-765c-43e8-a384-1bf41b0634b9} deleted.
    snapshot: list all
     1: 2014/02/06:15:58 {052e85ae-7379-4164-8b7c-1be14fea1754}
     2:   C: {bdfe5953-3d4f-4195-b94b-593f66c2bc1e}
    
     3: 2014/02/07:07:12 {82daa70b-ac7e-438c-80f5-78de8a4500f7}
     4:   C: {0a05a0ab-4faa-442f-a65a-88579c037e1a}
    

2 个答案:

答案 0 :(得分:0)

这是我制作的批次代码。他基于文本文件来管理快照的保留。

享受。

@echo off

REM +-------------------------------------------------------------------------------------------------------------------+
REM ¦   Autor       : Ficzko                                                                                            ¦
REM ¦   Version     : 1.0                                                                                               ¦
REM ¦   Date        : 2014/10/22                                                                                        ¦
REM ¦   Comments    : Batch File for manage retention of the Snapshots into NTDSUTIL.                                   ¦
REM ¦                 Each Snapshot create a txt file. The txt file containt the id of the backup.                      ¦ 
REM +-------------------------------------------------------------------------------------------------------------------+

REM     Logs Location (Used for manage retention). The folder have to contain only the text files of this batch.
set mypath=C:\ADSnapLogs\
if not exist %mypath% mkdir %mypath%

REM     Setup of the retention of snapshots in seconds  (1 Day = 86400, 31 Days = 2678400, ...)
set myretention=2678400

REM     Generation of a Linux TimeStamp for the naming of log files 
REM     (Time in seconds since 1970-01-01 00:00:00  ex : 205329600 for Sun Jul 4 12:00:00 1976 GMT)
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do (set %%x)
set /a z=(14-100%Month%%%100)/12, y=10000%Year%%%10000-z
set /a mydate=y*365+y/4-y/100+y/400+(153*(100%Month%%%100+12*z-3)+2)/5+Day-719469
set /a mydate=mydate*86400+100%Hour%%%100*3600+100%Minute%%%100*60+100%Second%%%100

REM     Creation of the snapshot
echo Snapshot in progress
ntdsutil snapshot "activate instance ntds" create quit quit > %mypath%%mydate%.txt

REM     Get the ID of the Snapshot and save it into the log file
for /f "tokens=1,2,3,4,5 delims= " %%a in ('findstr /i /c:"{" "%mypath%%mydate%.txt"') do @echo %%c > %mypath%%mydate%.txt

REM     Removing old snapshot
echo Removing old snapshot
for /f "tokens=1,2 delims=." %%a in ('dir %mypath% /b') do call :CalcRetension %%a
goto end
:CalcRetension
set val=%1
set /a result=%mydate%-%myretention%
IF %val% LSS %result% (
    for /f "tokens=1,* delims= " %%a in ('findstr /i /c:"{" "%mypath%%val%.txt"') do call :removeSnapshot %%a %val%
)
goto end
:removeSnapshot
set guid=%1
set filename=%2
ntdsutil snapshot "delete %guid%" quit quit
del /S "%mypath%%filename%.txt"
goto end
:End

答案 1 :(得分:0)

我使用相同的脚本执行每日快照,并且我一直有5个最新快照。我没有打扰他们清理它们。