iChatServer_backup [plain text]
$ECHO = "/bin/echo";
$MKDIR = "/bin/mkdir";
$MKTEMP_PATH = "/usr/bin/mktemp";
$PBUDDY = "/usr/libexec/PlistBuddy";
$SQLITE = "/usr/bin/sqlite3";
$TAR = "/usr/bin/gnutar";
$SBS_CONF = "/private/etc/server_backup";
$SBSProgressLogFile = "/private/var/log/server_backup/sbs_progress.log";
$tmpPath = "/private/var/jabberd/tmp";
$sqliteDefaultDbPath = "/Library/Server/iChat/Data/sqlite/jabberd2.db";
$sqliteDbPath = "";
$ServiceConf = "75-iChatServer.plist";
$servermgrJabberConfigPath = "/Library/Preferences/com.apple.ichatserver.plist";
$ServiceName = "iChatServer";
%$BigList = "";
%dbinfo = {};
@MasterFilesList;
$VERBOSE = 0;
$DEBUG = 0;
$FUNCLOG = 0;
$PROGRESS = 0;
($OUTPUT_NONE, $OUTPUT_DEBUG, $OUTPUT_VERBOSE, $OUTPUT_PROGRESS,
$OUTPUT_FUNCLOG, $OUTPUT_PRINT) = (0..5);
$LOG_PATH = &_get_log_path(@ARGV);
if($ENV{DEBUG}) {$DEBUG = '1'; print "iChat Server backup DEBUG = 1\n";}
if($ENV{VERBOSE}) {$VERBOSE = '1'; print "iChat Server backup VERBOSE = 1\n";}
if($ENV{FUNCLOG} && $DEBUG) {$FUNCLOG = '1'; print "iChat Server backup FUNCLOG = 1\n";}
if($ENV{PROGRESS} && $DEBUG) {$PROGRESS = '1'; print "iChat Server backup PROGRESS = 1\n";}
&_log($OUTPUT_VERBOSE, "iChatServer_backup was called.\n");
ParseOptions();
if ($DEBUG)
{ &dumpAssociativeArray(@ARGV); }
my $pbuddyDbPath = qx(${PBUDDY} -c \"Print :jabberdDatabasePath:\" $servermgrJabberConfigPath);
chomp($pbuddyDbPath);
if (-e $pbuddyDbPath) {
$sqliteDbPath = $pbuddyDbPath;
} else {
$sqliteDbPath = $sqliteDefaultDbPath;
}
&validateOptionsAndDispatch(@ARGV);
exit();
sub validateOptionsAndDispatch()
{
%BigList = @_;
my $nothing = 0;
SWITCH: {
if (uc($BigList{"-cmd"}) eq uc("actions")) { if($DEBUG) {print("actions\n");} Actions(); last SWITCH; }
if (uc($BigList{"-cmd"}) eq uc("backup")) { if($DEBUG) {print("backup\n");} Backup(); last SWITCH; }
if (uc($BigList{"-cmd"}) eq uc("help")) { if($DEBUG) {print("help\n");} Usage(); last SWITCH; }
if (uc($BigList{"-cmd"}) eq uc("size")) { if($DEBUG) {print("size\n");} Size(); last SWITCH; }
if (uc($BigList{"-cmd"}) eq uc("version")) { if($DEBUG) {print("version\n");} Version(); last SWITCH; }
$nothing = 1;
}
if($nothing == 1)
{print("Legal options were not supplied!\n");Usage();}
}
sub Actions()
{
&_log($OUTPUT_FUNCLOG, "Start Actions-------------------------------------------------------+\n");
&_log($OUTPUT_VERBOSE, (qq(${PBUDDY} -c \"Print :BackupActions\" $SBS_CONF/$ServiceConf) . "\n"));
$Version = qx(${PBUDDY} -c \"Print :BackupActions\" $SBS_CONF/$ServiceConf);
&_log($OUTPUT_PRINT, $Version);
&_log($OUTPUT_FUNCLOG, "End Actions-------------------------------------------------------+\n");
}
sub Backup()
{
my $OPT = $BigList{"-opt"};
my $cmd = "";
my $jabberDataTmpDir = "";
umask(077);
&_log($OUTPUT_FUNCLOG, "Start Backup-------------------------------------------------------+\n");
if ($OPT ne "configuration" && $OPT ne "data" && $OPT ne "all") {
&_log($OUTPUT_PRINT, "No valid -opt selected for backup operation.\n");
return 1;
}
my $SBS_PATH=$BigList{"-path"};
if ($SBS_PATH eq "") {
&_log($OUTPUT_PRINT, "No -path given. Aborting.\n");
return 1;
}
if(! -e $SBS_PATH) {
&_log($OUTPUT_DEBUG, "${MKDIR} $SBS_PATH\n");
qx(${MKDIR} $SBS_PATH);
}
if ($OPT eq "all" || $OPT eq "configuration") {
&_log($OUTPUT_PROGRESS, "Backing up configuration files", 0);
my $configTarPath = $SBS_PATH."/"."iChatServer.conf.tar.gz";
my @configurationFiles;
my @configurationFilesPbuddy = qx(${PBUDDY} -c \"Print :ConfigurationFilesToTar:\" $SBS_CONF/$ServiceConf);
if ($ for (my $i = 1; $i < $ my $configTmp = $configurationFilesPbuddy[$i];
chomp($configTmp);
$configTmp =~ s/^\s*//;
push(@configurationFiles, $configTmp);
}
}
if ($VERBOSE) {
$cmd = "$TAR --ignore-failed-read -cvzf \"$configTarPath\"";
} else {
$cmd = "$TAR --ignore-failed-read -czf \"$configTarPath\"";
}
foreach $confFile (@configurationFiles) {
$cmd .= " $confFile";
}
&_log($OUTPUT_PRINT, "Executing command: $cmd\n");
system($cmd);
&_log($OUTPUT_VERBOSE, "Backed up configuration files to $configTarPath\n");
push(@MasterFilesList, "iChatServer.conf.tar.gz");
&_log($OUTPUT_PROGRESS, "Backing up configuration files", 100);
}
if ($OPT eq "all" || $OPT eq "data") {
&_log($OUTPUT_PROGRESS, "Backing up data files", 0);
my $dataTarPath = $SBS_PATH."/"."iChatServer.data.tar.gz";
my $sqlFilePath = "iChatServer_data_backup.sql";
$cmd = "rm -Rf \"$tmpPath/*\"";
system($cmd);
for (my $i = 0; $i < 5; $i++) {
$cmd = "$MKTEMP_PATH -d $tmpPath/jabber_backup.XXXXXXXXXXXXXXXXXXXXXXXX";
$jabberDataTmpDir = `$cmd`;
chomp($jabberDataTmpDir);
if (-e $jabberDataTmpDir) {
last;
}
if ($i == 4) {
die "Error: Cannot create temporary file:\n$jabberDataTmpDir";
}
}
qx(echo ".dump" | $SQLITE $sqliteDbPath > \"$jabberDataTmpDir/$sqlFilePath\");
my $origCWD = $ENV{PWD};
chdir("$jabberDataTmpDir");
if ($VERBOSE) {
$cmd = "$TAR -cvzf \"$dataTarPath\" \"$sqlFilePath\"";
print "Executing command: $cmd\n";
} else {
$cmd = "$TAR -czf \"$dataTarPath\" \"$sqlFilePath\"";
}
system($cmd);
chdir($origCWD);
unlink("$jabberDataTmpDir/$sqlFilePath");
qx(rm -Rf $jabberDataTmpDir);
&_log($OUTPUT_PROGRESS, "Backing up data files", 100);
&_log($OUTPUT_VERBOSE, "Backed up database to $dataTarPath\n");
push(@MasterFilesList, "iChatServer.data.tar.gz");
}
CreateBrowsePlist();
&_log($OUTPUT_FUNCLOG, "End Backup-------------------------------------------------------+\n");
}
sub CreateBrowsePlist()
{
my $SBS_PATH=$BigList{"-path"};
my $browseFilePath=$SBS_PATH . "/$ServiceName.browse.plist";
open(OUT, ">$browseFilePath") or die ("Couldn't open the $browseFilePath file for writing.\n");
select(OUT);
print ('<?xml version="1.0" encoding="UTF-8"?>' . "\n");
print ('<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' . "\n");
print ('<plist version="1.0">' . "\n");
print (' <array>' . "\n");
for($i=0; $i<=$ $LG=$MasterFilesList[$i];
chomp($LG);
print ' <string>'.$LG.'</string>'. "\n";
}
print (' </array>' . "\n");
print ("</plist>" . "\n");
close(OUT);
select(STDOUT); }
sub Size()
{
my $OPT = $BigList{"-opt"};
my $jabberDataTmpDir = "";
if ($OPT ne "configuration" && $OPT ne "data" && $OPT ne "all") {
&_log($OUTPUT_PRINT, "No valid -opt selected for size operation.\n");
&Usage;
}
umask(077);
&_log($OUTPUT_FUNCLOG, "Start Size-------------------------------------------------------+\n");
$totalsize = 0; if ($OPT eq "all" || $OPT eq "configuration") {
my @configurationFiles;
my @configurationFilesPbuddy = qx(${PBUDDY} -c \"Print :ConfigurationFilesToTar:\" $SBS_CONF/$ServiceConf);
if ($ for (my $i = 1; $i < $ my $configTmp = $configurationFilesPbuddy[$i];
chomp($configTmp);
$configTmp =~ s/^\s*//;
push(@configurationFiles, $configTmp);
}
}
foreach my $configFile (@configurationFiles) {
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($configFile);
$totalsize += $size;
}
}
if ($OPT eq "all" || $OPT eq "data") {
$cmd = "rm -Rf \"$tmpPath/*\"";
system($cmd);
for (my $i = 0; $i < 5; $i++) {
$jabberDataTmpDir = `$MKTEMP_PATH -d $tmpPath/jabber_backup.XXXXXXXXXXXXXXXXXXXXXXXX`;
chomp($jabberDataTmpDir);
if (-e $jabberDataTmpDir) {
last;
}
if ($i == 4) {
die "Error: Cannot create temporary file:\n$jabberDataTmpDir";
}
}
qx(echo ".dump" | $SQLITE $sqliteDbPath > \"$jabberDataTmpDir/$sqlFilePath\");
my $dataTarPath = "$jabberDataTmpDir/iChatServer_data_backup_tmp.tar.gz";
if ($VERBOSE) {
$cmd = "$TAR -cvzf \"$dataTarPath\" \"$jabberDataTmpDir/$sqlFilePath\"";
&_log($OUTPUT_PRINT, "Executing command: $cmd\n");
} else {
$cmd = "$TAR -czf \"$dataTarPath\" \"$jabberDataTmpDir/$sqlFilePath\"";
}
system($cmd);
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($dataTarPath);
unlink($dataTarPath);
unlink("$jabberDataTmpDir/$sqlFilePath");
qx(rm -Rf $jabberDataTmpDir);
$totalsize += $size;
}
$totalsize /= 1024;
&_log($OUTPUT_PRINT, "total := ".int($totalsize)."\n");
&_log($OUTPUT_FUNCLOG, "End Size-------------------------------------------------------+\n");
}
sub Version()
{
&_log($OUTPUT_FUNCLOG, "Start Version-------------------------------------------------------+\n");
$Version = qx(${PBUDDY} -c \"Print :Version\" $SBS_CONF/$ServiceConf);
&_log($OUTPUT_PRINT, $Version);
&_log($OUTPUT_FUNCLOG, "End Version-------------------------------------------------------+\n");
}
sub ParseOptions {
local (@optval) = @_;
local ($opt, @opts, %valFollows, @newargs);
while (@optval) {
$opt = shift(@optval);
push(@opts,$opt);
$valFollows{$opt} = shift(@optval);
}
@optArgs = ();
%opt = ();
arg: while (defined($arg = shift(@ARGV))) {
foreach $opt (@opts) {
if ($arg eq $opt) {
push(@optArgs, $arg);
if ($valFollows{$opt}) {
$opt{$opt} = shift(@ARGV);
push(@optArgs, $opt{$opt});
} else {
$opt{$opt} = 1;
}
next arg;
}
}
push(@newargs,$arg);
}
@ARGV = @newargs;
}
sub dumpAssociativeArray()
{
%BigList = @_;
while(($theKey, $theVal) = each (%BigList))
{ print "$theKey is the key for value $theVal\n"; }
if($BigList{"-cmd"} eq "backup")
{ print "cmd := ", $BigList{"-cmd"}, "\n"; }
}
sub timestamp()
{
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
localtime(time);
$year += 1900;
$mon += 1;
if ( $hour =~ /^\d$/ ) { $hour = "0" . $hour; }
if ( $min =~ /^\d$/ ) { $min = "0" . $min; }
if ( $sec =~ /^\d$/ ) { $sec = "0" . $sec; }
my $ret = $year."-".$mon."-".$mday."-$hour:$min:$sec";
return $ret;
}
sub Usage()
{
print "Usage:\n";
print "iChatServer_backup supports the following options:\n";
print " -cmd backup -path path -log logPath \n";
print " -cmd backup -path path -opt option\n";
print " where path is the path to the mounted image where the data was backed-up.\n";
print " where option is one of: configuration, data, all\n";
print " where logPath is the path to the backup log file for this service, it can be found in property list file := $SBS_CONF$ServiceConf\n";
print " -cmd size -opt option : Calculate size of the storage requirements for iChatServer.\n";
print " where option is one of: configuration, data, all\n";
print " -cmd help : Displays this usage.\n";
print " -cmd version : Prints out the version value from the property list := $SBS_CONF$ServiceConf\n";
exit(0);
}
sub _log {
my $type = shift;
my $msg = shift;
my $progress_percent = shift;
if ($PROGRESS && ($type == $OUTPUT_PROGRESS)) {
if (! defined($progress_percent)) {
&_log($OUTPUT_DEBUG, "ERROR: Missing progress percentage");
return;
}
my $pString = sprintf("\"## PROGRESS :: :: iChat Server :: $msg :: %%%f \"", $progress_percent);
qx($ECHO $pString >> $SBSProgressLogFile);
return;
}
if ($LOG_PATH ne "") {
open(OUT, ">>$LOG_PATH") || die "ERROR: Cannot open log file at path: $LOG_PATH : $!";
print OUT ×tamp."\t".$msg;
close(OUT);
}
if (($type == $OUTPUT_PRINT) ||
($FUNCLOG && ($type == $OUTPUT_FUNCLOG)) ||
($DEBUG && ($type == $OUTPUT_DEBUG)) ||
($VERBOSE && ($type == $OUTPUT_VERBOSE))) {
print $msg;
}
}
sub _get_log_path {
my ($theKey, $theVal);
my %theArgList = @ARGV;
while(($theKey, $theVal) = each (%theArgList)) {
if ($theKey eq "-log") {
return $theVal;
}
}
return "";
}