Commit 71a57428 authored by Daniel Armbruster's avatar Daniel Armbruster Committed by thomas.forbriger
Browse files

Proceeding

This is a legacy commit from before 2015-05-18.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.



SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 4134
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 9f0d0a7c
......@@ -8,7 +8,7 @@
# @author Daniel Armbruster
# \date 11/09/2011
#
# Purpose: Generate a crontab for the csback toolkit using the a configuration
# Purpose: Generate a crontab for the csback toolkit using a configuration
# file.
#
# ----
......@@ -40,6 +40,7 @@ import getopt
import os
import pwd
import string
from datetime import datetime
version="cscron2back V0.1"
license="GPL"
......@@ -55,7 +56,7 @@ class Usage(Exception):
or: csback2cron -h
Note: If [-i CONFIGFILE] isn't passed as a argument, csback2cron uses
~/.csbackrc\n"""
if sys.version_info >= (3,)
if sys.version_info >= (3,):
print("csback2cron:" + self.msg, file=sys.stderr)
print(usage_text, file=sys.stderr)
else:
......@@ -86,10 +87,86 @@ class Processor():
def __init__(self, configfile, crontabfile):
self.__configfile = str(configfile)
self.__crontabfile = str(crontabfile)
# Check once again the reading function
# Note on sheet of paper
def read(self):
configfile = open(self.__configfile)
def convert(self):
def write(self):
lines = configfile.readlines()
configfile.close()
# strip comment lines
lines = list(filter((lambda x: x[0] != '#'), lines))
# strip eol
lines = [l.rstrip() for l in lines]
# get mail= line
tmp = str(list(filter((lambda x: x[0:5] == "mail="), lines))[0])
lines.remove(lines.index(tmp))
if len(tmp[5:]) == 0:
self.__sendmail = False
else:
self.__sendmail = True
self.__addresses = tmp[5:]
# collect setting lines
for line in lines:
l = line.split(" ")
d = dict(zip(self.__keylist, l))
self.__sLines.append(d)
def write(self, outlines):
# header lines
output = ['# This is <'+self.__crontabfile+'>',
'# Generated with csback2cron.',
'# '+datetime.now().strftime("%Y-%m-%d %H:%M:%S")]
output.extend(self.__convert())
crontabfile = open(self.__crontabfile, 'w')
crontabfile.writelines(output)
crontabfile.close()
def __convert(self):
res = []
for D in self.__sLines:
# check flags
flags = {}
if self.__keylist[2] in D:
flags = self__.check_flags(D[self.__keylist[2]])
s = D[self.__keylist[0]] + " "
if self.__keylist[3] in x:
# backup is desired - use rsync
s += ''
else:
# test only
s += 'csbackchk' + D[self.__keylist[1]]
if len(flags) != 0:
if __flags[1] in flags and flags[__flags[1]]:
s += " " + __flags[1]
if __flags[3] in flags and flags[__flags[3]]:
s += " " + __flags[3] + " " + flags[__flags[3]]
res.append(s)
# email report
if self.__sendmail:
# TODO: variable time
s = '0 12 * * * csbackmail ' + self.__addresses
res.append(s)
return res
def __check_flags(self, flags):
res = dict.fromkeys(__flags, False)
l = flags.split()
for f in __flags[:3]:
if __flags[f] in l:
res[__flags[f]] == True
# handle exclude flag
if __flags[3] in l and l[l.index(__flags(3)+1)] not in __flags:
res[__flags[3]] = l[l.index(__flags(3)+1)]
return res
__flags = ['-c', '-t', '-b', '-e']
__keylist = ['cronexpr', 'source', 'flags', 'target']
# REMEMBER: Not applying all tests yet!
#def __check_cronexpr(self, expr):
#def __check_dir(self, dir):
# -----------------------------------------------------------------------------
def main(argv=None):
......@@ -100,7 +177,7 @@ def main(argv=None):
opts, args = getopt.getopt(argv[1:], "hvi:", ["help", "verbose"])
except getopt.error, msg:
raise Usage(msg)
if len(args) == 0
if len(args) == 0:
raise Usage("CRONTABFILENAME not given.")
output = None
input = '/home/' + get_username()+ '/.csbackrc'
......@@ -115,7 +192,9 @@ def main(argv=None):
input = a
else:
raise Usage("Unhandled option chosen.")
data = read()
Processor process(input,output)
process.read()
process.write()
except Usage as err:
err.display()
return 2
......
......@@ -23,13 +23,15 @@
# pages.
#
# An example setting line is given here.
# It's important to know that the entries in a setting line have to be seperated
# with two whitspaces.
# -----------------------------------------------------------------------------
# CRONEXPR SOURCE TARGET FLAGS
# CRONEXPR SOURCE FLAGS TARGET
# -----------------------------------------------------------------------------
#
# Example 1)
#
# * */10 * * * /data /data.bak -e *log
# * */10 * * * /data -e *log /data.bak
#
# This line will perform a backup of /data exluding subdirectories matching the
# pattern *log to /data.bak every ten hours.
......@@ -39,7 +41,7 @@
# Another possibility is to set up a line as follows, if only a test of a
# special directory (e.g. /data.bak) is wished.
#
# * * */3 * * /data.bak -t -e *log
# * * */3 * * /data.bak -t -e *log
#
# CRONEXPR:
# With the cron expression you're able to schedule your backup. For further
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment