From 8063b198064dafb9f1e227a89989d9fe3c9af115 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sat, 17 Dec 2016 20:05:35 +0100 Subject: [PATCH] refactor --- borg_interface/backup_to_fileserver.py | 25 +++++----- borg_interface/interface_functions.py | 37 +++++++++----- borg_interface/main.py | 11 ++--- borg_interface/test.py | 67 +++++++++++++++++++++----- 4 files changed, 99 insertions(+), 41 deletions(-) mode change 100644 => 100755 borg_interface/test.py diff --git a/borg_interface/backup_to_fileserver.py b/borg_interface/backup_to_fileserver.py index de1667d..c89446d 100644 --- a/borg_interface/backup_to_fileserver.py +++ b/borg_interface/backup_to_fileserver.py @@ -8,6 +8,7 @@ import socket int_vars = interface_variables + def take_backup(): response = subprocess.Popen(['ping', '-c', '1', 'fileserver.2li.local']) response.wait() @@ -21,43 +22,45 @@ def take_backup(): print("Server not available") sys.exit(0) + def backup_home(): path_to_backup = "/home/andreas/" archive_name = (socket.gethostname() + "-home" - + time.strftime("_%Y-%m-%d_%H:%M")) + + time.strftime("_%Y-%m-%d_%H:%M")) p = subprocess.Popen(['borg', 'create', '--exclude', '/home/andreas/.cache', '--exclude', '/home/andreas/Downloads', '::' + archive_name, path_to_backup]) p.wait() + def backup_vms(): path_to_backup = "/mnt/sdc/VMs" archive_name = (socket.gethostname() + "-VMs" - + time.strftime("_%Y-%m-%d_%H:%M")) + + time.strftime("_%Y-%m-%d_%H:%M")) p = subprocess.Popen(['borg', 'create', '::' + archive_name, path_to_backup]) p.wait() + def prune_home(): archive_name = (socket.gethostname() + "-home") p = subprocess.Popen(['borg', 'prune', '--prefix', archive_name, - '--keep-hourly=24', '--keep-daily=7', - '--keep-weekly=4', '--keep-monthly=12', - '--keep-yearly=1']) + '--keep-hourly=24', '--keep-daily=7', + '--keep-weekly=4', '--keep-monthly=12', + '--keep-yearly=1']) p.wait() + def prune_vms(): archive_name = (socket.gethostname() + "-VMs") p = subprocess.Popen(['borg', 'prune', '--prefix', archive_name, - '--keep-hourly=24', '--keep-daily=7', - '--keep-weekly=4', '--keep-monthly=12', - '--keep-yearly=1']) + '--keep-hourly=24', '--keep-daily=7', + '--keep-weekly=4', '--keep-monthly=12', + '--keep-yearly=1']) p.wait() + interface_functions.configuration() -print("config finished") -interface_functions.pause() take_backup() -print("backup finished") diff --git a/borg_interface/interface_functions.py b/borg_interface/interface_functions.py index c310074..69ce82e 100644 --- a/borg_interface/interface_functions.py +++ b/borg_interface/interface_functions.py @@ -4,11 +4,11 @@ import configparser import subprocess import interface_variables import curses -import re int_vars = interface_variables screen = curses.initscr() + def get_param(prompt_string): screen.clear() screen.border(0) @@ -17,6 +17,7 @@ def get_param(prompt_string): input = screen.getstr(3, 2, 60) return input + def draw_menu(): screen.clear() screen.border(0) @@ -30,6 +31,7 @@ def draw_menu(): screen.addstr(10, 4, "0 - Exit") screen.refresh() + def draw_screen(r, c, message): screen.clear() screen.border(0) @@ -44,6 +46,7 @@ def list_archives(): borg_list.wait() less_output.wait() + def show_info(): archive_name = get_param("Please enter the archive name: ").decode('utf-8') curses.endwin() @@ -52,33 +55,36 @@ def show_info(): p.wait() pause() + def mount_archive(): archive_name = get_param("Please enter the archive name: ").decode('utf-8') int_vars.mount_point = os.path.join('/tmp', archive_name) if not os.path.exists(int_vars.mount_point): - os.makedirs(int_vars.mount_point) + os.makedirs(int_vars.mount_point) p = subprocess.Popen(['borg', 'mount', '::' + archive_name, int_vars.mount_point]) p.wait() draw_screen(2, 2, "Archive mounted at " + int_vars.mount_point + "/.") - screen.addstr(3, 2, "The archive will remain mounted as long this programm " - "is running.") + screen.addstr(3, 2, "The archive will remain mounted as long this program " + "is running.") screen.refresh() ncurses_pause(5) + def restore_archive(): archive_name = get_param("Please enter the archive name: ").decode('utf-8') restore_path = get_param("Please enter the path where you want to " - "restore to: ").decode('utf-8') + "restore to: ").decode('utf-8') draw_screen(2, 2, "Please wait while the archive gets restored.") if not os.path.exists(restore_path): os.makedirs(restore_path) p = subprocess.Popen(['borg', 'extract', '::' + archive_name] - ,cwd=restore_path) + , cwd=restore_path) p.wait() draw_screen(2, 2, "Archive extracted to " + restore_path) ncurses_pause(5) + def delete_archive(): archive_name = get_param("Please enter the archive name: ").decode('utf-8') draw_screen(2, 2, "Please wait while the archive gets deleted.") @@ -87,6 +93,7 @@ def delete_archive(): draw_screen(2, 2, "Archive " + archive_name + " deleted") ncurses_pause(5) + def create_archive(): archive_name = get_param("Please enter an archive name: ").decode('utf-8') path_to_backup = get_param("Please enter the " @@ -98,6 +105,7 @@ def create_archive(): draw_screen(2, 2, "Archive of " + path_to_backup + " created.") ncurses_pause(5) + def configuration(): # setup the config parser config = configparser.ConfigParser() @@ -113,15 +121,15 @@ def configuration(): config.read(config_file) else: print("Configuration file not found.") - exit() - # assign the repository variable depending wheter it's a remote or a local + sys.exit(0) + # assign the repository variable depending whether it's a remote or a local # repository if 'server' in config['DEFAULT']: repository = (config['DEFAULT']['user'] - + "@" - + config['DEFAULT']['server'] - + ":" - + config['DEFAULT']['repository_path']) + + "@" + + config['DEFAULT']['server'] + + ":" + + config['DEFAULT']['repository_path']) int_vars.server = config['DEFAULT']['server'] else: repository = config['DEFAULT']['repository_path'] @@ -131,8 +139,9 @@ def configuration(): os.environ['BORG_REPO'] = repository os.environ['BORG_PASSPHRASE'] = password + def exit(): - if (not int_vars.mount_point): + if not int_vars.mount_point: curses.endwin() os.system('clear') sys.exit(0) @@ -145,6 +154,7 @@ def exit(): os.rmdir(int_vars.mount_point) sys.exit(0) + def ncurses_pause(c): screen.border(0) screen.addstr(c, 2, "Press Enter to continue...") @@ -152,5 +162,6 @@ def ncurses_pause(c): input = screen.getstr(3, 2, 60) return input + def pause(): input("Press Enter to continue...") diff --git a/borg_interface/main.py b/borg_interface/main.py index 1545fe8..e15cbd9 100755 --- a/borg_interface/main.py +++ b/borg_interface/main.py @@ -1,8 +1,7 @@ #!/usr/bin/env python3 -import os import interface_functions -import interface_variables import curses + chosen_activity = None # The main menu starts there @@ -24,12 +23,12 @@ while chosen_activity != 0: # mounts a chosen archive to /tmp/archive name interface_functions.mount_archive() if chosen_activity == ord('4'): - interface_functions.restore_archive() + interface_functions.restore_archive() if chosen_activity == ord('5'): - interface_functions.delete_archive() + interface_functions.delete_archive() if chosen_activity == ord('6'): - interface_functions.create_archive() + interface_functions.create_archive() elif chosen_activity == ord('0'): - interface_functions.exit() + interface_functions.exit() except ValueError: print("Please enter a full number.") diff --git a/borg_interface/test.py b/borg_interface/test.py old mode 100644 new mode 100755 index c817152..236bdd3 --- a/borg_interface/test.py +++ b/borg_interface/test.py @@ -1,16 +1,61 @@ +#!/usr/bin/env python3 import subprocess -import socket -import time -import interface_functions import sys +import time +import socket +import interface_functions -path_to_backup = "/home/andreas/" -archive_name = (socket.gethostname() + "-home" - + time.strftime("_%Y-%m-%d_%H:%M")) +def take_backup(): + response = subprocess.Popen(['ping', '-c', '1', 'fileserver.2li.local']) + response.wait() + returncode = response.returncode + if returncode == 0: + backup_home() + backup_vms() + prune_home() + prune_vms() + else: + print("Server not available") + sys.exit(0) -p = subprocess.Popen(['borg', 'create', '--exclude', '/home/andreas/.cache', - '--exclude', '/home/andreas/Downloads', - '::' + archive_name, path_to_backup], stderr=subprocess.PIPE) -p.wait() -sys.exit(0) +def backup_home(): + path_to_backup = "/home/andreas/" + archive_name = (socket.gethostname() + "-home" + + time.strftime("_%Y-%m-%d_%H:%M")) + + p = subprocess.Popen(['borg', 'create', '--exclude', '/home/andreas/.cache', + '--exclude', '/home/andreas/Downloads', + '::' + archive_name, path_to_backup]) + p.wait() + + +def backup_vms(): + path_to_backup = "/mnt/sdc/VMs" + archive_name = (socket.gethostname() + "-VMs" + + time.strftime("_%Y-%m-%d_%H:%M")) + + p = subprocess.Popen(['borg', 'create', + '::' + archive_name, path_to_backup]) + p.wait() + + +def prune_home(): + archive_name = (socket.gethostname() + "-home") + p = subprocess.Popen(['borg', 'prune', '--prefix', archive_name, + '--keep-hourly=24', '--keep-daily=7', + '--keep-weekly=4', '--keep-monthly=12', + '--keep-yearly=1']) + p.wait() + + +def prune_vms(): + archive_name = (socket.gethostname() + "-VMs") + p = subprocess.Popen(['borg', 'prune', '--prefix', archive_name, + '--keep-hourly=24', '--keep-daily=7', + '--keep-weekly=4', '--keep-monthly=12', + '--keep-yearly=1']) + p.wait() + +interface_functions.configuration() +take_backup()