Python – NCP Mount, Rsync

[code]
#!/usr/bin/python
# Kenneth Holmqvist 2014
# kholmqvist88@gmail.com
# Use this script to rsync folders from source to destination
# and receive an email with the output.
# Version 0.1
##########################
import smtplib, subprocess, argparse, time;

class Email(object):
def __init__(self, smtp_server, port, ssl, username, password, to, subject, message):
self.smtp_server = smtp_server;
self.port = int(port);
self.ssl = ssl;
self.username = username;
self.password = password;
self.to = to;
self.subject = subject;
header = ‘To: ‘ + self.to + ‘\n’ + ‘From: ‘ + self.username + ‘\n’ + ‘Subject: ‘ + self.subject;
message = header + ‘\n’ + message;
self.message = str(message);

def Send(self):
try:
server = smtplib.SMTP(self.smtp_server, self.port);
except smtplib.socket.gaierror:
return False;

if self.ssl==”ssl”:
try:
server.ehlo_or_helo_if_needed();
except smtplib.SMTPHeloError:
return False;
try:
server.starttls();
except smtplib.SMTPExeception:
return False;

if self.ssl==”none” or self.ssl==”ssl”:
try:
server.login(self.username, self.password);
except smtplib.SMTPAuthenticationError:
return False;

try:
server.sendmail(self.username, self.to, self.message);
return True;
except smtplib.SMTPDataError:
return False;
finally:
server.quit();

class Rsync(object):
def __init__(self, source, destination, options):
self.source = source;
self.destination = destination;
self.options = options;

def Backup(self):
command = [“rsync”, self.options, self.source, self.destination];
rsync = subprocess.check_output(command);
return rsync;

class Novell(object):
def __init__(self, server, server_name, username, password, volume, mount_point):
self.server = server;
self.server_name = server_name;
self.username = username;
self.password = password;
self.volume = volume;
self.mount_point = mount_point;

def Connect(self):
command = [“ncpmount”, “-A”, self.server, “-S”, self.server_name, “-U”, self.username, “-P”, self.password, “-V”, self.volume, self.mount_point];
ncpmount = subprocess.call(command);
return ncpmount;

def Disconnect(self):
command = [“ncpunmount”, self.mount_point];
ncpunmount = subprocess.call(command);
return ncpunmount;

## Variables
smtp_server=”smtp.gmail.com”;
smtp_port=”587″;
smtp_ssl=”ssl”
smtp_username=”example@example.com”;
smtp_password=””;
email_to=”example@example.com”;
subject=”This is a test”;
rsync_options=”-avhPn”;

parser = argparse.ArgumentParser();
parser.add_argument(“-A”,”–address”,help=”IP or FQDN for Novell Server”);
parser.add_argument(“-S”,”–server”,help=”Server name to be used for Novell Server”);
parser.add_argument(“-U”,”–username”,help=”Novell Username”);
parser.add_argument(“-P”,”–password”,help=”Novell Password”);
parser.add_argument(“-V”,”–volume”,help=”Novell Volume”);
parser.add_argument(“-M”,”–mountpoint”,help=”Local mount point for Novell share”);
parser.add_argument(“-s”,”–source”,help=”Rsync source folder”);
parser.add_argument(“-d”,”–destination”,help=”Rsync destination folder”);
arg = parser.parse_args();

def main(novell_server,novell_server_name,novell_username,novell_password,novell_volume,novell_mountpoint,rsync_source,rsync_destination,rsync_options):
novell = Novell(novell_server,novell_server_name,novell_username,novell_password,novell_volume,novell_mountpoint);
novell.Connect();
#time.sleep(10);
rsync = Rsync(rsync_source,rsync_destination,rsync_options);
output = rsync.Backup();
mail = Email(smtp_server,smtp_port,smtp_ssl,smtp_username,smtp_password,email_to, subject, output);
mail.Send();
novell.Disconnect();

main(arg.address,arg.server,arg.username,arg.password,arg.volume,arg.mountpoint,arg.source,arg.destination,rsync_options);
[/code]


Kenneth Holmqvist