Wednesday, March 2, 2016

Active Directory Offline Defragmentation on Server 2008 R2



Offline defragmentation create a modified version of the old database (Ntds.dit) file in a new location which we provide. Later we can copy the compacted (offline defragmented) database file to its original location.
Note: Always make sure you have enough space in your local directory were you are trying to compact the AD database. Check the size of Ntds.dit and check the space of drive and make sure the drive is having more space than the Ntds.dit file.
I have followed the below steps for successful Offline Defragmentation.

Steps for offline defragmentation:
1.       Open a Command Prompt as an administrator.

2.       At the command prompt, type the following command to step AD DS Service, and then press ENTER.
net stop ntds

3.       Type yes to stop other service also. It will look like below.

Do you want to continue this operation? (Y/N) [N]: y
The File Replication Service service is stopping...
The File Replication Service service was stopped successfully.

The Kerberos Key Distribution Center service was stopped successfully.

The Intersite Messaging service is stopping.
The Intersite Messaging service was stopped successfully.

The DNS Server service is stopping.
The DNS Server service was stopped successfully.

The Active Directory Domain Services service is stopping.
The Active Directory Domain Services service was stopped successfully.

4.       At the command prompt, type ntdsutil, and then press ENTER.

5.       At the ntdsutil prompt, type activate instance ntds, and then press ENTER.

6.       At the ntdsutil prompt, type files, and then press ENTER.

7.       At the file maintenance prompt, type compact to <drive>:\ <LocalDirectoryPath> (where <drive>:\ <LocalDirectoryPath> is the path to a location on the local computer), and then press ENTER.

8.       I created a local directory in C:\Temp\NtdsCompact. So command should be like Compact to C:\Temp\NtdsCompact. An example is given below:

C:\Users\Administrator.TEST>ntdsutil
ntdsutil: activate instance ntds
Active instance set to "ntds".
ntdsutil: files
file maintenance: compact to C:\Temp\NtdsCompact
Initiating DEFRAGMENTATION mode...
     Source Database: C:\Windows\NTDS\ntds.dit
     Target Database: C:\Temp\NtdsCompact\ntds.dit

                  Defragmentation  Status (% complete)

          0    10   20   30   40   50   60   70   80   90  100
          |----|----|----|----|----|----|----|----|----|----|
          ...................................................

It is recommended that you immediately perform a full backup
of this database. If you restore a backup made before the
defragmentation, the database will be rolled back to the state
it was in at the time of that backup.

Compaction is successful. You need to:
   copy "C:\Temp\NtdsCompact\ntds.dit" "C:\Windows\NTDS\ntds.dit"
and delete the old log files:
   del C:\Windows\NTDS\*.log

9.       If defragmentation completes successfully, type quit and again type quit the file maintenance mode.

10.   If defragmentation fails with errors, go to step 17

11.   Type quit to exit from Ntdsutil prompt.

12.   Now from the command prompt delete the log file as per the instruction above.
C:\Users\Administrator.TEST>del C:\Windows\NTDS\*.log

13.   Copy the compacted database file to the original location as per the instruction above.

C:\Users\Administrator.TEST>copy "C:\Temp\NtdsCompact\ntds.dit" "C:\Windows\NTDS\ntds.dit"
Overwrite C:\Windows\NTDS\ntds.dit? (Yes/No/All): y
        1 file(s) copied.

14.   Now at the command prompt, type ntdsutil, and then press ENTER.

15.   At the ntdsutil prompt type activate instance ntds and press Enter.
Will get the message Active instance set to "ntds".

16.   At the ntdsutil prompt, type files, and then press ENTER.

17.   At the file maintenance prompt, type integrity, and then press ENTER.

18.   Once the integrity check succeeds type quit and press ENTER to quit the file maintenance prompt, and then type quit and press ENTER again to exit from Ntdsutil.exe.

19.   Start the AD DS Service by typing the following command.
net start ntds

C:\Users\Administrator.TEST>net start ntds
The Active Directory Domain Services service is starting.....
The Active Directory Domain Services service was started successfully.

20.   If integrity check fails follow Step 13 to 16 again.

21.   Below is the example of above performed commands (step 13 to 16).

C:\Users\Administrator.TEST>ntdsutil
ntdsutil: activate instance ntds
Active instance set to "ntds".
ntdsutil: files
file maintenance: integrity
Doing Integrity Check for db: C:\Windows\NTDS\ntds.dit.

Checking database integrity.

                     Scanning  Status (% complete)

          0    10   20   30   40   50   60   70   80   90  100
          |----|----|----|----|----|----|----|----|----|----|
          ...................................................


Integrity check successful.

It is recommended you run semantic database analysis
to ensure semantic database consistency as well.
22.   If you need you can perform the semantic database check as well by following Microsoft Technet article (https://technet.microsoft.com/en-us/library/cc816754(v=WS.10).aspx)

23.   Please check if you are receiving the following Events in Directory Service.

Log Name:      Directory Service
Source:        NTDS ISAM
Event ID:      701
Task Category: Online Defragmentation
Level:         Information
Keywords:      Classic
Description: Online defragmentation has completed a full pass on database C:\WINNT\NTDS\ntds.dit, freeing xx pages.

Log Name:      Directory Service
Source:        NTDS ISAM
Event ID:      700
Task Category: Online Defragmentation
Level:         Information
Keywords:      Classic
Description: Online defragmentation is beginning a full pass on   database 'C:\Windows\NTDS\ntds.dit'.

No comments: