Contribute  :  Advanced Search  :  Directory  :  Forum  :  FAQ's  :  My Downloads  :  Links  :  Polls  
AFP548 Changing the world one server at a time.
Welcome to AFP548
Thursday, July 29 2010 @ 09:34 am MDT
   

Automated Archiving of OD and Exploring serveradmin

ArticlesYou can archive OD from Server Admin, but how do you do this from a script?

I asked myself the same question. It's not that pretty, but it's not too hard to get working. This is also a decent walk through how to bend Server Admin to your will!

The first line of thought I had on this was to use the slapconfig -backupdb command. This works great, but it's interactive. So I'd either have to learn expect or move on to something else. By far though slapconfig -backupdb is the easiest way to do this interactively from the CLI, so if you need an immediate backup, and your on the box, use it.

So putting on my thinking toque and with an assist from Mr. Bartosh I figured this one out.

Before getting too far into this, you have to know how Server Admin is even telling the serveradmin facilities on the remote server how to do this. Usually this would involve a lot of mucking about in the servermgrd files and using strings to find out what commands the cgi's support.

Bah!

With a tiny bit of guess work you can actually open up a debug menu in Server Admin. This will give you a log of all the info that's being sent to the server and what's coming back.

Woot!

To enable this just run a quick defaults command to put the right key into the Server Admin plist.

defaults write com.apple.serveradmin UseDebugMenu YES

Then run Server Admin again and actually issue the archive command. You'll see in the transaction window a big blob of xml with lots of commands and stuff in it.


[2005-06-22 16:28:31 -0400] #2 https://odm:311/
commands/servermgr_dirserv
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST
 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>backupArchiveParams</key>
	<dict>
		<key>archivePassword</key>
		<string>test</string>
		<key>archivePath</key>
		<string>/usr/local/backup/archive1</string>
	</dict>
	<key>command</key>
	<string>backupArchive</string>
</dict>
</plist>
Ick! What are you going to do with this?

If you've ever used serveradmin you probably don't have a clue how to enter this into the command. Server Admin talks in XML whereas serveradmin doesn't. What's an admin to do?

Read the man page for serveradmin and you get a glimpse that you can actually send serveradmin interactive commands to do more than one thing at a time.

serveradmin command

will give you a subshell that you can tap out your commands. Read the manpage on the syntax but you'll see it ends up being something like this:

dirserv:backupArchiveParams:archivePassword = test
dirserv:backupArchiveParams:archivePath = /usr/local/backup/archive1
dirserv:command = backupArchive

Will give you the same output, that is if you remember to finish it with control-D.

So now that you are this close, let me mention that you can actually feed serveradmin those commands from a text file by doing this:

serveradmin command < yourfile

And you're off to the races. So to make this into a real system you'll need a shell script that gets the current date, or some other incremental variable, and builds the archive path with that. Then writes the three commands out to a text file and calls serveradmin to read in that file and do the dirty work. Run that script from cron or launchd and you're home from the office early on Fridays.

I'll post a sample script up here in a bit. Until then consider this a homework exercise in scripting. Feel free to post what works for you in the comments.

Story Options

Advertising

Automated Archiving of OD and Exploring serveradmin | 18 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.
Automated Archiving of OD and Exploring serveradmin
Authored by: Anonymous on Wednesday, June 22 2005 @ 07:53 pm MDT
Nice trick (especially for the debug menu) but, when I tried this archiving
functionnality, the first thing I checked was the slapconfig.log as I
suspected this magic and powerful command was involved, and that was it.
Check the manpage and see :

slapconfig -backupdb path/to/backup

And also -restoredb and even a -mergedb with a -f.

I appreciate serveradmin but in this case I prefer slapconfig, it seems
designed for the job as it seems to be used in the background...
Automated Archiving of OD--sample script
Authored by: unixgeek42 on Thursday, June 23 2005 @ 10:09 am MDT
A small note of correction in the feeding the commands from the file. The direction symbol is reversed (the arrow should point in the direction of the commands).

Using the info from the article, I wrote a backup script that should do the trick. It looks like it does, but it also gives me an error at the end: dirserv:error = "backupArchive error". Not sure exactly what's wrong though. Here's the script:

#!/bin/sh
LOCATION=/usr/local/backup/archive-`date "+%Y%m%d"`
echo "dirserv:backupArchiveParams:archivePassword = test" > sacommands.txt
echo "dirserv:backupArchiveParams:archivePath = $LOCATION" >> sacommands.txt
echo "dirserv:command = backupArchive" >> sacommands.txt
serveradmin command < sacommands.txt

Be sure your sacommands.txt file is in a secure location as it contains the password to the archive of all your users' passwords!

Automated Archiving of OD and Exploring serveradmin
Authored by: Anonymous on Thursday, June 23 2005 @ 03:16 pm MDT
What versions of MacOS X Server does this apply to? 10.1?10.2?10.3?10.4?

Secondly, I found that NetInfo by default backs up its files in /var/backups/
Possibly a good place for the scripts output to place the results.
Automated Archiving of OD and Exploring serveradmin
Authored by: Anonymous on Thursday, November 10 2005 @ 09:08 am MST
Am I the only one experiencing the error:

"You can only target one service at a time with the command option"

???

I am using Server 10.4.3 currently.
Automated Archiving of OD and Exploring serveradmin
Authored by: kainewynd2 on Thursday, November 10 2005 @ 01:39 pm MST
Well not the most secure thing in the world, I wrote an applescript to do this with a network backup feature. This uses the schell script posted above to create the save name and perform the actual commands:

[code]
do shell script "sudo /backup.sh" password "password1" with administrator privileges
delay 200
tell application "Finder"
mount volume "afp://your.server.com/Backup" on server "mac" as user name "user" with password "password2"
end
do shell script "sudo ditto /Backup/Volumes/Backup/" password "password1" with administrator privileges
delay 200
tell application "Finder"
eject "Backup"
end tell
[/code]

Why applescript? you might ask. I'm not a huge fan of cron (due to a disinterest in working in it, I'll admit) so I like to use iCal recurring appointments to schedule things. If anyone out there is like me - here you go!
Automated Archiving of OD with Perl
Authored by: stdin on Thursday, December 01 2005 @ 12:54 pm MST
I posted this script a while ago (as a comment to someone elses comment), but I've made a few improvements and thought I'd pass them along. This script will backup OD data using the techniques discussed in this forum thread using Perl. The additions I've made also automatically clean up (delete) old archives.
#!/usr/bin/perl -w

use strict;

my $archive_password = 'password';
my $archive_path = '/private/odarchives/';

my $max_keep_time = 1; # MONTHS TO KEEP ARCHIVES AROUND

my @date = localtime();
my $year = $date[5] + 1900;
my $month = sprintf("%.2d",$date[4] + 1);
my $day = sprintf("%.2d",$date[3]);

my $filename = $year.$month.$day;
my $archive_file = $archive_path.$filename;

print "Archiving to $archive_file...\n";
 
if (open(CMD,"|/usr/sbin/serveradmin command")) {
  print CMD "dirserv:backupArchiveParams:archivePassword = $archive_password\n"
  print CMD "dirserv:backupArchiveParams:archivePath = $archive_file\n";
  print CMD "dirserv:command = backupArchive\n";
  close(CMD);
  print "Archive successful.\n";
} else {
  print "Error: $!\n";
  exit;
}

$month -= $max_keep_time;
if ($month < 1) {
  $month = 12;   
  $year--;
}

my $expire_date = sprintf("%.4d%.2d%.2d",$year,$month,$day);

print "Cleaning up old archives...\n";

if (opendir(DIR,$archive_path)) {
  while (my $file = readdir(DIR)) {
    chomp($file);
    next unless ($file =~ /^\d{8}\.sparseimage$/i);
    my $file_date = $file;
    $file_date =~ s/[^0-9]//g;
    if ($file_date < $expire_date) {
      print "Removing ".$archive_path.$file."\n";
      unlink($archive_path.$file);
    }
  }
  print "Cleanup successful.\n";
} else {
  print "Error: $!\n";
}

exit;
Automated Archiving of OD and Exploring serveradmin
Authored by: Anonymous on Wednesday, June 21 2006 @ 03:54 pm MDT
Expect isn't so bad:
# cat odcmd.txt

spawn slapconfig -backupdb archive
expect "Enter archive password"
send "password\r"
expect eof

Then execute this:
expect -f odcmd.txt