【问题标题】:Deleting AD snapshots older than 30 days with NTDSUTIL使用 NTDSUTIL 删除超过 30 天的 AD 快照
【发布时间】:2014-03-04 21:52:32
【问题描述】:

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

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

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

现在我想使用批处理文件删除超过 30 天的 AD 快照。 ntdsutil 中有一个删除命令,但我无法将删除操作放入 for 循环中。有没有办法访问“删除”引用的索引变量?

我想:

  1. 打开 ntdsutil 并进入快照上下文
  2. 在 %s 大于 60 的循环中运行 delete %s
  3. 退出循环和 ntdsutil

这是 ntds 快照命令行屏幕的截图:

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}

【问题讨论】:

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


    【解决方案1】:

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

    享受吧。

    @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
    

    【讨论】:

      【解决方案2】:

      我正在使用相同的脚本制作每日快照,并且我一直有 5 个最新快照。我懒得清理它们。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-07
        • 1970-01-01
        • 2016-04-18
        • 1970-01-01
        • 2018-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多