2021-05-05 15:21:54 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
project: Backuppy
|
2021-05-07 10:08:33 +00:00
|
|
|
version: 0.8
|
2021-05-05 15:21:54 +00:00
|
|
|
file: install.py
|
2021-05-07 10:08:33 +00:00
|
|
|
summary: python installer-script in CLI-mode
|
2021-05-05 15:21:54 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Standard library imports
|
2021-05-06 17:40:07 +00:00
|
|
|
import sys
|
2021-05-05 15:21:54 +00:00
|
|
|
import os
|
2021-05-06 17:40:07 +00:00
|
|
|
import time
|
2021-05-05 15:21:54 +00:00
|
|
|
|
|
|
|
# local imports
|
|
|
|
from languages import english
|
|
|
|
from languages import german
|
|
|
|
|
|
|
|
# local globals
|
2021-05-06 17:40:07 +00:00
|
|
|
# ----------------------
|
2021-05-07 10:08:33 +00:00
|
|
|
VERSION: str = "0.8"
|
|
|
|
EMAIL = "fotocoder@joschu.ch"
|
2021-05-06 17:40:07 +00:00
|
|
|
EXCLUDE_FILE = "exclude.txt"
|
|
|
|
BACKUPPY_SCRIPT = "Backuppy.sh"
|
2021-05-07 10:08:33 +00:00
|
|
|
# ----------------------
|
|
|
|
MYDIR = os.getcwd()
|
|
|
|
EXCLUDE: bool = False
|
2021-05-06 17:40:07 +00:00
|
|
|
SHELL = os.environ.get("SHELL")
|
|
|
|
HOME = os.environ.get("HOME")
|
2021-05-07 10:08:33 +00:00
|
|
|
LANG_EN = "English"
|
|
|
|
LANG_DE = "German"
|
|
|
|
LANGUAGE = LANG_EN
|
2021-05-05 15:21:54 +00:00
|
|
|
RSYNC_CMD: str = None
|
2021-05-06 17:40:07 +00:00
|
|
|
|
|
|
|
def set_language(language):
|
|
|
|
global LANGUAGE
|
|
|
|
LANGUAGE = language
|
|
|
|
|
2021-05-10 09:29:40 +00:00
|
|
|
def trace(message_txt, prefix_crlf=False):
|
|
|
|
""" Print a formatted message to std out.
|
|
|
|
|
|
|
|
:param "message_txt" [in] The message text that should be displayed.
|
|
|
|
:param "prefix_crlf" [in] If True, a carriage return/line feed will be done prior to the message text.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
if prefix_crlf:
|
|
|
|
print("\n")
|
2021-05-06 17:40:07 +00:00
|
|
|
print("[ OK ] " + message_txt)
|
2021-05-05 15:21:54 +00:00
|
|
|
|
|
|
|
def get_lang_text(search_str: str):
|
2021-05-06 17:40:07 +00:00
|
|
|
global LANGUAGE
|
2021-05-05 15:21:54 +00:00
|
|
|
""" Returns a string from the appropriate language file. """
|
|
|
|
return_str: str = eval("english." + search_str)
|
|
|
|
if LANGUAGE == LANG_DE:
|
|
|
|
return_str = eval("german." + search_str)
|
|
|
|
return return_str
|
|
|
|
|
2021-05-07 10:08:33 +00:00
|
|
|
def main_install_cli():
|
2021-05-10 09:29:40 +00:00
|
|
|
try:
|
|
|
|
language = input("Hello, first of all, which language do you prefer: German [DE] or English [EN]?\n> ")
|
|
|
|
if language.upper() == "DE":
|
|
|
|
set_language(LANG_DE)
|
|
|
|
print("Perfekt, nun ist das deutsche Sprachpaket aktiviert. Willkommen!\n")
|
|
|
|
else:
|
|
|
|
print("Perfect, the English language package is now activated. Welcome!.\n")
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
print("\n" + get_lang_text("intromsg1") + "\n")
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
print("\n" + get_lang_text("intromsg2") + "\n")
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# which Rsync options are available and which one you want to use
|
|
|
|
print(get_lang_text("rsyncopt") + "\n")
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# asks if you want to exclude files/directories from backup and creates an exclude file in case of Yes
|
|
|
|
exclude = input(get_lang_text("excludefile1") + "\n> ")
|
|
|
|
global EXCLUDE
|
|
|
|
if exclude.upper() in ("J", "Y"):
|
|
|
|
EXCLUDE = True
|
|
|
|
print(get_lang_text("excludefile2") + "\n")
|
|
|
|
else:
|
|
|
|
EXCLUDE = False
|
|
|
|
print(get_lang_text("excludefile3") + "\n")
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# Asks for the source directory which should be saved
|
|
|
|
print(get_lang_text("srcdir1"))
|
|
|
|
time.sleep(1)
|
|
|
|
sourcedir = input(get_lang_text("srcdir2") + "\n> ")
|
|
|
|
|
|
|
|
print(f"{get_lang_text('srcdir3_1')} {sourcedir} {get_lang_text('srcdir3_2')}")
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# asks for the destination directory in which the backup should be saved
|
|
|
|
targetdir = input(get_lang_text("targetdir1") + "\n> ")
|
|
|
|
print(f"{get_lang_text('targetdir2_1')} {targetdir} {get_lang_text('targetdir2_2')}")
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# collects all the information needed to execute the rsync command and creates it.
|
|
|
|
print(get_lang_text("collect") + "\n")
|
|
|
|
time.sleep(1)
|
|
|
|
exclude_file = os.path.join(MYDIR, EXCLUDE_FILE)
|
|
|
|
|
|
|
|
RSYNC_CMD = f"rsync -aqp --exclude-from={exclude_file} {sourcedir} {targetdir}"
|
|
|
|
|
|
|
|
print(f"{RSYNC_CMD}")
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# Outro
|
|
|
|
print(get_lang_text("outro1"))
|
|
|
|
time.sleep(2)
|
|
|
|
print(get_lang_text("outro2") + " " + EMAIL)
|
|
|
|
|
|
|
|
return True, EXCLUDE, RSYNC_CMD
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
trace("Programm interrupted by user.", prefix_crlf=True)
|
|
|
|
return False, None, None
|
2021-05-06 17:40:07 +00:00
|
|
|
|
2021-05-07 10:08:33 +00:00
|
|
|
|
2021-05-06 17:40:07 +00:00
|
|
|
def create_exclude_file(directory, exclude_file):
|
|
|
|
exclude_file = os.path.join(directory, exclude_file)
|
|
|
|
with open(exclude_file, "w") as fExclude:
|
|
|
|
trace(f"creating exclude-file '{exclude_file}'.")
|
|
|
|
fExclude.write("\n")
|
|
|
|
|
2021-05-07 10:08:33 +00:00
|
|
|
def create_alias(shell, home_dir, directory, backuppy_script):
|
2021-05-06 17:40:07 +00:00
|
|
|
# alias entry in .bashrc or .zshrc
|
|
|
|
backuppy_script = os.path.join(directory, backuppy_script)
|
|
|
|
alias_str = f"alias backuppy='sudo {backuppy_script}'"
|
2021-05-05 15:21:54 +00:00
|
|
|
|
2021-05-06 13:14:50 +00:00
|
|
|
# Check for installed ZSH
|
2021-05-07 10:08:33 +00:00
|
|
|
if shell.upper().find("ZSH") > 0:
|
|
|
|
rc_filepath = os.path.join(home_dir, ".zshrc")
|
2021-05-06 13:14:50 +00:00
|
|
|
# Check for installed BASH
|
2021-05-07 10:08:33 +00:00
|
|
|
if shell.upper().find("BASH") > 0:
|
|
|
|
rc_filepath = os.path.join(home_dir, ".bashrc")
|
2021-05-06 13:14:50 +00:00
|
|
|
# Append our alias if not already existing
|
2021-05-05 15:21:54 +00:00
|
|
|
if os.path.isfile(rc_filepath):
|
2021-05-06 13:14:50 +00:00
|
|
|
fileRc = open(rc_filepath, "r") # open file in read mode
|
|
|
|
backuppy_entry_exists = False
|
2021-05-06 17:40:07 +00:00
|
|
|
for line in fileRc:
|
2021-05-06 13:14:50 +00:00
|
|
|
if "alias backuppy=" in line:
|
|
|
|
backuppy_entry_exists = True
|
|
|
|
break
|
2021-05-06 17:40:07 +00:00
|
|
|
if not backuppy_entry_exists:
|
|
|
|
trace(f"Writing {alias_str} to config file '{rc_filepath}'.")
|
2021-05-06 13:14:50 +00:00
|
|
|
fileRc = open(rc_filepath, "a") # open file in append mode
|
2021-05-06 17:40:07 +00:00
|
|
|
fileRc.write("\n# Following line was created by Backuppy\n" + alias_str + "\n")
|
2021-05-06 13:14:50 +00:00
|
|
|
fileRc.close()
|
2021-05-05 15:21:54 +00:00
|
|
|
|
2021-05-06 17:40:07 +00:00
|
|
|
def create_backuppy_script(directory, backuppy_script, rsync_cmd):
|
|
|
|
# creates the file 'Backuppy.sh'
|
|
|
|
backuppy_file = os.path.join(directory, backuppy_script)
|
|
|
|
with open(backuppy_file, "w") as fBackuppy:
|
|
|
|
trace(f"creating backuppy-file '{backuppy_file}'.")
|
|
|
|
fBackuppy.write("#!/bin/bash\n" + rsync_cmd + "\n")
|
|
|
|
|
|
|
|
os.chmod(backuppy_file, 0o777) # make file executable
|
|
|
|
|
2021-05-07 10:08:33 +00:00
|
|
|
def do_the_install(is_exclude: bool, rsync_cmd: str):
|
|
|
|
""" Creates scripts and entries based on environment variables. """
|
2021-05-06 17:40:07 +00:00
|
|
|
|
2021-05-07 10:08:33 +00:00
|
|
|
if is_exclude:
|
|
|
|
create_exclude_file(MYDIR, EXCLUDE_FILE)
|
|
|
|
|
|
|
|
if rsync_cmd:
|
|
|
|
create_backuppy_script(MYDIR, BACKUPPY_SCRIPT, rsync_cmd)
|
|
|
|
create_alias(SHELL, HOME, MYDIR, BACKUPPY_SCRIPT)
|
2021-05-06 17:40:07 +00:00
|
|
|
|
|
|
|
def main(argv):
|
2021-05-05 15:21:54 +00:00
|
|
|
trace(f"Starting Backuppy install.py v{VERSION}")
|
2021-05-07 10:08:33 +00:00
|
|
|
is_finalized = False
|
2021-05-05 15:21:54 +00:00
|
|
|
|
2021-05-06 17:40:07 +00:00
|
|
|
if argv and argv[0] == "--gui":
|
2021-05-07 10:08:33 +00:00
|
|
|
from install_gui import main_install_gui
|
2021-05-07 11:16:13 +00:00
|
|
|
trace("Starting GUI-version.")
|
2021-05-07 10:08:33 +00:00
|
|
|
is_finalized, is_exclude, rsync_cmd = main_install_gui() # collect user input via GUI and store in env. variables
|
|
|
|
|
2021-05-06 17:40:07 +00:00
|
|
|
else:
|
|
|
|
trace("Starting CLI-version.\n")
|
2021-05-07 10:08:33 +00:00
|
|
|
is_finalized, is_exclude, rsync_cmd = main_install_cli() # collect user input via CLI and store in env. variables
|
|
|
|
|
|
|
|
if is_finalized:
|
|
|
|
do_the_install(is_exclude, rsync_cmd)
|
2021-05-05 15:21:54 +00:00
|
|
|
|
|
|
|
trace("Ending Backuppy install.py")
|
2021-05-06 17:40:07 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# sys.argv.append("--gui") # TODO: disable for production
|
|
|
|
sys.exit(main(sys.argv[1:]))
|