This repository has been archived on 2020-04-03. You can view files and clone it, but cannot push or open issues or pull requests.
borg_interface/borg_interface/interface_functions.py

126 lines
3.8 KiB
Python
Raw Normal View History

2016-12-13 22:49:17 +01:00
import os
2016-12-15 09:58:13 +01:00
import sys
2016-12-13 22:49:17 +01:00
import configparser
import subprocess
import interface_variables
import curses
2016-12-13 22:49:17 +01:00
int_vars = interface_variables
def get_param(prompt_string):
screen.clear()
screen.border(0)
screen.addstr(2, 2, prompt_string)
screen.refresh()
input = screen.getstr(10, 10, 60)
return input
def draw_screen():
screen = curses.initscr()
screen.clear()
screen.border(0)
screen.addstr(2, 2, "Please enter a number...")
screen.addstr(4, 4, "1 - List archives in repository")
screen.addstr(5, 4, "2 - Show archive details")
screen.addstr(6, 4, "3 - Mount archive")
screen.addstr(7, 4, "4 - Restore an archive to specific location")
screen.addstr(8, 4, "0 - Exit")
screen.refresh()
2016-12-13 22:49:17 +01:00
def list_archives():
curses.endwin()
borg_list = subprocess.Popen(['borg', 'list'], stdout=subprocess.PIPE)
less_output = subprocess.Popen(['less'], stdin=borg_list.stdout)
borg_list.wait()
less_output.wait()
2016-12-13 22:49:17 +01:00
def show_info():
curses.endwin()
prompt_archive_name()
2016-12-13 22:49:17 +01:00
os.system('clear')
p = subprocess.Popen(['borg', 'info', '::' + int_vars.archive_name])
p.wait()
2016-12-13 22:49:17 +01:00
print()
2016-12-14 13:07:48 +01:00
pause()
2016-12-13 22:49:17 +01:00
def mount_archive():
curses.endwin()
prompt_archive_name()
int_vars.mount_point = "/tmp/" + int_vars.archive_name
2016-12-13 22:49:17 +01:00
if not os.path.exists(int_vars.mount_point):
os.makedirs(int_vars.mount_point)
p = subprocess.Popen(['borg', 'mount', '::' + int_vars.archive_name,
int_vars.mount_point])
p.wait()
2016-12-13 22:49:17 +01:00
print()
2016-12-14 13:07:48 +01:00
print("Archive mounted at " + int_vars.mount_point + "/.")
print("The archive will remain mounted as long this programm is running.")
2016-12-13 22:49:17 +01:00
print()
2016-12-14 13:07:48 +01:00
pause()
2016-12-13 22:49:17 +01:00
def restore_archive():
curses.endwin()
prompt_archive_name()
2016-12-13 22:49:17 +01:00
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', '::' + int_vars.archive_name]
2016-12-13 22:49:17 +01:00
,cwd=restore_path)
p.wait()
print()
print("Archive extracted to " + restore_path)
print()
2016-12-14 20:54:49 +01:00
pause()
2016-12-13 22:49:17 +01:00
def configuration():
# setup the config parser
config = configparser.ConfigParser()
# check whether the config file exists either in the home folder or next to
# the binary
home = os.getenv('HOME')
config_file = "borg_interface.cfg"
2016-12-15 15:25:37 +01:00
config_folder = ".config/borg_interface/"
config_path = os.path.join(home, config_folder, config_file)
if os.path.isfile(config_path):
config.read(config_path)
elif os.path.isfile(config_file):
config.read(config_file)
else:
print("Configuration file not found.")
2016-12-15 09:58:13 +01:00
sys.exit(1)
# assign the repository variable depending wheter it's a remote or a local
# repository
if 'server' in config['DEFAULT']:
2016-12-13 22:49:17 +01:00
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():
curses.endwin()
2016-12-13 22:49:17 +01:00
if (not int_vars.mount_point):
print()
2016-12-15 09:58:13 +01:00
sys.exit(0)
2016-12-13 22:49:17 +01:00
else:
print()
print("Unmount Archive and remove folder.")
print()
os.system('fusermount -u' + " " + int_vars.mount_point)
os.rmdir(int_vars.mount_point)
2016-12-15 09:58:13 +01:00
sys.exit(0)
2016-12-14 13:07:48 +01:00
def pause():
2016-12-15 15:24:06 +01:00
input("Press Enter to continue.")
def prompt_archive_name():
int_vars.archive_name = input("Please enter the archive name: ")