2021-05-05 15:21:54 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
project: Backuppy
|
2021-05-06 17:40:07 +00:00
|
|
|
version: 0.7
|
2021-05-05 15:21:54 +00:00
|
|
|
file: install.py
|
|
|
|
summary: main entry python file
|
|
|
|
"""
|
|
|
|
|
|
|
|
# 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
|
|
|
# ----------------------
|
|
|
|
VERSION: str = "0.7"
|
|
|
|
# ----------------------
|
|
|
|
MYDIR = os.getcwd()
|
|
|
|
EXCLUDE_FILE = "exclude.txt"
|
|
|
|
BACKUPPY_SCRIPT = "Backuppy.sh"
|
|
|
|
SHELL = os.environ.get("SHELL")
|
|
|
|
HOME = os.environ.get("HOME")
|
2021-05-05 15:21:54 +00:00
|
|
|
LANG_EN: str = "English"
|
|
|
|
LANG_DE: str = "German"
|
2021-05-06 17:40:07 +00:00
|
|
|
LANGUAGE: str = None
|
2021-05-05 15:21:54 +00:00
|
|
|
RSYNC_CMD: str = None
|
2021-05-06 17:40:07 +00:00
|
|
|
EMAIL: str = "fotocoder@joschu.ch"
|
|
|
|
|
|
|
|
def set_language(language):
|
|
|
|
global LANGUAGE
|
|
|
|
LANGUAGE = language
|
|
|
|
|
|
|
|
def trace(message_txt):
|
|
|
|
""" Print a formatted message to std out. """
|
|
|
|
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-06 17:40:07 +00:00
|
|
|
def install_cli_main():
|
|
|
|
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> ")
|
|
|
|
if exclude.upper() in ("J", "Y"):
|
|
|
|
print(get_lang_text("excludefile2") + "\n")
|
|
|
|
os.environ["BUPY_CREATE_EXCLUDE"] = "True"
|
|
|
|
else:
|
|
|
|
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}"
|
|
|
|
os.environ["BUPY_RSYNC_CMD"] = RSYNC_CMD
|
|
|
|
print(f"{RSYNC_CMD}")
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# Outro
|
|
|
|
print(get_lang_text("outro1"))
|
|
|
|
time.sleep(2)
|
|
|
|
print(get_lang_text("outro2") + " " + EMAIL)
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
return exclude_file
|
|
|
|
|
|
|
|
def create_alias(directory, backuppy_script):
|
|
|
|
# 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-06 17:40:07 +00:00
|
|
|
if SHELL.upper().find("ZSH") > 0:
|
|
|
|
rc_filepath = os.path.join(HOME, ".zshrc")
|
2021-05-06 13:14:50 +00:00
|
|
|
# Check for installed BASH
|
2021-05-06 17:40:07 +00:00
|
|
|
if SHELL.upper().find("BASH") > 0:
|
|
|
|
rc_filepath = os.path.join(HOME, ".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
|
|
|
|
|
|
|
|
def do_the_install():
|
|
|
|
""" Does the things with our environment variables. """
|
|
|
|
|
|
|
|
is_exclude = os.environ.get("BUPY_CREATE_EXCLUDE")
|
|
|
|
rsync_cmd = os.environ.get("BUPY_RSYNC_CMD")
|
|
|
|
|
|
|
|
if is_exclude == "True":
|
|
|
|
exclude_file = create_exclude_file(MYDIR, EXCLUDE_FILE)
|
|
|
|
create_alias(MYDIR, BACKUPPY_SCRIPT)
|
|
|
|
create_backuppy_script(MYDIR, BACKUPPY_SCRIPT, rsync_cmd)
|
|
|
|
|
|
|
|
def main(argv):
|
2021-05-05 15:21:54 +00:00
|
|
|
trace(f"Starting Backuppy install.py v{VERSION}")
|
|
|
|
|
2021-05-06 17:40:07 +00:00
|
|
|
if argv and argv[0] == "--gui":
|
|
|
|
import install_gui
|
|
|
|
trace("Starting GUI-version.\n")
|
|
|
|
install_gui.main() # collect user input via GUI and store in env. variables
|
|
|
|
else:
|
|
|
|
trace("Starting CLI-version.\n")
|
|
|
|
install_cli_main() # collect user input via CLI and store in env. variables
|
|
|
|
|
|
|
|
do_the_install()
|
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:]))
|