From 5a4d9ff69a06fb47a11acd9f39feb0034939deee Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Tue, 13 Dec 2016 22:49:17 +0100 Subject: [PATCH] split the programm into modules --- borg_interface.cfg | 8 ++--- borg_interface.py | 80 +++++------------------------------------- interface_functions.py | 73 ++++++++++++++++++++++++++++++++++++++ interface_variables.py | 1 + 4 files changed, 86 insertions(+), 76 deletions(-) create mode 100644 interface_functions.py create mode 100644 interface_variables.py diff --git a/borg_interface.cfg b/borg_interface.cfg index 7bbbb1c..3cb0f43 100644 --- a/borg_interface.cfg +++ b/borg_interface.cfg @@ -1,5 +1,5 @@ [DEFAULT] -server: -user: -repository_path: /home/andreas/test -password: foo +server: fileserver.2li.local +user: borg +repository_path: /home/borg/backup/gwyn +password: diff --git a/borg_interface.py b/borg_interface.py index d92b3ce..7b3e21d 100755 --- a/borg_interface.py +++ b/borg_interface.py @@ -2,77 +2,13 @@ import os import configparser import subprocess +import interface_functions +import interface_variables chosen_activity = None -mount_point = None - -def list_archives(): - os.system('borg list | less') - -def show_info(): - archive_name = input("Please enter the archive name: ") - os.system('clear') - os.system('borg info ::' + archive_name) - print() - input("Press a key to continue.") - -def mount_archive(): - global mount_point - archive_name = input("Please enter the archive name: ") - mount_point = "/tmp/" + archive_name - if not os.path.exists(mount_point): - os.makedirs(mount_point) - os.system('borg mount ::' + archive_name + " " + mount_point) - print() - print("Archive mounted at " + mount_point + "/") - print() - -def restore_archive(): - archive_name = input("Please enter the archive name: ") - restore_path = input("Please enter the path where you want to " - "restore to: ") - if not os.path.exists(restore_path): - os.makedirs(restore_path) - p = subprocess.Popen(['borg', 'extract', '::' + archive_name] - ,cwd=restore_path) - p.wait() - print() - print("Archive extracted to " + restore_path) - print() - -def configuration(): - # setup the config parser - config = configparser.ConfigParser() - # read config file - config.read('borg_interface.cfg') - # assign the repository variable - if 'server' in config: - repository = (config['DEFAULT']['user'] - + "@" - + config['DEFAULT']['server'] - + ":" - + config['DEFAULT']['repository_path']) - else: - repository = config['DEFAULT']['repository_path'] - # assign the password variable - password = config['DEFAULT']['password'] - # set the environment variables - os.environ['BORG_REPO'] = repository - os.environ['BORG_PASSPHRASE'] = password - -def exit(): - global mount_point - if (not mount_point): - print() - else: - print() - print("Unmount Archive and remove folder.") - print() - os.system('fusermount -u' + " " + mount_point) - os.rmdir(mount_point) # The main menu starts there -configuration() +interface_functions.configuration() while chosen_activity != 0: os.system('clear') print("What would you like to do?") @@ -84,17 +20,17 @@ while chosen_activity != 0: if chosen_activity == 1: # prints all the archives in the repository and lists them with # less - list_archives() + interface_functions.list_archives() if chosen_activity == 2: # Displays all the information related to the archive name the user # enters - show_info() + interface_functions.show_info() if chosen_activity == 3: # mounts a chosen archive to /tmp/archive name - mount_archive() + interface_functions.mount_archive() if chosen_activity == 4: - restore_archive() + interface_functions.restore_archive() elif chosen_activity == 0: - exit() + interface_functions.exit() except ValueError: print("Please enter a full number.") diff --git a/interface_functions.py b/interface_functions.py new file mode 100644 index 0000000..aed3a87 --- /dev/null +++ b/interface_functions.py @@ -0,0 +1,73 @@ +import os +import configparser +import subprocess +import interface_variables + +int_vars = interface_variables + +def list_archives(): + os.system('borg list | less') + +def show_info(): + archive_name = input("Please enter the archive name: ") + os.system('clear') + os.system('borg info ::' + archive_name) + print() + input("Press a key to continue.") + +def mount_archive(): + archive_name = input("Please enter the archive name: ") + int_vars.mount_point = "/tmp/" + archive_name + if not os.path.exists(int_vars.mount_point): + os.makedirs(int_vars.mount_point) + os.system('borg mount ::' + archive_name + " " + int_vars.mount_point) + print() + print("Archive mounted at " + int_vars.mount_point + "/") + print() + +def restore_archive(): + archive_name = input("Please enter the archive name: ") + restore_path = input("Please enter the path where you want to " + "restore to: ") + if not os.path.exists(restore_path): + os.makedirs(restore_path) + p = subprocess.Popen(['borg', 'extract', '::' + archive_name] + ,cwd=restore_path) + p.wait() + print() + print("Archive extracted to " + restore_path) + print() + +def configuration(): + # setup the config parser + config = configparser.ConfigParser() + # read config file + config.read('borg_interface.cfg') + # assign the repository variable + print(config['DEFAULT']['server']) + if 'server' in config: + repository = (config['DEFAULT']['user'] + + "@" + + config['DEFAULT']['server'] + + ":" + + config['DEFAULT']['repository_path']) + print("remote archive") + else: + repository = config['DEFAULT']['repository_path'] + print("local archive") + input() + # assign the password variable + password = config['DEFAULT']['password'] + # set the environment variables + os.environ['BORG_REPO'] = repository + os.environ['BORG_PASSPHRASE'] = password + +def exit(): + if (not int_vars.mount_point): + print() + else: + print() + print("Unmount Archive and remove folder.") + print() + os.system('fusermount -u' + " " + int_vars.mount_point) + os.rmdir(int_vars.mount_point) diff --git a/interface_variables.py b/interface_variables.py new file mode 100644 index 0000000..eba0912 --- /dev/null +++ b/interface_variables.py @@ -0,0 +1 @@ +mount_point = None