2021-05-05 15:21:54 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
project: Backuppy
|
2021-05-10 16:49:30 +00:00
|
|
|
version: 0.9
|
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
|
|
|
|
|
|
|
|
# local imports
|
2021-05-10 16:49:30 +00:00
|
|
|
from utils import set_language, query, _print, trace
|
2021-05-05 15:21:54 +00:00
|
|
|
|
|
|
|
# local globals
|
2021-05-10 16:49:30 +00:00
|
|
|
VERSION: str = "0.9"
|
2021-05-07 10:08:33 +00:00
|
|
|
EMAIL = "fotocoder@joschu.ch"
|
2021-05-06 17:40:07 +00:00
|
|
|
EXCLUDE_FILE = "exclude.txt"
|
|
|
|
BACKUPPY_SCRIPT = "Backuppy.sh"
|
2021-05-10 16:49:30 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
# 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("srcdir2")
|
|
|
|
if not sourcedir:
|
2021-05-10 09:29:40 +00:00
|
|
|
return False, None, None
|
2021-05-06 17:40:07 +00:00
|
|
|
|
2021-05-10 16:49:30 +00:00
|
|
|
_print("srcdir3_1")
|
|
|
|
print(sourcedir)
|
|
|
|
_print("srcdir3_2")
|
|
|
|
|
|
|
|
# asks for the destination directory in which the backup should be saved
|
|
|
|
targetdir = query("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")
|
|
|
|
|
|
|
|
rsync_cmd = f"rsync -aqp --exclude-from={os.path.join(mydir, exclude_file)} {sourcedir} {targetdir}"
|
|
|
|
|
|
|
|
print(f"{rsync_cmd}")
|
|
|
|
|
|
|
|
# Outro
|
|
|
|
_print("outro1")
|
|
|
|
|
|
|
|
_print("outro2")
|
|
|
|
print(EMAIL)
|
|
|
|
|
|
|
|
return True, exclude, rsync_cmd
|
2021-05-07 10:08:33 +00:00
|
|
|
|
2021-05-10 16:49:30 +00:00
|
|
|
def create_exclude_file(mydir, exclude_file):
|
|
|
|
exclude_file = os.path.join(mydir, exclude_file)
|
2021-05-06 17:40:07 +00:00
|
|
|
with open(exclude_file, "w") as fExclude:
|
|
|
|
trace(f"creating exclude-file '{exclude_file}'.")
|
|
|
|
fExclude.write("\n")
|
|
|
|
|
2021-05-10 16:49:30 +00:00
|
|
|
def create_alias(mydir, backuppy_script):
|
|
|
|
shell = os.environ.get("SHELL")
|
|
|
|
home_dir = os.environ.get("HOME")
|
2021-05-06 17:40:07 +00:00
|
|
|
# alias entry in .bashrc or .zshrc
|
2021-05-10 16:49:30 +00:00
|
|
|
backuppy_script = os.path.join(mydir, backuppy_script)
|
2021-05-06 17:40:07 +00:00
|
|
|
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-10 16:49:30 +00:00
|
|
|
def do_the_install(mydir: str, exclude_file, is_exclude: bool, backuppy_script: str, rsync_cmd: str):
|
2021-05-07 10:08:33 +00:00
|
|
|
""" 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:
|
2021-05-10 16:49:30 +00:00
|
|
|
create_exclude_file(mydir, exclude_file)
|
2021-05-07 10:08:33 +00:00
|
|
|
|
|
|
|
if rsync_cmd:
|
2021-05-10 16:49:30 +00:00
|
|
|
create_backuppy_script(mydir, backuppy_script, rsync_cmd)
|
|
|
|
create_alias(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-10 16:49:30 +00:00
|
|
|
mydir = os.getcwd()
|
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-10 16:49:30 +00:00
|
|
|
is_finalized, is_exclude, rsync_cmd = main_install_gui(mydir, EXCLUDE_FILE) # collect user input via GUI and store in env. variables
|
2021-05-07 10:08:33 +00:00
|
|
|
|
2021-05-06 17:40:07 +00:00
|
|
|
else:
|
|
|
|
trace("Starting CLI-version.\n")
|
2021-05-10 16:49:30 +00:00
|
|
|
is_finalized, is_exclude, rsync_cmd = main_install_cli(mydir, EXCLUDE_FILE) # collect user input via CLI and store in env. variables
|
2021-05-07 10:08:33 +00:00
|
|
|
|
|
|
|
if is_finalized:
|
2021-05-10 16:49:30 +00:00
|
|
|
do_the_install(mydir, EXCLUDE_FILE, is_exclude, BACKUPPY_SCRIPT, 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__':
|
2021-05-10 16:53:34 +00:00
|
|
|
# sys.argv.append("--gui") # TODO: disable for production
|
2021-05-06 17:40:07 +00:00
|
|
|
sys.exit(main(sys.argv[1:]))
|