2016-11-13 22:14:09 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
import sys
|
2016-12-12 13:34:00 +01:00
|
|
|
import configparser
|
2016-12-13 15:01:34 +01:00
|
|
|
import subprocess
|
2016-12-12 13:34:00 +01:00
|
|
|
|
2016-12-12 20:04:42 +01:00
|
|
|
chosen_activity = None
|
|
|
|
mount_point = None
|
2016-12-12 13:34:00 +01:00
|
|
|
|
2016-12-12 19:06:08 +01:00
|
|
|
# setup the config parser
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
|
|
|
|
# read config file
|
|
|
|
config.read('borg_interface.cfg')
|
|
|
|
|
|
|
|
# assign the repository variable
|
2016-12-13 14:09:19 +01:00
|
|
|
if 'server' in config:
|
|
|
|
repository = (config['DEFAULT']['user']
|
|
|
|
+ "@"
|
|
|
|
+ config['DEFAULT']['server']
|
|
|
|
+ ":"
|
|
|
|
+ config['DEFAULT']['repository_path'])
|
|
|
|
else:
|
|
|
|
repository = config['DEFAULT']['repository_path']
|
2016-12-12 19:06:08 +01:00
|
|
|
|
|
|
|
# assign the password variable
|
|
|
|
password = config['DEFAULT']['password']
|
|
|
|
|
|
|
|
# set the environment variables
|
|
|
|
os.environ['BORG_REPO'] = repository
|
2016-12-12 20:04:42 +01:00
|
|
|
os.environ['BORG_PASSPHRASE'] = password
|
2016-12-12 13:34:00 +01:00
|
|
|
|
|
|
|
# The main menu starts there
|
2016-11-14 18:08:19 +01:00
|
|
|
while chosen_activity != 0:
|
|
|
|
print("What would you like to do?")
|
|
|
|
# Start the chosen activity and go back to the activity selector.
|
2016-12-13 14:24:16 +01:00
|
|
|
print("1: List Backups, 2: Show archive details, 3: Mount Archive, "
|
2016-12-13 14:18:30 +01:00
|
|
|
"4: Restore Backup, 0: Exit")
|
2016-11-14 18:08:19 +01:00
|
|
|
try:
|
2016-12-12 19:06:08 +01:00
|
|
|
chosen_activity = int(input("Choose the desired activity: "))
|
2016-11-14 18:08:19 +01:00
|
|
|
if chosen_activity == 1:
|
2016-12-12 13:34:00 +01:00
|
|
|
# prints all the archives in the repository and lists them with
|
|
|
|
# less
|
2016-12-12 13:28:25 +01:00
|
|
|
os.system('borg list | less')
|
2016-11-14 18:08:19 +01:00
|
|
|
if chosen_activity == 2:
|
2016-12-12 13:34:00 +01:00
|
|
|
# Displays all the information related to the archive name the user
|
|
|
|
# enters
|
2016-12-12 13:29:34 +01:00
|
|
|
archive_name = input("Please enter the archive name: ")
|
2016-11-14 18:08:19 +01:00
|
|
|
os.system('borg info ::' + archive_name)
|
2016-12-12 13:28:25 +01:00
|
|
|
if chosen_activity == 3:
|
2016-12-12 13:34:00 +01:00
|
|
|
# mounts a chosen archive to /tmp/archive name
|
2016-12-12 13:29:34 +01:00
|
|
|
archive_name = input("Please enter the archive name: ")
|
2016-12-12 13:28:25 +01:00
|
|
|
mount_point = "/tmp/" + archive_name
|
|
|
|
if not os.path.exists(mount_point):
|
|
|
|
os.makedirs(mount_point)
|
|
|
|
os.system('borg mount ::' + archive_name + " " + mount_point)
|
2016-11-14 18:08:19 +01:00
|
|
|
print()
|
2016-12-12 20:04:42 +01:00
|
|
|
print("Archive mounted at " + mount_point + "/")
|
|
|
|
print()
|
2016-12-13 13:40:15 +01:00
|
|
|
if chosen_activity == 4:
|
|
|
|
archive_name = input("Please enter the archive name: ")
|
2016-12-13 14:24:16 +01:00
|
|
|
restore_path = input("Please enter the path where you want to "
|
2016-12-13 14:41:18 +01:00
|
|
|
"restore to: ")
|
|
|
|
working_directory = os.system('pwd')
|
|
|
|
if not os.path.exists(restore_path):
|
|
|
|
os.makedirs(restore_path)
|
2016-12-13 16:13:40 +01:00
|
|
|
p = subprocess.Popen(['borg', 'extract', ':: + archive_name],
|
|
|
|
cwd=restore_path)
|
2016-12-13 15:01:34 +01:00
|
|
|
p.wait()
|
2016-12-13 16:13:40 +01:00
|
|
|
print()
|
|
|
|
print("Archive extracted to " + restore_path)
|
|
|
|
print()
|
2016-12-12 20:04:42 +01:00
|
|
|
elif chosen_activity == 0:
|
|
|
|
if (not mount_point):
|
|
|
|
print()
|
|
|
|
else:
|
|
|
|
print()
|
|
|
|
print("Unmount Archive and remove folder.")
|
|
|
|
print()
|
|
|
|
os.system('fusermount -u' + " " + mount_point)
|
|
|
|
os.rmdir(mount_point)
|
2016-11-14 18:08:19 +01:00
|
|
|
except ValueError:
|
2016-12-12 19:06:08 +01:00
|
|
|
print("Please enter a full number.")
|