Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 103 lines (83 sloc) 2.93 KB
#!/usr/bin/perl -w
#
# Copyright (C) 2013 Nethesis S.r.l.
# http://www.nethesis.it - support@nethesis.it
#
# Original work:
# copyright (C) 2006-2007 Jean-Paul Leclere <jean-paul@leclere.org>
# copyright (C) 2007 Charlie Brady <charlieb@e-smith.com>
#
# This script is part of NethServer.
#
# NethServer 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 3 of the License,
# or any later version.
#
# NethServer 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 NethServer. If not, see <http://www.gnu.org/licenses/>.
#
use strict;
use Errno;
use File::Temp;
use esmith::ConfigDB;
use NethServer::BackupData();
use File::Path qw(make_path);
sub ldie;
# The action must be invoked at least with 1 parameter: an event name (could be a fake one)
# If a second parameter is passed, it must be a valid key inside the backups db
my $event = shift || die("No event given");
my $name = shift || die("No name given");
my $db = esmith::ConfigDB->open_ro('backups') || die("Could not open backups db\n");
my $mntdir = "/mnt/backup-$name";
my $backupwk = $db->get($name) || die("No '$name' db entry found\n");
my $status = $backupwk->prop('status') || 'disabled';
exit 0 if ($status eq 'disabled');
my $smbhost = $backupwk->prop('SMBHost');
my $smbshare = $backupwk->prop('SMBShare');
my $login = $backupwk->prop('SMBLogin');
my $password = $backupwk->prop('SMBPassword');
my $VFSType = $backupwk->prop('VFSType') || 'UNKNOWN';
my $err = 0;
my $mntdone = 0;
exit 0 unless ($VFSType eq 'cifs');
our $b = new NethServer::BackupData();
# creating credentials file
my $tmp = File::Temp->new(UNLINK => 1);
chmod(0600,$tmp->filename);
print $tmp "user=$login\n";
print $tmp "password=$password\n";
# creating mount directory
if ( ! -d $mntdir ) {
make_path($mntdir);
}
# mounting backup directory if not already mounted
if (!$b->is_mounted("\/$smbhost(.*)\/$smbshare") && !$b->is_mounted($mntdir)) {
# mount the backup dir
$err = qx(/bin/mount -t cifs "//$smbhost/$smbshare" $mntdir -o credentials=$tmp,nounix 2>&1);
if ($? > 0) {
# HACK: try to mount using old CIFS version
$err = qx(/bin/mount -t cifs "//$smbhost/$smbshare" $mntdir -o credentials=$tmp,nounix,vers=1.0 2>&1);
} else {
if ($err) {
$err =~s/\n/ /g;
ldie("Error while mounting $smbhost:$smbshare : " . $err);
}
}
}
$mntdone = 1;
# verify $mntdir has an entry in /proc/mounts
if (!$b->is_mounted($mntdir)) {
ldie("Backup directory is not mounted");
}
sub ldie
{
my $errmsg = shift;
system("/bin/umount", "$mntdir") if $mntdone;
die($errmsg."\n");
}