|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Python script that performs backups of the current directory. It creates a tar file |
| 4 | +# using find to select files less than a given size in bytes and up to a given recent |
| 5 | +# time period based on number of days. In addition, it has an option to create an |
| 6 | +# encrypted archive either via the zip utility. |
| 7 | +# |
| 8 | +# Note: |
| 9 | +# - Written by Tana A. given Bash snippets I use. |
| 10 | +# |
| 11 | + |
| 12 | +"""Simple program to make backups of a source based on several parameters""" |
| 13 | +import time |
| 14 | +import os |
| 15 | +import socket |
| 16 | +import shutil |
| 17 | +import logging |
| 18 | +import tarfile |
| 19 | +from datetime import date |
| 20 | +import py7zr |
| 21 | +import click |
| 22 | + |
| 23 | +# Local modules |
| 24 | +from mezcla import debug |
| 25 | +from mezcla import system |
| 26 | + |
| 27 | +# Environment constants |
| 28 | +HOME_DIR = system.getenv_text("HOME", "~", |
| 29 | + "User home directory") |
| 30 | +BACKUP_DIR = system.getenv_text("BACKUP_DIR", HOME_DIR, |
| 31 | + "Base directory for backups") |
| 32 | + |
| 33 | + |
| 34 | +def create_backup_folder(source): |
| 35 | + """Try to create the backup folder if it doesn't exist""" |
| 36 | + ## OLD: backup_dir = os.path.join(os.environ["HOME"], socket.gethostname()) |
| 37 | + backup_dir = os.path.join(BACKUP_DIR, socket.gethostname()) |
| 38 | + base_dir = source.split("/")[-2] |
| 39 | + try: |
| 40 | + os.makedirs(backup_dir, exist_ok=True) |
| 41 | + except OSError as err: |
| 42 | + print(err) |
| 43 | + total, used, free = tuple( |
| 44 | + number / 1073741824 for number in shutil.disk_usage(backup_dir) |
| 45 | + ) |
| 46 | + print(f"{backup_dir}: Total={total:1f} Used={used:.1f} Free={free:.1f} " |
| 47 | + f"{free/total*100:.1f}% of the total space)" |
| 48 | + ) |
| 49 | + logging.basicConfig( |
| 50 | + ## OLD: filename=f"{backup_dir}/_make-{base_dir}-incremental-backup-" |
| 51 | + filename=f"_make-{base_dir}-incremental-backup-" |
| 52 | + f'{date.today().strftime("%Y%m%d")}.log', |
| 53 | + filemode="w", |
| 54 | + encoding="utf-8", |
| 55 | + level=logging.DEBUG, |
| 56 | + ) |
| 57 | + logging.debug("Debug starts now") |
| 58 | + |
| 59 | + |
| 60 | +def backup_derive(source, max_days_old, max_size_chars): |
| 61 | + """Sets type of backup depending on modification time and file max_size_chars""" |
| 62 | + logging.info("Starts setting basename") |
| 63 | + |
| 64 | + # Backup source, equivalent to $HOME/$HOSTNAME |
| 65 | + ## OLD: backup_dir = os.path.join(os.environ["HOME"], socket.gethostname()) |
| 66 | + backup_dir = os.path.join(BACKUP_DIR, socket.gethostname()) |
| 67 | + |
| 68 | + hostname = socket.gethostname() ##Get's hostname |
| 69 | + base_dir = source.split("/")[-2] ##Backup folder name |
| 70 | + ## OLD: max_days_old=31 |
| 71 | + ## -or-: max_days_old=92; -or-: max_days_old=366; |
| 72 | + ## -or-: max_days_old=$(calc-int "5 * 365.25"); |
| 73 | + ## -or-: max_days_old=36525 ## (i.e., 100 years--no limit) |
| 74 | + ## OLD: max_size_chars=$(calc-int "5 * 1024**2") |
| 75 | + ## -or-: max_size_chars=131072 -or-: max_size_chars=1048577 |
| 76 | + ## -or-: max_size_chars=1000000000 -or-: max_size_chars=1099511627776 |
| 77 | + ##(i.e., 1TB--effectively no limit) |
| 78 | + ## TODO: max_days_old=$(calc-int "5 * 365.25"); max_size_chars=$(calc-int "10**9") |
| 79 | + # max_days_old = 5 * 365.25 |
| 80 | + # max_size_chars = 10 ** 9 |
| 81 | + if ( |
| 82 | + max_days_old > 36525 and max_size_chars > 1048576 |
| 83 | + ): ##If 100 years and 1TB--effectively no limit |
| 84 | + basename = f"{backup_dir}/full-{hostname}-{base_dir}-" |
| 85 | + elif max_days_old > 365.25 * 5 and max_size_chars > 1024: ##If 5 years and 1GB |
| 86 | + basename = f"{backup_dir}/fullish-{hostname}-{base_dir}-" |
| 87 | + else: |
| 88 | + basename = f"{backup_dir}/incr-{hostname}-{base_dir}-" |
| 89 | + basename = basename + date.today().strftime( |
| 90 | + "%Y%m%d" |
| 91 | + ) ##Adds date in YY-MM-DD format |
| 92 | + print(f"basename: {basename}") |
| 93 | + logging.info("basename:%s", basename) |
| 94 | + logging.info("Finish setting basename") |
| 95 | + return basename |
| 96 | + |
| 97 | + |
| 98 | +def sort_files(walkdir, size, days): |
| 99 | + """Walks inside selected path and sift files based on given parameters""" |
| 100 | + logging.info("Starts walking inside path, some errors are expected") |
| 101 | + max_days_old = days * 86400 # Time in days converted to seconds |
| 102 | + max_size_chars = size * 1048576 # max_size_chars in megabytes converted to bytes |
| 103 | + lista = [] # Initializes a new empty list |
| 104 | + # Walks inside every source and file in walkdir |
| 105 | + for root, _, files in os.walk(walkdir): |
| 106 | + for file in files: |
| 107 | + try: |
| 108 | + if ( |
| 109 | + os.stat(os.path.join(root, file)).st_size < max_size_chars |
| 110 | + and time.time() - os.path.getmtime(os.path.join(root, file)) |
| 111 | + < max_days_old |
| 112 | + ): ##If size is less than max size and time (in epoch seconds) less than max days |
| 113 | + lista.append(os.path.join(root, file)) |
| 114 | + except OSError as err: |
| 115 | + logging.warning(err) |
| 116 | + pass |
| 117 | + logging.debug("list: %s", lista) |
| 118 | + logging.info("Finish walking inside path") |
| 119 | + return lista |
| 120 | + |
| 121 | + |
| 122 | +def create_tar(basename, lista): |
| 123 | + """Creates a simple 7z file and writes files on it""" |
| 124 | + logging.info("Starts creating tar.gz file") |
| 125 | + with tarfile.open(basename + ".tar.gz", "w:gz") as archive: |
| 126 | + for file in lista: |
| 127 | + try: |
| 128 | + archive.add(file) |
| 129 | + except OSError as err: ##Broken links usually gives errors at this point |
| 130 | + logging.warning(err) |
| 131 | + pass |
| 132 | + except Exception as err: # pylint: disable=broad-except |
| 133 | + logging.critical(err) |
| 134 | + print(err) |
| 135 | + logging.info("Finish creating tar.gz file") |
| 136 | + |
| 137 | + |
| 138 | +def create_encrypted_7z(password, basename, lista): |
| 139 | + """Creates a encrypted 7z file and writes files on it""" |
| 140 | + logging.info("Starts creating encrypted 7z file") |
| 141 | + with py7zr.SevenZipFile(basename + ".7z", "w", password=password) as archive: |
| 142 | + for file in lista: |
| 143 | + try: |
| 144 | + archive.write(file) |
| 145 | + except OSError as err: |
| 146 | + logging.warning(err) |
| 147 | + pass |
| 148 | + except Exception as err: # pylint: disable=broad-except |
| 149 | + logging.critical(err) |
| 150 | + ## OLD: print("Unable to continue. Please see log") |
| 151 | + system.print_error("Unable to continue:\n\t{err}") |
| 152 | + logging.info("Finish creating encrypted 7z file") |
| 153 | + |
| 154 | + |
| 155 | +@click.command() |
| 156 | +@click.option( |
| 157 | + "--password", |
| 158 | + prompt=True, |
| 159 | + hide_input=True, |
| 160 | + default="", |
| 161 | + confirmation_prompt=True, |
| 162 | + help="Blank for no encryptation", |
| 163 | +) |
| 164 | +@click.option("-S", "--source", default=os.getcwd, help="Alternative source") |
| 165 | +@click.option("-f", "--full", is_flag=True, help="Full backup. Overrides size and days") |
| 166 | +@click.option("-d", "--days", required=True, type=int, help="Max days since modification") |
| 167 | +@click.option("-s", "--size", required=True, type=int, help="Max size in MB") |
| 168 | +def main(password, source, full, days, size): |
| 169 | + """Main function""" |
| 170 | + create_backup_folder(source) |
| 171 | + logging.info("Checking --full flag") |
| 172 | + if full: |
| 173 | + logging.info("Changing days and size to infinite (--full)") |
| 174 | + days = size = float("inf") |
| 175 | + basename = backup_derive(source, days, size) |
| 176 | + lista = sort_files(source, days, size) |
| 177 | + logging.info("Testing password") |
| 178 | + if password: |
| 179 | + logging.info("Password given") |
| 180 | + create_encrypted_7z(password, basename, lista) |
| 181 | + else: |
| 182 | + logging.info("Password not given") |
| 183 | + create_tar(basename, lista) |
| 184 | + |
| 185 | + |
| 186 | +if __name__ == "__main__": |
| 187 | + debug.trace_current_context(level=debug.QUITE_DETAILED) |
| 188 | + debug.trace_fmt(4, "Environment options: {eo}", |
| 189 | + eo=system.formatted_environment_option_descriptions()) |
| 190 | + # pylint: disable=no-value-for-parameter |
| 191 | + main() |
0 commit comments