|
|
@@ -0,0 +1,150 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+# Backup Python Script
|
|
|
+# Version: 0.0.1.0
|
|
|
+# Maintainer: Tobias Simetsreiter <tobias@tsimnet.eu>
|
|
|
+
|
|
|
+import sys
|
|
|
+import os
|
|
|
+
|
|
|
+def main():
|
|
|
+ args = _parser()
|
|
|
+ if "func" in args:
|
|
|
+ return args.func()
|
|
|
+
|
|
|
+def _parser():
|
|
|
+ import argparse as ap
|
|
|
+ parser = ap.ArgumentParser()
|
|
|
+ parser.set_defaults(func=parser.print_help)
|
|
|
+ subp = parser.add_subparsers()
|
|
|
+ subp_sc = subp.add_parser("shellscript")
|
|
|
+ subp_sc.set_defaults(func=_shellscript_command)
|
|
|
+ subp_sc = subp.add_parser("cronjob")
|
|
|
+ subp_sc.set_defaults(func=lambda:print(Cronjob(command="asdf")))
|
|
|
+ return parser.parse_args()
|
|
|
+
|
|
|
+def _shellscript_command(args):
|
|
|
+ return bash(SHELLSCRIPT).returncode
|
|
|
+
|
|
|
+def bash(script="", args=[], encoding="utf-8"):
|
|
|
+ import subprocess as sub
|
|
|
+ proc = sub.Popen(["bash"], stdin=sub.PIPE)
|
|
|
+ proc.stdin.write(script.encode(encoding))
|
|
|
+ proc.stdin.flush()
|
|
|
+ proc.wait()
|
|
|
+ return proc
|
|
|
+
|
|
|
+def tarxz():
|
|
|
+ pass
|
|
|
+
|
|
|
+class Cronjob:
|
|
|
+ _minute = "*"
|
|
|
+ _hour = "*"
|
|
|
+ _day_of_month = "*"
|
|
|
+ _month = "*"
|
|
|
+ _day_of_week = "*"
|
|
|
+ _user = "root"
|
|
|
+ _command = ""
|
|
|
+
|
|
|
+ def __init__(self, *args, **kwargs):
|
|
|
+ for key,value in kwargs.items():
|
|
|
+ getattr(self,"_"+key)
|
|
|
+ setattr(self,"_"+key,value)
|
|
|
+
|
|
|
+ def __str__(self):
|
|
|
+ CRON_TEMPLATE = '{self.minute} {self.hour} {self.day_of_month} {self.month} {self.day_of_week} {self.user} {self.command}'
|
|
|
+ return CRON_TEMPLATE.format(self=self)
|
|
|
+
|
|
|
+crontab_defaults={
|
|
|
+ "minute":"*",
|
|
|
+ "hour":"*",
|
|
|
+ "day_of_month": "*",
|
|
|
+ "month": "*",
|
|
|
+ "day_of_week": "*",
|
|
|
+ "user": "root",
|
|
|
+ "command": "",
|
|
|
+}
|
|
|
+
|
|
|
+def crontab(name=__file__, args={}):
|
|
|
+ format_args = crontab_defaults.copy()
|
|
|
+ format_args.update(args)
|
|
|
+
|
|
|
+SHELLSCRIPT = '''#!/usr/bin/env bash
|
|
|
+
|
|
|
+main(){
|
|
|
+ trap 'errhandler $LINENO' ERR
|
|
|
+ BACKUPDIR="/mnt/backup"
|
|
|
+ MAILRCP=""
|
|
|
+ checkmount "$BACKUPDIR"
|
|
|
+ tarxz root/scripts/ "$BACKUPDIR/root-scripts.tar.xz"
|
|
|
+ tarxz etc/apache2/ "$BACKUPDIR/apache-config.tar.xz"
|
|
|
+ tarxz etc/postfix/ "$BACKUPDIR/etc-postfix.tar.xz"
|
|
|
+ tarxz var/www/html/ "$BACKUPDIR/adms-html.tar.xz"
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+setup(){
|
|
|
+while getopts "h?vf:" opt; do
|
|
|
+ case "$opt" in
|
|
|
+ h|\?)
|
|
|
+ show_help
|
|
|
+ exit 0
|
|
|
+ ;;
|
|
|
+ v) verbose=1
|
|
|
+ set -x
|
|
|
+ ;;
|
|
|
+ f) output_file=$OPTARG
|
|
|
+ ;;
|
|
|
+ esac
|
|
|
+done
|
|
|
+
|
|
|
+if [ -t 1 ]
|
|
|
+then
|
|
|
+ export TTY=/dev/tty
|
|
|
+else
|
|
|
+ export TTY=/dev/null
|
|
|
+fi
|
|
|
+
|
|
|
+set -o history
|
|
|
+shopt -s lastpipe
|
|
|
+
|
|
|
+export LOG=""
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+errhandler(){
|
|
|
+ local RETURNCODE="$?"
|
|
|
+ local LAST_CMD=$(echo `history |tail -n3 |head -n1` | sed 's/[0-9]* //')
|
|
|
+ printf "Error on line: $1,RETURNCODE: $RETURNCODE, CMD: [$LAST_CMD]\n"
|
|
|
+ printf ">>>LOG:\n$LOG<<<END LOG\n"
|
|
|
+ exit $RETURNCODE
|
|
|
+}
|
|
|
+
|
|
|
+log_wrap(){
|
|
|
+export BASHOPTS
|
|
|
+LOG="$LOG$($@)\n";RETURNCODE=$?;
|
|
|
+export LOG
|
|
|
+return $RETURNCODE
|
|
|
+}
|
|
|
+
|
|
|
+log_send(){
|
|
|
+ printf "$LOG" | sendmail
|
|
|
+}
|
|
|
+
|
|
|
+checkmount(){
|
|
|
+ log_wrap grep -qs "$1" /proc/mounts
|
|
|
+}
|
|
|
+
|
|
|
+tarxz(){
|
|
|
+ local SOURCE="$1"
|
|
|
+ local TARGET="$2"
|
|
|
+ XZ_OPT="-4e -T 0" log_wrap tar -c -J -C / -f "$TARGET" "$SOURCE"
|
|
|
+ return $?
|
|
|
+}
|
|
|
+
|
|
|
+setup "$@"
|
|
|
+main
|
|
|
+exit 0
|
|
|
+'''
|
|
|
+
|
|
|
+if __name__=='__main__':
|
|
|
+ sys.exit(main())
|