#!/usr/bin/env python3 """ project: Backuppy version: 0.10 file: install.py summary: python installer-script in CLI-mode """ # Standard library imports import sys import os # local imports from utils import set_language, query, query_path, _print, trace, paths_are_identical # local globals VERSION: str = "0.10" EMAIL = "fotocoder@joschu.ch" EXCLUDE_FILE = "exclude.txt" BACKUPPY_SCRIPT = "Backuppy.sh" def main_install_cli(mydir, exclude_file): language = query("welcome") if not language: return False, None, None set_language(language) _print("languagepack") _print("intromsg1") _print("intromsg2") # which Rsync options are available and which one you want to use _print("rsyncopt", wait=2) # asks if you want to exclude files/directories from backup and creates an exclude file in case of Yes exclude = query("excludefile1") if not exclude: return False, None, None elif exclude.upper() in ("J", "Y"): _print("excludefile2") exclude = True else: _print("excludefile3") exclude = False # Asks for the source directory which should be saved _print("srcdir1") sourcedir = query_path("srcdir2") if not sourcedir: return False, None, None #_print("srcdir3_1") #print(sourcedir) _print("srcdir3_2") # asks for the destination directory in which the backup should be saved targetdir = query_path("targetdir1") while paths_are_identical(sourcedir, targetdir): _print("paths_must_differ") targetdir = query_path("targetdir1") if not targetdir: return False, None, None #_print("targetdir2_1") #print(targetdir) _print("targetdir2_2") # collects all the information needed to execute the rsync command and creates it. _print("collect", wait=2) rsync_cmd = f"rsync -aqp --exclude-from={os.path.join(mydir, exclude_file)} {sourcedir} {targetdir}" print(f"{rsync_cmd}") # Outro _print("outro1", wait=2) _print("outro2") print(EMAIL) return True, exclude, rsync_cmd def create_exclude_file(mydir, exclude_file): exclude_file = os.path.join(mydir, exclude_file) with open(exclude_file, "w") as fExclude: trace(f"creating exclude-file '{exclude_file}'.") fExclude.write("\n") def create_alias(mydir, backuppy_script): shell = os.environ.get("SHELL") home_dir = os.environ.get("HOME") # alias entry in .bashrc or .zshrc backuppy_script = os.path.join(mydir, backuppy_script) alias_str = f"alias backuppy='sudo {backuppy_script}'" # Check for installed ZSH if shell.upper().find("ZSH") > 0: rc_filepath = os.path.join(home_dir, ".zshrc") # Check for installed BASH if shell.upper().find("BASH") > 0: rc_filepath = os.path.join(home_dir, ".bashrc") # Append our alias if not already existing if os.path.isfile(rc_filepath): fileRc = open(rc_filepath, "r") # open file in read mode backuppy_entry_exists = False for line in fileRc: if "alias backuppy=" in line: backuppy_entry_exists = True break if not backuppy_entry_exists: trace(f"Writing {alias_str} to config file '{rc_filepath}'.") fileRc = open(rc_filepath, "a") # open file in append mode fileRc.write("\n# Following line was created by Backuppy\n" + alias_str + "\n") fileRc.close() 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(mydir: str, exclude_file, is_exclude: bool, backuppy_script: str, rsync_cmd: str): """ Creates scripts and entries based on environment variables. """ if is_exclude: create_exclude_file(mydir, exclude_file) if rsync_cmd: create_backuppy_script(mydir, backuppy_script, rsync_cmd) create_alias(mydir, backuppy_script) def main(argv): trace(f"Starting Backuppy install.py v{VERSION}") is_finalized = False mydir = os.getcwd() if argv and argv[0] == "--gui": from install_gui import main_install_gui trace("Starting GUI-version.") is_finalized, is_exclude, rsync_cmd = main_install_gui(mydir, EXCLUDE_FILE) # collect user input via GUI and store in env. variables else: trace("Starting CLI-version.\n") is_finalized, is_exclude, rsync_cmd = main_install_cli(mydir, EXCLUDE_FILE) # collect user input via CLI and store in env. variables if is_finalized: do_the_install(mydir, EXCLUDE_FILE, is_exclude, BACKUPPY_SCRIPT, rsync_cmd) trace("Ending Backuppy install.py") if __name__ == '__main__': # sys.argv.append("--gui") # TODO: disable for production sys.exit(main(sys.argv[1:]))