110 lines
3.0 KiB
Plaintext
110 lines
3.0 KiB
Plaintext
|
#! /usr/bin/perl -w
|
||
|
#----------------------------------------------------------------------
|
||
|
# copyright (C) 2006 SME Server
|
||
|
#
|
||
|
# This program is free software; you can redistribute it and/or modify
|
||
|
# it under the terms of the GNU General Public License as published by
|
||
|
# the Free Software Foundation; either version 2 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with this program; if not, write to the Free Software
|
||
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||
|
#
|
||
|
#----------------------------------------------------------------------
|
||
|
|
||
|
use strict;
|
||
|
use esmith::ConfigDB;
|
||
|
use esmith::BackupHistoryDB;
|
||
|
use esmith::Backup;
|
||
|
#use esmith::lockfile;
|
||
|
|
||
|
# lock file.. see bug 9127
|
||
|
my $backup_lock;
|
||
|
|
||
|
$ENV{PATH} = "/sbin/e-smith:/sbin:/bin:/usr/bin";
|
||
|
|
||
|
my $conf = esmith::ConfigDB->open || die("Could not open config db\n");
|
||
|
|
||
|
# set lock.. if not, exit
|
||
|
unless (SetLock()) {
|
||
|
die "Error: failed to create lock file.. is a backup already running?";
|
||
|
}
|
||
|
|
||
|
my $backup = $conf->get('backupwk');
|
||
|
my $status = $backup->prop('status') || 'disabled';
|
||
|
my $program = $backup->prop('Program') || 'dar';
|
||
|
|
||
|
|
||
|
|
||
|
unless ($status eq 'enabled')
|
||
|
{
|
||
|
print "Backup is disabled\n";
|
||
|
exit 0;
|
||
|
}
|
||
|
|
||
|
my $backups = esmith::BackupHistoryDB->open
|
||
|
|| die("Could not open backup history db\n");
|
||
|
my $now = time();
|
||
|
die "backup not allowed twice at the same date/time, delayed to next day\n" if $backups->get($now);
|
||
|
my $backup_rec = $backups->new_record($now, { type => 'backup_record' });
|
||
|
$backup_rec->set_prop('StartEpochTime', "$now");
|
||
|
$backup_rec->set_prop('BackupType', "workstation");
|
||
|
|
||
|
if ($status = system(qw(signal-event pre-backup)))
|
||
|
{
|
||
|
exit bad_exit($backup_rec, "pre-backup", $status);
|
||
|
}
|
||
|
|
||
|
if ($status = system("/etc/e-smith/events/actions/workstation-backup-$program", "DailyBackup"))
|
||
|
{
|
||
|
exit bad_exit($backup_rec, "backup", $status);
|
||
|
}
|
||
|
|
||
|
if ($status = system(qw(signal-event post-backup)))
|
||
|
{
|
||
|
exit bad_exit($backup_rec, "post-backup", $status);
|
||
|
}
|
||
|
|
||
|
$now = time();
|
||
|
$backup_rec->set_prop('EndEpochTime', "$now");
|
||
|
$backup_rec->set_prop('Result', "$status");
|
||
|
# remove lock
|
||
|
RemoveLock();
|
||
|
exit 0;
|
||
|
|
||
|
sub bad_exit
|
||
|
{
|
||
|
my ($backup_rec, $phase, $status) = @_;
|
||
|
my $now = time();
|
||
|
|
||
|
warn("Backup terminated: $phase failed - status: $status\n");
|
||
|
$backup_rec->set_prop('EndEpochTime', "$now");
|
||
|
$backup_rec->set_prop('Result', "$phase:$status");
|
||
|
# remove lock
|
||
|
RemoveLock();
|
||
|
return $status / 256;
|
||
|
}
|
||
|
|
||
|
# subs to set and remove lock on backup
|
||
|
|
||
|
sub SetLock
|
||
|
{
|
||
|
$backup_lock = esmith::Backup::set_lock;
|
||
|
return $backup_lock;
|
||
|
}
|
||
|
|
||
|
sub RemoveLock
|
||
|
{
|
||
|
if (defined($backup_lock))
|
||
|
{
|
||
|
esmith::Backup::remove_lock($backup_lock);
|
||
|
$backup_lock = undef;
|
||
|
}
|
||
|
}
|