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

172 lines
5.3 KiB
Python
Raw Permalink 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
2016-12-17 20:05:35 +01:00
def get_param(prompt_string):
screen = curses.initscr()
2016-12-16 10:05:32 +01:00
screen.clear()
screen.border(0)
screen.addstr(2, 2, prompt_string)
screen.refresh()
2016-12-16 11:16:01 +01:00
input = screen.getstr(3, 2, 60)
2016-12-16 10:05:32 +01:00
return input
2016-12-17 20:05:35 +01:00
2016-12-16 13:13:58 +01:00
def draw_menu():
screen = curses.initscr()
2016-12-16 10:05:32 +01:00
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")
2016-12-17 11:07:50 +01:00
screen.addstr(8, 4, "5 - Delete an archive")
screen.addstr(9, 4, "6 - Create a backup")
screen.addstr(10, 4, "0 - Exit")
2016-12-16 10:05:32 +01:00
screen.refresh()
2016-12-17 20:05:35 +01:00
2016-12-16 13:13:58 +01:00
def draw_screen(r, c, message):
screen = curses.initscr()
2016-12-16 13:13:58 +01:00
screen.clear()
screen.border(0)
screen.addstr(r, c, message)
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
2016-12-17 20:05:35 +01:00
2016-12-13 22:49:17 +01:00
def show_info():
2016-12-16 13:13:58 +01:00
archive_name = get_param("Please enter the archive name: ").decode('utf-8')
curses.endwin()
2016-12-13 22:49:17 +01:00
os.system('clear')
p = subprocess.Popen(['borg', 'info', '::' + archive_name])
p.wait()
pause()
2016-12-13 22:49:17 +01:00
2016-12-17 20:05:35 +01:00
2016-12-13 22:49:17 +01:00
def mount_archive():
screen = curses.initscr()
2016-12-16 13:13:58 +01:00
archive_name = get_param("Please enter the archive name: ").decode('utf-8')
2016-12-16 10:05:32 +01:00
int_vars.mount_point = os.path.join('/tmp', archive_name)
2016-12-13 22:49:17 +01:00
if not os.path.exists(int_vars.mount_point):
2016-12-17 20:05:35 +01:00
os.makedirs(int_vars.mount_point)
2016-12-17 16:10:38 +01:00
p = subprocess.Popen(['borg', 'mount', '::' + archive_name,
int_vars.mount_point])
p.wait()
2016-12-16 13:13:58 +01:00
draw_screen(2, 2, "Archive mounted at " + int_vars.mount_point + "/.")
2016-12-17 20:05:35 +01:00
screen.addstr(3, 2, "The archive will remain mounted as long this program "
"is running.")
2016-12-16 13:13:58 +01:00
screen.refresh()
ncurses_pause(5)
2016-12-13 22:49:17 +01:00
2016-12-17 20:05:35 +01:00
2016-12-13 22:49:17 +01:00
def restore_archive():
2016-12-16 13:13:58 +01:00
archive_name = get_param("Please enter the archive name: ").decode('utf-8')
restore_path = get_param("Please enter the path where you want to "
2016-12-17 20:05:35 +01:00
"restore to: ").decode('utf-8')
2016-12-16 13:13:58 +01:00
draw_screen(2, 2, "Please wait while the archive gets restored.")
2016-12-13 22:49:17 +01:00
if not os.path.exists(restore_path):
os.makedirs(restore_path)
2016-12-16 13:13:58 +01:00
p = subprocess.Popen(['borg', 'extract', '::' + archive_name]
2016-12-17 20:05:35 +01:00
, cwd=restore_path)
2016-12-13 22:49:17 +01:00
p.wait()
2016-12-16 13:13:58 +01:00
draw_screen(2, 2, "Archive extracted to " + restore_path)
ncurses_pause(5)
2016-12-13 22:49:17 +01:00
2016-12-17 20:05:35 +01:00
2016-12-17 11:07:50 +01:00
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.")
p = subprocess.Popen(['borg', 'delete', '::' + archive_name])
p.wait()
draw_screen(2, 2, "Archive " + archive_name + " deleted")
ncurses_pause(5)
2016-12-17 20:05:35 +01:00
2016-12-17 11:07:50 +01:00
def create_archive():
archive_name = get_param("Please enter an archive name: ").decode('utf-8')
2016-12-17 16:10:38 +01:00
path_to_backup = get_param("Please enter the "
"path to backup: ").decode('utf-8')
2016-12-17 11:07:50 +01:00
draw_screen(2, 2, "Please wait while the backup gets created.")
2016-12-17 16:10:38 +01:00
p = subprocess.Popen(['borg', 'create', '::' + archive_name,
path_to_backup])
2016-12-17 11:07:50 +01:00
p.wait()
draw_screen(2, 2, "Archive of " + path_to_backup + " created.")
ncurses_pause(5)
2016-12-17 20:05:35 +01:00
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.")
sys.exit(1)
2016-12-17 20:05:35 +01:00
# assign the repository variable depending whether 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']
2016-12-17 20:05:35 +01:00
+ "@"
+ config['DEFAULT']['server']
+ ":"
+ config['DEFAULT']['repository_path'])
int_vars.server = config['DEFAULT']['server']
2016-12-13 22:49:17 +01:00
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
2016-12-17 20:05:35 +01:00
2016-12-13 22:49:17 +01:00
def exit():
2016-12-17 20:05:35 +01:00
if not int_vars.mount_point:
2016-12-16 13:13:58 +01:00
curses.endwin()
2016-12-16 10:05:32 +01:00
os.system('clear')
2016-12-15 09:58:13 +01:00
sys.exit(0)
2016-12-13 22:49:17 +01:00
else:
2016-12-16 13:13:58 +01:00
curses.endwin()
os.system('clear')
2016-12-13 22:49:17 +01:00
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
2016-12-17 20:05:35 +01:00
def ncurses_pause(c):
screen = curses.initscr()
2016-12-16 13:13:58 +01:00
screen.border(0)
screen.addstr(c, 2, "Press Enter to continue...")
2016-12-16 13:13:58 +01:00
screen.refresh()
input = screen.getstr(3, 2, 60)
return input
2016-12-17 20:05:35 +01:00
2016-12-14 13:07:48 +01:00
def pause():
2016-12-16 13:13:58 +01:00
input("Press Enter to continue...")