From a84aa217f6e86763351e458bfe6c36314596abce Mon Sep 17 00:00:00 2001 From: Cyber-Syntax <115875369+Cyber-Syntax@users.noreply.github.com> Date: Thu, 2 Jan 2025 11:14:46 +0300 Subject: [PATCH 1/6] style: PEP8 fixes --- ignore.txt | 1 - main.py | 137 +++++++++++++++++++++++++++++++++-------------- requirements.txt | 0 3 files changed, 96 insertions(+), 42 deletions(-) delete mode 100644 requirements.txt diff --git a/ignore.txt b/ignore.txt index c328f24..34845df 100644 --- a/ignore.txt +++ b/ignore.txt @@ -2,5 +2,4 @@ Documents/appimages Documents/screenloyout Documents/backup-for-cloud Documents/linuxISO -Documents/personal Documents/test diff --git a/main.py b/main.py index db70b52..825c8c8 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ """ This script will backup with tar command and encrypt, decrypt with openssl command""" + import os import subprocess import hashlib @@ -6,37 +7,41 @@ import sys import time + class BackupManager: - """ This class will backup the directories listed in dirs_to_backup.txt to a compressed file """ + """This class will backup the directories listed in dirs_to_backup.txt to a compressed file""" def __init__(self): self.backup_folder = os.path.expanduser("~/Documents/backup-for-cloud/") - self.dirs_file_path = 'dirs_to_backup.txt' + self.dirs_file_path = "dirs_to_backup.txt" self.current_date = datetime.datetime.now().strftime("%d-%m-%Y") self.backup_file_path = os.path.expanduser( - f"{self.backup_folder}/{self.current_date}.tar.xz") - self.ignore_file_path = os.path.expanduser('ignore.txt') + f"{self.backup_folder}/{self.current_date}.tar.xz" + ) + self.ignore_file_path = os.path.expanduser("ignore.txt") # check backup already exist for today def check_backup_exist(self): - """ Check if a backup file already exists for today """ + """Check if a backup file already exists for today""" return os.path.isfile(self.backup_file_path) def backup_directories(self): - """ Backup the directories listed in dirs_to_backup.txt to a compressed file """ + """Backup the directories listed in dirs_to_backup.txt to a compressed file""" # Read in the directories to backup from the file dirs_to_backup = [] - with open(self.dirs_file_path, 'r', encoding='utf-8') as file: + with open(self.dirs_file_path, "r", encoding="utf-8") as file: for line in file: directory = line.strip() if directory: dirs_to_backup.append(directory) # Exclude files and directories listed in the ignore file - exclude_option = (f"--exclude-from={self.ignore_file_path}" - if os.path.isfile(self.ignore_file_path) - else f"--exclude={self.ignore_file_path}") + exclude_option = ( + f"--exclude-from={self.ignore_file_path}" + if os.path.isfile(self.ignore_file_path) + else f"--exclude={self.ignore_file_path}" + ) # Only backup files on the same filesystem as the backup folder filesystem_option = "--one-file-system" @@ -45,10 +50,11 @@ def backup_directories(self): dir_paths = [os.path.expanduser(path) for path in dirs_to_backup] # Create the tar command with tqdm progress bar - total_size = sum(entry.stat().st_size - for path in dir_paths - for entry in os.scandir(path) - if entry.is_file() + total_size = sum( + entry.stat().st_size + for path in dir_paths + for entry in os.scandir(path) + if entry.is_file() ) print(f"Total size: {total_size} bytes") @@ -69,8 +75,13 @@ def backup_directories(self): # Run the tar command try: subprocess.run(os_cmd, check=True, shell=True) - except (subprocess.CalledProcessError, FileNotFoundError, PermissionError, OSError, - ValueError) as error: + except ( + subprocess.CalledProcessError, + FileNotFoundError, + PermissionError, + OSError, + ValueError, + ) as error: print(f"Error backing up files: {type(error).__name__} - {error}") return False except KeyboardInterrupt: @@ -80,29 +91,50 @@ def backup_directories(self): print("Backup completed successfully") return True + # EncryptionManager inherits from BackupManager class EncryptionManager(BackupManager): - """ This script will encrypt the backup file with openssl command""" + """This script will encrypt the backup file with openssl command""" + def __init__(self): # Call the __init__() method of the parent class super().__init__() - self.decrypt_file_path = os.path.expanduser("~/Documents/backup-for-cloud/decrypted.tar.xz") + self.decrypt_file_path = os.path.expanduser( + "~/Documents/backup-for-cloud/decrypted.tar.xz" + ) def encrypt_backup(self): - """ Encrypt the backup file with openssl command""" + """Encrypt the backup file with openssl command""" # The encrypted backup file will be named with the current date - file_to_encrypt = os.path.join(self.backup_folder, f"{self.current_date}.tar.xz.enc") + file_to_encrypt = os.path.join( + self.backup_folder, f"{self.current_date}.tar.xz.enc" + ) # Encrypt the backed up file with openssl command - encrypt_cmd = ["openssl", "aes-256-cbc", "-a", "-salt", "-pbkdf2", "-in", - self.backup_file_path, "-out", file_to_encrypt] + encrypt_cmd = [ + "openssl", + "aes-256-cbc", + "-a", + "-salt", + "-pbkdf2", + "-in", + self.backup_file_path, + "-out", + file_to_encrypt, + ] try: # ask user for password, when user enter password, encrypt file subprocess.run(encrypt_cmd, check=True, input="password", encoding="ascii") - except (subprocess.CalledProcessError, FileNotFoundError, PermissionError, - OSError, subprocess.TimeoutExpired, ValueError) as error: + except ( + subprocess.CalledProcessError, + FileNotFoundError, + PermissionError, + OSError, + subprocess.TimeoutExpired, + ValueError, + ) as error: print(f"Error encrypting file: {type(error).__name__} - {error}") return False except KeyboardInterrupt: @@ -114,19 +146,35 @@ def encrypt_backup(self): return True def decrypt(self, file_to_decrypt): - """ Decrypt the backup file """ + """Decrypt the backup file""" # Decrypt the backup file with openssl command - decrypt_cmd = ["openssl", "aes-256-cbc", "-d", "-a", "-salt", - "-pbkdf2", "-in", file_to_decrypt, "-out", self.decrypt_file_path] + decrypt_cmd = [ + "openssl", + "aes-256-cbc", + "-d", + "-a", + "-salt", + "-pbkdf2", + "-in", + file_to_decrypt, + "-out", + self.decrypt_file_path, + ] try: # ask user for password subprocess.run(decrypt_cmd, check=True, input="password", encoding="ascii") # Wait for the file to be decrypted time.sleep(1) - except (subprocess.CalledProcessError, FileNotFoundError, PermissionError, OSError, - subprocess.TimeoutExpired,ValueError) as error: + except ( + subprocess.CalledProcessError, + FileNotFoundError, + PermissionError, + OSError, + subprocess.TimeoutExpired, + ValueError, + ) as error: print(f"Error decrypting file: {type(error).__name__} - {error}") return False except KeyboardInterrupt: @@ -138,14 +186,14 @@ def decrypt(self, file_to_decrypt): return True def verify_decrypt_file(self, file_to_decrypt): - """ Verify the decrypted file same with original file """ + """Verify the decrypted file same with original file""" # remove .enc from file_to_decrypt original_file_path = file_to_decrypt[:-4] # Compute the SHA256 checksum of the *decrypted file* hasher = hashlib.sha256() - with open(self.decrypt_file_path, 'rb') as f: + with open(self.decrypt_file_path, "rb") as f: while True: data = f.read(65536) if not data: @@ -155,7 +203,7 @@ def verify_decrypt_file(self, file_to_decrypt): # Compute the SHA256 checksum of the *original file* hasher_orginal = hashlib.sha256() - with open(original_file_path, 'rb') as f: + with open(original_file_path, "rb") as f: while True: data = f.read(65536) if not data: @@ -165,12 +213,13 @@ def verify_decrypt_file(self, file_to_decrypt): # Compare the checksums if actual_checksum == expected_checksum: - print('File integrity verified: checksums match') + print("File integrity verified: checksums match") else: - print('File integrity check failed: checksums do not match') + print("File integrity check failed: checksums do not match") + def main(): - """ Backup the directories listed in dirs_to_backup.txt to a compressed file """ + """Backup the directories listed in dirs_to_backup.txt to a compressed file""" # Classes backup_manager = BackupManager() @@ -188,8 +237,7 @@ def main(): print("=====================================") try: choice = int(input("Enter your choice: ")) - except (ValueError, TypeError, NameError, AttributeError, - IndexError) as error: + except (ValueError, TypeError, NameError, AttributeError, IndexError) as error: print(f"Error: {type(error).__name__} - {error}") print("Please enter a number between 1 and 4.") continue @@ -200,7 +248,7 @@ def main(): if choice == 1: # Check if a backup file already exists for today if backup_manager.check_backup_exist(): - print('Backup already exists for today') + print("Backup already exists for today") continue # Backup the directories listed in dirs_to_backup.txt to a compressed file backup_manager.backup_directories() @@ -211,8 +259,12 @@ def main(): # List all encrypted files print("=====================================") print("Choose which file to decrypt: ") - # List only encrypted files - files = [f for f in os.listdir(backup_manager.backup_folder) if f.endswith('.enc')] + # List only encrypted files + files = [ + f + for f in os.listdir(backup_manager.backup_folder) + if f.endswith(".enc") + ] for i, file in enumerate(files, start=1): print(f"{i}. {file}") @@ -220,7 +272,9 @@ def main(): choice = int(input("Enter your choice: ")) # files[choice - 1] -> get file name from list files - file_to_decrypt = os.path.join(backup_manager.backup_folder, files[choice - 1]) + file_to_decrypt = os.path.join( + backup_manager.backup_folder, files[choice - 1] + ) encryption_manager.decrypt(file_to_decrypt) encryption_manager.verify_decrypt_file(file_to_decrypt) elif choice == 4: @@ -230,5 +284,6 @@ def main(): print("Invalid choice. Please enter a number between 1 and 4.") return + if __name__ == "__main__": main() diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index e69de29..0000000 From 97d7aa33f6d3a8d2212054f9dd853f9a9896b97b Mon Sep 17 00:00:00 2001 From: Cyber-Syntax <115875369+Cyber-Syntax@users.noreply.github.com> Date: Thu, 2 Jan 2025 11:16:19 +0300 Subject: [PATCH 2/6] chore: delete .venv --- .gitignore | 3 +- .venv-nixos/bin/Activate.ps1 | 247 ---------------------------------- .venv-nixos/bin/activate | 63 --------- .venv-nixos/bin/activate.csh | 26 ---- .venv-nixos/bin/activate.fish | 69 ---------- .venv-nixos/bin/pip | 8 -- .venv-nixos/bin/pip3 | 8 -- .venv-nixos/bin/pip3.11 | 8 -- .venv-nixos/bin/python | 1 - .venv-nixos/bin/python3 | 1 - .venv-nixos/bin/python3.11 | 1 - .venv-nixos/bin/tqdm | 8 -- .venv-nixos/lib64 | 1 - .venv-nixos/pyvenv.cfg | 5 - 14 files changed, 2 insertions(+), 447 deletions(-) delete mode 100644 .venv-nixos/bin/Activate.ps1 delete mode 100644 .venv-nixos/bin/activate delete mode 100644 .venv-nixos/bin/activate.csh delete mode 100644 .venv-nixos/bin/activate.fish delete mode 100755 .venv-nixos/bin/pip delete mode 100755 .venv-nixos/bin/pip3 delete mode 100755 .venv-nixos/bin/pip3.11 delete mode 120000 .venv-nixos/bin/python delete mode 120000 .venv-nixos/bin/python3 delete mode 120000 .venv-nixos/bin/python3.11 delete mode 100755 .venv-nixos/bin/tqdm delete mode 120000 .venv-nixos/lib64 delete mode 100644 .venv-nixos/pyvenv.cfg diff --git a/.gitignore b/.gitignore index 2d0c589..1806045 100644 --- a/.gitignore +++ b/.gitignore @@ -104,6 +104,7 @@ celerybeat.pid # Environments .env .venv +.venv* env/ venv/ ENV/ @@ -135,4 +136,4 @@ dmypy.json .vscode/ # dirs_to_backup -dirs_to_backup* \ No newline at end of file +dirs_to_backup* diff --git a/.venv-nixos/bin/Activate.ps1 b/.venv-nixos/bin/Activate.ps1 deleted file mode 100644 index b49d77b..0000000 --- a/.venv-nixos/bin/Activate.ps1 +++ /dev/null @@ -1,247 +0,0 @@ -<# -.Synopsis -Activate a Python virtual environment for the current PowerShell session. - -.Description -Pushes the python executable for a virtual environment to the front of the -$Env:PATH environment variable and sets the prompt to signify that you are -in a Python virtual environment. Makes use of the command line switches as -well as the `pyvenv.cfg` file values present in the virtual environment. - -.Parameter VenvDir -Path to the directory that contains the virtual environment to activate. The -default value for this is the parent of the directory that the Activate.ps1 -script is located within. - -.Parameter Prompt -The prompt prefix to display when this virtual environment is activated. By -default, this prompt is the name of the virtual environment folder (VenvDir) -surrounded by parentheses and followed by a single space (ie. '(.venv) '). - -.Example -Activate.ps1 -Activates the Python virtual environment that contains the Activate.ps1 script. - -.Example -Activate.ps1 -Verbose -Activates the Python virtual environment that contains the Activate.ps1 script, -and shows extra information about the activation as it executes. - -.Example -Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv -Activates the Python virtual environment located in the specified location. - -.Example -Activate.ps1 -Prompt "MyPython" -Activates the Python virtual environment that contains the Activate.ps1 script, -and prefixes the current prompt with the specified string (surrounded in -parentheses) while the virtual environment is active. - -.Notes -On Windows, it may be required to enable this Activate.ps1 script by setting the -execution policy for the user. You can do this by issuing the following PowerShell -command: - -PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser - -For more information on Execution Policies: -https://go.microsoft.com/fwlink/?LinkID=135170 - -#> -Param( - [Parameter(Mandatory = $false)] - [String] - $VenvDir, - [Parameter(Mandatory = $false)] - [String] - $Prompt -) - -<# Function declarations --------------------------------------------------- #> - -<# -.Synopsis -Remove all shell session elements added by the Activate script, including the -addition of the virtual environment's Python executable from the beginning of -the PATH variable. - -.Parameter NonDestructive -If present, do not remove this function from the global namespace for the -session. - -#> -function global:deactivate ([switch]$NonDestructive) { - # Revert to original values - - # The prior prompt: - if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) { - Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt - Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT - } - - # The prior PYTHONHOME: - if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) { - Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME - Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME - } - - # The prior PATH: - if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) { - Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH - Remove-Item -Path Env:_OLD_VIRTUAL_PATH - } - - # Just remove the VIRTUAL_ENV altogether: - if (Test-Path -Path Env:VIRTUAL_ENV) { - Remove-Item -Path env:VIRTUAL_ENV - } - - # Just remove VIRTUAL_ENV_PROMPT altogether. - if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) { - Remove-Item -Path env:VIRTUAL_ENV_PROMPT - } - - # Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether: - if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) { - Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force - } - - # Leave deactivate function in the global namespace if requested: - if (-not $NonDestructive) { - Remove-Item -Path function:deactivate - } -} - -<# -.Description -Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the -given folder, and returns them in a map. - -For each line in the pyvenv.cfg file, if that line can be parsed into exactly -two strings separated by `=` (with any amount of whitespace surrounding the =) -then it is considered a `key = value` line. The left hand string is the key, -the right hand is the value. - -If the value starts with a `'` or a `"` then the first and last character is -stripped from the value before being captured. - -.Parameter ConfigDir -Path to the directory that contains the `pyvenv.cfg` file. -#> -function Get-PyVenvConfig( - [String] - $ConfigDir -) { - Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg" - - # Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue). - $pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue - - # An empty map will be returned if no config file is found. - $pyvenvConfig = @{ } - - if ($pyvenvConfigPath) { - - Write-Verbose "File exists, parse `key = value` lines" - $pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath - - $pyvenvConfigContent | ForEach-Object { - $keyval = $PSItem -split "\s*=\s*", 2 - if ($keyval[0] -and $keyval[1]) { - $val = $keyval[1] - - # Remove extraneous quotations around a string value. - if ("'""".Contains($val.Substring(0, 1))) { - $val = $val.Substring(1, $val.Length - 2) - } - - $pyvenvConfig[$keyval[0]] = $val - Write-Verbose "Adding Key: '$($keyval[0])'='$val'" - } - } - } - return $pyvenvConfig -} - - -<# Begin Activate script --------------------------------------------------- #> - -# Determine the containing directory of this script -$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition -$VenvExecDir = Get-Item -Path $VenvExecPath - -Write-Verbose "Activation script is located in path: '$VenvExecPath'" -Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)" -Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)" - -# Set values required in priority: CmdLine, ConfigFile, Default -# First, get the location of the virtual environment, it might not be -# VenvExecDir if specified on the command line. -if ($VenvDir) { - Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values" -} -else { - Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir." - $VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/") - Write-Verbose "VenvDir=$VenvDir" -} - -# Next, read the `pyvenv.cfg` file to determine any required value such -# as `prompt`. -$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir - -# Next, set the prompt from the command line, or the config file, or -# just use the name of the virtual environment folder. -if ($Prompt) { - Write-Verbose "Prompt specified as argument, using '$Prompt'" -} -else { - Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value" - if ($pyvenvCfg -and $pyvenvCfg['prompt']) { - Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'" - $Prompt = $pyvenvCfg['prompt']; - } - else { - Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)" - Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'" - $Prompt = Split-Path -Path $venvDir -Leaf - } -} - -Write-Verbose "Prompt = '$Prompt'" -Write-Verbose "VenvDir='$VenvDir'" - -# Deactivate any currently active virtual environment, but leave the -# deactivate function in place. -deactivate -nondestructive - -# Now set the environment variable VIRTUAL_ENV, used by many tools to determine -# that there is an activated venv. -$env:VIRTUAL_ENV = $VenvDir - -if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) { - - Write-Verbose "Setting prompt to '$Prompt'" - - # Set the prompt to include the env name - # Make sure _OLD_VIRTUAL_PROMPT is global - function global:_OLD_VIRTUAL_PROMPT { "" } - Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT - New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt - - function global:prompt { - Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) " - _OLD_VIRTUAL_PROMPT - } - $env:VIRTUAL_ENV_PROMPT = $Prompt -} - -# Clear PYTHONHOME -if (Test-Path -Path Env:PYTHONHOME) { - Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME - Remove-Item -Path Env:PYTHONHOME -} - -# Add the venv to the PATH -Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH -$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH" diff --git a/.venv-nixos/bin/activate b/.venv-nixos/bin/activate deleted file mode 100644 index 578052d..0000000 --- a/.venv-nixos/bin/activate +++ /dev/null @@ -1,63 +0,0 @@ -# This file must be used with "source bin/activate" *from bash* -# you cannot run it directly - -deactivate () { - # reset old environment variables - if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then - PATH="${_OLD_VIRTUAL_PATH:-}" - export PATH - unset _OLD_VIRTUAL_PATH - fi - if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then - PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}" - export PYTHONHOME - unset _OLD_VIRTUAL_PYTHONHOME - fi - - # Call hash to forget past commands. Without forgetting - # past commands the $PATH changes we made may not be respected - hash -r 2> /dev/null - - if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then - PS1="${_OLD_VIRTUAL_PS1:-}" - export PS1 - unset _OLD_VIRTUAL_PS1 - fi - - unset VIRTUAL_ENV - unset VIRTUAL_ENV_PROMPT - if [ ! "${1:-}" = "nondestructive" ] ; then - # Self destruct! - unset -f deactivate - fi -} - -# unset irrelevant variables -deactivate nondestructive - -VIRTUAL_ENV="/home/developer/Documents/repository/AutoTarCompress/.venv-nixos" -export VIRTUAL_ENV - -_OLD_VIRTUAL_PATH="$PATH" -PATH="$VIRTUAL_ENV/bin:$PATH" -export PATH - -# unset PYTHONHOME if set -# this will fail if PYTHONHOME is set to the empty string (which is bad anyway) -# could use `if (set -u; : $PYTHONHOME) ;` in bash -if [ -n "${PYTHONHOME:-}" ] ; then - _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}" - unset PYTHONHOME -fi - -if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then - _OLD_VIRTUAL_PS1="${PS1:-}" - PS1="(.venv-nixos) ${PS1:-}" - export PS1 - VIRTUAL_ENV_PROMPT="(.venv-nixos) " - export VIRTUAL_ENV_PROMPT -fi - -# Call hash to forget past commands. Without forgetting -# past commands the $PATH changes we made may not be respected -hash -r 2> /dev/null diff --git a/.venv-nixos/bin/activate.csh b/.venv-nixos/bin/activate.csh deleted file mode 100644 index 4f798b7..0000000 --- a/.venv-nixos/bin/activate.csh +++ /dev/null @@ -1,26 +0,0 @@ -# This file must be used with "source bin/activate.csh" *from csh*. -# You cannot run it directly. -# Created by Davide Di Blasi . -# Ported to Python 3.3 venv by Andrew Svetlov - -alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; unsetenv VIRTUAL_ENV_PROMPT; test "\!:*" != "nondestructive" && unalias deactivate' - -# Unset irrelevant variables. -deactivate nondestructive - -setenv VIRTUAL_ENV "/home/developer/Documents/repository/AutoTarCompress/.venv-nixos" - -set _OLD_VIRTUAL_PATH="$PATH" -setenv PATH "$VIRTUAL_ENV/bin:$PATH" - - -set _OLD_VIRTUAL_PROMPT="$prompt" - -if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then - set prompt = "(.venv-nixos) $prompt" - setenv VIRTUAL_ENV_PROMPT "(.venv-nixos) " -endif - -alias pydoc python -m pydoc - -rehash diff --git a/.venv-nixos/bin/activate.fish b/.venv-nixos/bin/activate.fish deleted file mode 100644 index 5888942..0000000 --- a/.venv-nixos/bin/activate.fish +++ /dev/null @@ -1,69 +0,0 @@ -# This file must be used with "source /bin/activate.fish" *from fish* -# (https://fishshell.com/); you cannot run it directly. - -function deactivate -d "Exit virtual environment and return to normal shell environment" - # reset old environment variables - if test -n "$_OLD_VIRTUAL_PATH" - set -gx PATH $_OLD_VIRTUAL_PATH - set -e _OLD_VIRTUAL_PATH - end - if test -n "$_OLD_VIRTUAL_PYTHONHOME" - set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME - set -e _OLD_VIRTUAL_PYTHONHOME - end - - if test -n "$_OLD_FISH_PROMPT_OVERRIDE" - set -e _OLD_FISH_PROMPT_OVERRIDE - # prevents error when using nested fish instances (Issue #93858) - if functions -q _old_fish_prompt - functions -e fish_prompt - functions -c _old_fish_prompt fish_prompt - functions -e _old_fish_prompt - end - end - - set -e VIRTUAL_ENV - set -e VIRTUAL_ENV_PROMPT - if test "$argv[1]" != "nondestructive" - # Self-destruct! - functions -e deactivate - end -end - -# Unset irrelevant variables. -deactivate nondestructive - -set -gx VIRTUAL_ENV "/home/developer/Documents/repository/AutoTarCompress/.venv-nixos" - -set -gx _OLD_VIRTUAL_PATH $PATH -set -gx PATH "$VIRTUAL_ENV/bin" $PATH - -# Unset PYTHONHOME if set. -if set -q PYTHONHOME - set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME - set -e PYTHONHOME -end - -if test -z "$VIRTUAL_ENV_DISABLE_PROMPT" - # fish uses a function instead of an env var to generate the prompt. - - # Save the current fish_prompt function as the function _old_fish_prompt. - functions -c fish_prompt _old_fish_prompt - - # With the original prompt function renamed, we can override with our own. - function fish_prompt - # Save the return status of the last command. - set -l old_status $status - - # Output the venv prompt; color taken from the blue of the Python logo. - printf "%s%s%s" (set_color 4B8BBE) "(.venv-nixos) " (set_color normal) - - # Restore the return status of the previous command. - echo "exit $old_status" | . - # Output the original/"old" prompt. - _old_fish_prompt - end - - set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV" - set -gx VIRTUAL_ENV_PROMPT "(.venv-nixos) " -end diff --git a/.venv-nixos/bin/pip b/.venv-nixos/bin/pip deleted file mode 100755 index c6106af..0000000 --- a/.venv-nixos/bin/pip +++ /dev/null @@ -1,8 +0,0 @@ -#!/home/developer/Documents/repository/AutoTarCompress/.venv-nixos/bin/python3 -# -*- coding: utf-8 -*- -import re -import sys -from pip._internal.cli.main import main -if __name__ == '__main__': - sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) - sys.exit(main()) diff --git a/.venv-nixos/bin/pip3 b/.venv-nixos/bin/pip3 deleted file mode 100755 index c6106af..0000000 --- a/.venv-nixos/bin/pip3 +++ /dev/null @@ -1,8 +0,0 @@ -#!/home/developer/Documents/repository/AutoTarCompress/.venv-nixos/bin/python3 -# -*- coding: utf-8 -*- -import re -import sys -from pip._internal.cli.main import main -if __name__ == '__main__': - sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) - sys.exit(main()) diff --git a/.venv-nixos/bin/pip3.11 b/.venv-nixos/bin/pip3.11 deleted file mode 100755 index c6106af..0000000 --- a/.venv-nixos/bin/pip3.11 +++ /dev/null @@ -1,8 +0,0 @@ -#!/home/developer/Documents/repository/AutoTarCompress/.venv-nixos/bin/python3 -# -*- coding: utf-8 -*- -import re -import sys -from pip._internal.cli.main import main -if __name__ == '__main__': - sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) - sys.exit(main()) diff --git a/.venv-nixos/bin/python b/.venv-nixos/bin/python deleted file mode 120000 index b8a0adb..0000000 --- a/.venv-nixos/bin/python +++ /dev/null @@ -1 +0,0 @@ -python3 \ No newline at end of file diff --git a/.venv-nixos/bin/python3 b/.venv-nixos/bin/python3 deleted file mode 120000 index c5047c5..0000000 --- a/.venv-nixos/bin/python3 +++ /dev/null @@ -1 +0,0 @@ -/nix/store/8dzgagiwp6xi6hahsdgq6y4kb5v8kn4j-python3-3.11.8/bin/python3 \ No newline at end of file diff --git a/.venv-nixos/bin/python3.11 b/.venv-nixos/bin/python3.11 deleted file mode 120000 index b8a0adb..0000000 --- a/.venv-nixos/bin/python3.11 +++ /dev/null @@ -1 +0,0 @@ -python3 \ No newline at end of file diff --git a/.venv-nixos/bin/tqdm b/.venv-nixos/bin/tqdm deleted file mode 100755 index 44485d9..0000000 --- a/.venv-nixos/bin/tqdm +++ /dev/null @@ -1,8 +0,0 @@ -#!/home/developer/Documents/repository/AutoTarCompress/.venv-nixos/bin/python3 -# -*- coding: utf-8 -*- -import re -import sys -from tqdm.cli import main -if __name__ == '__main__': - sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) - sys.exit(main()) diff --git a/.venv-nixos/lib64 b/.venv-nixos/lib64 deleted file mode 120000 index 7951405..0000000 --- a/.venv-nixos/lib64 +++ /dev/null @@ -1 +0,0 @@ -lib \ No newline at end of file diff --git a/.venv-nixos/pyvenv.cfg b/.venv-nixos/pyvenv.cfg deleted file mode 100644 index 042d2c4..0000000 --- a/.venv-nixos/pyvenv.cfg +++ /dev/null @@ -1,5 +0,0 @@ -home = /nix/store/8dzgagiwp6xi6hahsdgq6y4kb5v8kn4j-python3-3.11.8/bin -include-system-site-packages = false -version = 3.11.8 -executable = /nix/store/8dzgagiwp6xi6hahsdgq6y4kb5v8kn4j-python3-3.11.8/bin/python3.11 -command = /nix/store/8dzgagiwp6xi6hahsdgq6y4kb5v8kn4j-python3-3.11.8/bin/python3 -m venv /home/developer/Documents/repository/AutoTarCompress/.venv-nixos From ea3d69e09193a214f5ce4d06fcaa2992a3a1f43a Mon Sep 17 00:00:00 2001 From: Cyber-Syntax <115875369+Cyber-Syntax@users.noreply.github.com> Date: Thu, 2 Jan 2025 14:34:22 +0300 Subject: [PATCH 3/6] refactor: implement size calculation --- ignore.txt | 9 ++- main.py | 144 +++++++++++++++++++++++----------------- src/size_calculator.py | 145 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 232 insertions(+), 66 deletions(-) create mode 100644 src/size_calculator.py diff --git a/ignore.txt b/ignore.txt index 34845df..3b560f3 100644 --- a/ignore.txt +++ b/ignore.txt @@ -1,5 +1,4 @@ -Documents/appimages -Documents/screenloyout -Documents/backup-for-cloud -Documents/linuxISO -Documents/test +~/Documents/appimages +~/Documents/screenloyout +~/Documents/backup-for-cloud +~/Documents/linuxISO diff --git a/main.py b/main.py index 825c8c8..5f98f30 100644 --- a/main.py +++ b/main.py @@ -1,47 +1,53 @@ -""" This script will backup with tar command and encrypt, decrypt with openssl command""" - import os import subprocess -import hashlib import datetime +from dataclasses import dataclass, field +from typing import List +from tqdm import tqdm import sys import time +from src.size_calculator import SizeCalculator +@dataclass class BackupManager: - """This class will backup the directories listed in dirs_to_backup.txt to a compressed file""" + backup_folder: str = os.path.expanduser("~/Documents/backup-for-cloud/") + dirs_file_path: str = "dirs_to_backup.txt" + current_date: str = datetime.datetime.now().strftime("%d-%m-%Y") + backup_file_path: str = field(init=False) + ignore_file_path: str = os.path.expanduser("ignore.txt") - def __init__(self): - self.backup_folder = os.path.expanduser("~/Documents/backup-for-cloud/") - self.dirs_file_path = "dirs_to_backup.txt" - self.current_date = datetime.datetime.now().strftime("%d-%m-%Y") + def __post_init__(self): self.backup_file_path = os.path.expanduser( f"{self.backup_folder}/{self.current_date}.tar.xz" ) - self.ignore_file_path = os.path.expanduser("ignore.txt") - # check backup already exist for today - def check_backup_exist(self): + def check_backup_exist(self) -> bool: """Check if a backup file already exists for today""" return os.path.isfile(self.backup_file_path) - def backup_directories(self): + def backup_directories(self) -> bool: """Backup the directories listed in dirs_to_backup.txt to a compressed file""" # Read in the directories to backup from the file - dirs_to_backup = [] + dirs_to_backup: List[str] = [] with open(self.dirs_file_path, "r", encoding="utf-8") as file: for line in file: directory = line.strip() if directory: dirs_to_backup.append(directory) - # Exclude files and directories listed in the ignore file - exclude_option = ( - f"--exclude-from={self.ignore_file_path}" - if os.path.isfile(self.ignore_file_path) - else f"--exclude={self.ignore_file_path}" - ) + # Read and expand paths in the ignore file + ignore_paths = [] + if os.path.isfile(self.ignore_file_path): + with open(self.ignore_file_path, "r", encoding="utf-8") as file: + for line in file: + ignore_path = line.strip() + if ignore_path: + ignore_paths.append(os.path.expanduser(ignore_path)) + + # Generate the exclude options for the tar command + exclude_options = " ".join([f"--exclude={path}" for path in ignore_paths]) # Only backup files on the same filesystem as the backup folder filesystem_option = "--one-file-system" @@ -49,32 +55,57 @@ def backup_directories(self): # Expand the user's home directory for each directory to backup dir_paths = [os.path.expanduser(path) for path in dirs_to_backup] - # Create the tar command with tqdm progress bar - total_size = sum( - entry.stat().st_size - for path in dir_paths - for entry in os.scandir(path) - if entry.is_file() + # Calculate the total size using SizeCalculator + size_calculator = SizeCalculator(self.dirs_file_path, self.ignore_file_path) + total_size_bytes = size_calculator.calculate_total_backup_size( + dirs_to_backup, size_calculator.ignore_list ) - print(f"Total size: {total_size} bytes") + # Convert the total size to MB and GiB + total_size_mb = total_size_bytes / (1024 * 1024) + total_size_gib = total_size_bytes / (1024 * 1024 * 1024) + + print(f"Total size: {total_size_mb:.2f} MB / {total_size_gib:.2f} GiB") # Get the number of CPU threads for xz compression cpu_threads = os.cpu_count() - 1 print(f"CPU threads - 1: {cpu_threads}") - # Create the tar command with tqdm progress bar + # Create the tar command os_cmd = ( - f"tar -cf - {filesystem_option} {exclude_option} {' '.join(dir_paths)} | " - f"xz --threads={cpu_threads} | " - f"tqdm --bytes --total {total_size} --desc Processing | gzip | " - f"tqdm --bytes --total {total_size} " - f"--desc Compressing > {self.backup_file_path}" + f"tar -cf - {filesystem_option} {exclude_options} {' '.join(dir_paths)} | " + f"xz --threads={cpu_threads} > {self.backup_file_path}" ) - # Run the tar command + # Run the tar command and update the progress bar try: - subprocess.run(os_cmd, check=True, shell=True) + proc = subprocess.Popen( + os_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) + pbar = tqdm( + total=total_size_bytes, + unit="B", + unit_scale=True, + desc="Processing", + dynamic_ncols=True, + ) + + while proc.poll() is None: + if os.path.exists(self.backup_file_path): + current_size = os.path.getsize(self.backup_file_path) + pbar.update( + current_size - pbar.n + ) # Update the progress bar with the difference + time.sleep(0.1) # Sleep briefly to avoid too frequent polling + + proc.wait() + pbar.close() + + if proc.returncode != 0: + raise subprocess.CalledProcessError(proc.returncode, os_cmd) + + print("Backup completed successfully") + return True except ( subprocess.CalledProcessError, FileNotFoundError, @@ -87,23 +118,15 @@ def backup_directories(self): except KeyboardInterrupt: print("Backup cancelled") sys.exit(0) - else: - print("Backup completed successfully") - return True -# EncryptionManager inherits from BackupManager +@dataclass class EncryptionManager(BackupManager): - """This script will encrypt the backup file with openssl command""" + decrypt_file_path: str = os.path.expanduser( + "~/Documents/backup-for-cloud/decrypted.tar.xz" + ) - def __init__(self): - # Call the __init__() method of the parent class - super().__init__() - self.decrypt_file_path = os.path.expanduser( - "~/Documents/backup-for-cloud/decrypted.tar.xz" - ) - - def encrypt_backup(self): + def encrypt_backup(self) -> bool: """Encrypt the backup file with openssl command""" # The encrypted backup file will be named with the current date @@ -127,6 +150,7 @@ def encrypt_backup(self): try: # ask user for password, when user enter password, encrypt file subprocess.run(encrypt_cmd, check=True, input="password", encoding="ascii") + return True except ( subprocess.CalledProcessError, FileNotFoundError, @@ -143,9 +167,7 @@ def encrypt_backup(self): else: print("Encryption completed successfully") - return True - - def decrypt(self, file_to_decrypt): + def decrypt(self, file_to_decrypt: str) -> bool: """Decrypt the backup file""" # Decrypt the backup file with openssl command @@ -167,6 +189,7 @@ def decrypt(self, file_to_decrypt): subprocess.run(decrypt_cmd, check=True, input="password", encoding="ascii") # Wait for the file to be decrypted time.sleep(1) + return True except ( subprocess.CalledProcessError, FileNotFoundError, @@ -180,13 +203,9 @@ def decrypt(self, file_to_decrypt): except KeyboardInterrupt: print("Decryption cancelled") sys.exit(0) - else: - print("Decryption completed successfully") - return True - - def verify_decrypt_file(self, file_to_decrypt): - """Verify the decrypted file same with original file""" + def verify_decrypt_file(self, file_to_decrypt: str): + """Verify the decrypted file is the same as the original file""" # remove .enc from file_to_decrypt original_file_path = file_to_decrypt[:-4] @@ -202,14 +221,14 @@ def verify_decrypt_file(self, file_to_decrypt): actual_checksum = hasher.hexdigest() # Compute the SHA256 checksum of the *original file* - hasher_orginal = hashlib.sha256() + hasher_original = hashlib.sha256() with open(original_file_path, "rb") as f: while True: data = f.read(65536) if not data: break - hasher_orginal.update(data) - expected_checksum = hasher_orginal.hexdigest() + hasher_original.update(data) + expected_checksum = hasher_original.hexdigest() # Compare the checksums if actual_checksum == expected_checksum: @@ -251,10 +270,13 @@ def main(): print("Backup already exists for today") continue # Backup the directories listed in dirs_to_backup.txt to a compressed file - backup_manager.backup_directories() + success = backup_manager.backup_directories() + if success: + print("Backup was successful.") + else: + print("Backup failed.") elif choice == 2: encryption_manager.encrypt_backup() - elif choice == 3: # List all encrypted files print("=====================================") diff --git a/src/size_calculator.py b/src/size_calculator.py new file mode 100644 index 0000000..2abd239 --- /dev/null +++ b/src/size_calculator.py @@ -0,0 +1,145 @@ +import os +from dataclasses import dataclass, field +from typing import List +from tqdm import tqdm + + +@dataclass +class SizeCalculator: + dirs_file_path: str + ignore_file_path: str + dirs_to_backup: List[str] = field(init=False) + ignore_list: List[str] = field(init=False) + + def __post_init__(self): + self.dirs_to_backup = self.read_dirs_to_backup() + self.ignore_list = self.read_ignore_list() + + def read_dirs_to_backup(self) -> List[str]: + """Read the directories and files listed in dirs_to_backup.txt""" + dirs_to_backup = [] + with open(self.dirs_file_path, "r", encoding="utf-8") as file: + for line in file: + directory = line.strip() + if directory: + dirs_to_backup.append(os.path.expanduser(directory)) + return dirs_to_backup + + def read_ignore_list(self) -> List[str]: + """Read the directories and files listed in ignore.txt""" + ignore_list = [] + if os.path.isfile(self.ignore_file_path): + with open(self.ignore_file_path, "r", encoding="utf-8") as file: + for line in file: + ignore_path = line.strip() + if ignore_path: + ignore_list.append(os.path.expanduser(ignore_path)) + return ignore_list + + def calculate_directory_size( + self, directory: str, ignore_paths: List[str] = [] + ) -> int: + """Calculate the total size of a directory, excluding ignored paths""" + total_size = 0 + ignore_paths_set = set(ignore_paths) + for dirpath, dirnames, filenames in os.walk(directory): + # Skip ignored directories + dirnames[:] = [ + d + for d in dirnames + if not any( + os.path.join(dirpath, d).startswith(ignored) + for ignored in ignore_paths_set + ) + ] + for filename in filenames: + filepath = os.path.join(dirpath, filename) + if not any( + filepath.startswith(ignored) for ignored in ignore_paths_set + ): + try: + total_size += os.path.getsize(filepath) + except FileNotFoundError: + continue + except Exception as e: + print(f"Error accessing {filepath}: {e}") + continue + return total_size + + def calculate_total_size_of_dirs(self, dirs: List[str]) -> int: + """Calculate the total size of a list of directories""" + total_size = 0 + print("\nCalculating sizes for directories in dirs_to_backup.txt:") + for path in tqdm(dirs, desc="Processing directories"): + expanded_path = os.path.expanduser(path) + if os.path.isdir(expanded_path): + dir_size = self.calculate_directory_size(expanded_path, []) + total_size += dir_size + print(f"Directory: {expanded_path}, Size: {self.format_size(dir_size)}") + return total_size + + def calculate_total_backup_size( + self, dirs_to_backup: List[str], ignore_list: List[str] + ) -> int: + """Calculate the total size of directories to backup, excluding ignored paths""" + total_backup_size = 0 + print("\nCalculating sizes for backup directories excluding ignored paths:") + for path in tqdm(dirs_to_backup, desc="Processing backup directories"): + expanded_path = os.path.expanduser(path) + if os.path.isdir(expanded_path): + dir_size = self.calculate_directory_size(expanded_path, ignore_list) + total_backup_size += dir_size + print( + f"Backup Path: {expanded_path}\n Size: {self.format_size(dir_size)}" + ) + return total_backup_size + + def calculate_total_ignore_size(self, ignore_list: List[str]) -> int: + """Calculate the total size of ignored directories""" + total_ignore_size = 0 + print("\nCalculating sizes for ignored directories:") + for path in tqdm(ignore_list, desc="Processing ignored directories"): + expanded_ignore_path = os.path.expanduser(path) + if os.path.isdir(expanded_ignore_path): + ignore_size = self.calculate_directory_size(expanded_ignore_path, []) + total_ignore_size += ignore_size + print( + f"Ignored Path: {expanded_ignore_path}, Size: {self.format_size(ignore_size)}" + ) + return total_ignore_size + + def format_size(self, size: int) -> str: + """Format the size to be more user-friendly""" + size_mb = size / (1024 * 1024) + size_gb = size / (1024 * 1024 * 1024) + if size_gb >= 1: + return f"{size_gb:.2f} GiB" + else: + return f"{size_mb:.2f} MiB" + + def calculate_and_display_sizes(self): + """Calculate and display sizes""" + total_size_of_dirs_to_backup = self.calculate_total_size_of_dirs( + self.dirs_to_backup + ) + total_ignore_size = self.calculate_total_ignore_size(self.ignore_list) + total_backup_size_excluding_ignored = self.calculate_total_backup_size( + self.dirs_to_backup, self.ignore_list + ) + + print("\nSummary of Sizes:") + print( + f"Total size of directories to backup (before excluding ignored directories): {self.format_size(total_size_of_dirs_to_backup)}" + ) + print( + f"Total size of ignored directories: {self.format_size(total_ignore_size)}" + ) + print( + f"Total size of directories to backup (after excluding ignored directories): {self.format_size(total_backup_size_excluding_ignored)}" + ) + + +# Example usage: +if __name__ == "__main__": + calculator = SizeCalculator("dirs_to_backup.txt", "ignore.txt") + calculator.calculate_and_display_sizes() From 00955e5293689a99f4fe974fc02227f980e1521c Mon Sep 17 00:00:00 2001 From: Cyber-Syntax <115875369+Cyber-Syntax@users.noreply.github.com> Date: Thu, 2 Jan 2025 15:57:21 +0300 Subject: [PATCH 4/6] refactor: implement old backup files deletion --- backup_config.json | 4 ++ config_files/backup_config.json | 4 ++ main.py | 11 +++- src/old_delete.py | 111 ++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 backup_config.json create mode 100644 config_files/backup_config.json create mode 100644 src/old_delete.py diff --git a/backup_config.json b/backup_config.json new file mode 100644 index 0000000..b7bbab1 --- /dev/null +++ b/backup_config.json @@ -0,0 +1,4 @@ +{ + "keep_count": 3, + "keep_enc_count": 1 +} \ No newline at end of file diff --git a/config_files/backup_config.json b/config_files/backup_config.json new file mode 100644 index 0000000..57881b8 --- /dev/null +++ b/config_files/backup_config.json @@ -0,0 +1,4 @@ +{ + "keep_count": 2, + "keep_enc_count": 1 +} \ No newline at end of file diff --git a/main.py b/main.py index 5f98f30..7e83d62 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ import sys import time from src.size_calculator import SizeCalculator +from src.old_delete import BackupDeletionManager @dataclass @@ -243,6 +244,7 @@ def main(): # Classes backup_manager = BackupManager() encryption_manager = EncryptionManager() + deletion_manager = BackupDeletionManager() # Create a loop that will run until the user enters 4 to exit while True: @@ -252,13 +254,14 @@ def main(): print("1.Backup") print("2.Encrypt") print("3.Decrypt") - print("4.Exit") + print("4.Delete Old Backups") + print("5.Exit") print("=====================================") try: choice = int(input("Enter your choice: ")) except (ValueError, TypeError, NameError, AttributeError, IndexError) as error: print(f"Error: {type(error).__name__} - {error}") - print("Please enter a number between 1 and 4.") + print("Please enter a number between 1 and 5.") continue except KeyboardInterrupt: print("Exiting...") @@ -300,10 +303,12 @@ def main(): encryption_manager.decrypt(file_to_decrypt) encryption_manager.verify_decrypt_file(file_to_decrypt) elif choice == 4: + deletion_manager.delete_old_backups() + elif choice == 5: print("Exiting...") sys.exit(0) else: - print("Invalid choice. Please enter a number between 1 and 4.") + print("Invalid choice. Please enter a number between 1 and 5.") return diff --git a/src/old_delete.py b/src/old_delete.py new file mode 100644 index 0000000..accae10 --- /dev/null +++ b/src/old_delete.py @@ -0,0 +1,111 @@ +import os +import json +import datetime +import re +import sys +from dataclasses import dataclass + + +def ask_and_save_preferences(config_file_path: str): + """Ask the user for preferences and save them to the config file.""" + try: + keep_count = int(input("Enter the number of backups to keep: ")) + keep_enc_count = int(input("Enter the number of .enc backups to keep: ")) + except ValueError: + print("Invalid input. Please enter a number.") + sys.exit(1) + + config = {"keep_count": keep_count, "keep_enc_count": keep_enc_count} + with open(config_file_path, "w") as config_file: + json.dump(config, config_file, indent=4) + print(f"Updated number of backups to keep to {keep_count}") + print(f"Updated number of .enc backups to keep to {keep_enc_count}") + + +@dataclass +class BackupDeletionManager: + backup_folder: str = os.path.expanduser("~/Documents/backup-for-cloud/") + config_file_path: str = "config_files/backup_config.json" + keep_count: int = 3 # Default value + keep_enc_count: int = 0 # Default value + + def __post_init__(self): + # Ensure the config_files directory exists + os.makedirs(os.path.dirname(self.config_file_path), exist_ok=True) + self.load_config_or_ask_preferences() + + def load_config_or_ask_preferences(self): + """Load configuration from the config file or ask for user input if the file doesn't exist.""" + if os.path.exists(self.config_file_path): + try: + with open(self.config_file_path, "r") as config_file: + config = json.load(config_file) + self.keep_count = config.get("keep_count", 3) + self.keep_enc_count = config.get("keep_enc_count", 0) + except FileNotFoundError: + self.save_config() # Save default config if file not found + else: + ask_and_save_preferences(self.config_file_path) + self.load_config() # Load the newly created config + + def load_config(self): + """Load configuration from the config file.""" + try: + with open(self.config_file_path, "r") as config_file: + config = json.load(config_file) + self.keep_count = config.get("keep_count", 3) + self.keep_enc_count = config.get("keep_enc_count", 0) + except FileNotFoundError: + self.save_config() # Save default config if file not found + + def save_config(self): + """Save the current configuration to the config file.""" + config = {"keep_count": self.keep_count, "keep_enc_count": self.keep_enc_count} + with open(self.config_file_path, "w") as config_file: + json.dump(config, config_file, indent=4) + + def delete_old_backups(self): + """Delete old backup files if there are more than 'keep_count' files""" + # List all backup files in the backup folder + backup_files = [ + f + for f in os.listdir(self.backup_folder) + if re.match(r"\d{2}-\d{2}-\d{4}\.tar\.xz", f) + or re.match(r"\d{2}-\d{2}-\d{4}\.tar\.xz\.enc", f) + ] + + # Sort the backup files by date + backup_files.sort( + key=lambda x: datetime.datetime.strptime(x.split(".")[0], "%d-%m-%Y") + ) + + # Separate .xz and .xz.enc files + xz_files = [f for f in backup_files if f.endswith(".tar.xz")] + enc_files = [f for f in backup_files if f.endswith(".tar.xz.enc")] + + # Track deleted files to avoid duplicate deletions + deleted_files = set() + + # Delete old backups if there are more than 'keep_count' + while len(xz_files) > self.keep_count: + old_backup = xz_files.pop(0) + old_backup_path = os.path.join(self.backup_folder, old_backup) + print(f"Attempting to delete: {old_backup_path}") + try: + os.remove(old_backup_path) + deleted_files.add(old_backup) + print(f"Deleted old backup: {old_backup}") + except Exception as e: + print(f"Failed to delete {old_backup_path}: {e}") + + # Delete old .enc files if there are more than 'keep_enc_count' + while len(enc_files) > self.keep_enc_count: + old_enc_backup = enc_files.pop(0) + old_enc_backup_path = os.path.join(self.backup_folder, old_enc_backup) + print(f"Attempting to delete: {old_enc_backup_path}") + try: + os.remove(old_enc_backup_path) + deleted_files.add(old_enc_backup) + print(f"Deleted old encrypted backup: {old_enc_backup}") + except Exception as e: + print(f"Failed to delete {old_enc_backup_path}: {e}") From 42c0ef65accb867383eb6efd6168e56100a3b4dd Mon Sep 17 00:00:00 2001 From: Cyber-Syntax <115875369+Cyber-Syntax@users.noreply.github.com> Date: Thu, 2 Jan 2025 16:05:17 +0300 Subject: [PATCH 5/6] style: improve prompts --- backup_config.json | 4 ---- src/old_delete.py | 3 +++ 2 files changed, 3 insertions(+), 4 deletions(-) delete mode 100644 backup_config.json diff --git a/backup_config.json b/backup_config.json deleted file mode 100644 index b7bbab1..0000000 --- a/backup_config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "keep_count": 3, - "keep_enc_count": 1 -} \ No newline at end of file diff --git a/src/old_delete.py b/src/old_delete.py index accae10..554f059 100644 --- a/src/old_delete.py +++ b/src/old_delete.py @@ -18,6 +18,7 @@ def ask_and_save_preferences(config_file_path: str): config = {"keep_count": keep_count, "keep_enc_count": keep_enc_count} with open(config_file_path, "w") as config_file: json.dump(config, config_file, indent=4) + print(f"Configuration file created at {config_file_path}") print(f"Updated number of backups to keep to {keep_count}") print(f"Updated number of .enc backups to keep to {keep_enc_count}") @@ -109,3 +110,5 @@ def delete_old_backups(self): print(f"Deleted old encrypted backup: {old_enc_backup}") except Exception as e: print(f"Failed to delete {old_enc_backup_path}: {e}") + + print("Old backup deletion process completed.") From 3e236a664aeba82597bbe2a7510d15b7da0b87ce Mon Sep 17 00:00:00 2001 From: Cyber-Syntax <115875369+Cyber-Syntax@users.noreply.github.com> Date: Thu, 2 Jan 2025 16:36:42 +0300 Subject: [PATCH 6/6] security: use getpass for user password input --- main.py | 62 +++++++++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/main.py b/main.py index 7e83d62..699a73e 100644 --- a/main.py +++ b/main.py @@ -6,8 +6,11 @@ from tqdm import tqdm import sys import time +import getpass +import logging from src.size_calculator import SizeCalculator from src.old_delete import BackupDeletionManager +import hashlib @dataclass @@ -129,6 +132,7 @@ class EncryptionManager(BackupManager): def encrypt_backup(self) -> bool: """Encrypt the backup file with openssl command""" + password = getpass.getpass(prompt="Enter encryption password: ") # The encrypted backup file will be named with the current date file_to_encrypt = os.path.join( @@ -146,30 +150,24 @@ def encrypt_backup(self) -> bool: self.backup_file_path, "-out", file_to_encrypt, + "-pass", + f"pass:{password}", ] try: - # ask user for password, when user enter password, encrypt file - subprocess.run(encrypt_cmd, check=True, input="password", encoding="ascii") + subprocess.run(encrypt_cmd, check=True) + logging.info("Encryption completed successfully") return True - except ( - subprocess.CalledProcessError, - FileNotFoundError, - PermissionError, - OSError, - subprocess.TimeoutExpired, - ValueError, - ) as error: - print(f"Error encrypting file: {type(error).__name__} - {error}") + except subprocess.CalledProcessError as error: + logging.error(f"Error encrypting file: {error}") return False except KeyboardInterrupt: - print("Encryption cancelled") + logging.info("Encryption cancelled") sys.exit(0) - else: - print("Encryption completed successfully") def decrypt(self, file_to_decrypt: str) -> bool: """Decrypt the backup file""" + password = getpass.getpass(prompt="Enter decryption password: ") # Decrypt the backup file with openssl command decrypt_cmd = [ @@ -183,35 +181,27 @@ def decrypt(self, file_to_decrypt: str) -> bool: file_to_decrypt, "-out", self.decrypt_file_path, + "-pass", + f"pass:{password}", ] try: - # ask user for password - subprocess.run(decrypt_cmd, check=True, input="password", encoding="ascii") - # Wait for the file to be decrypted + subprocess.run(decrypt_cmd, check=True) time.sleep(1) + logging.info("Decryption completed successfully") return True - except ( - subprocess.CalledProcessError, - FileNotFoundError, - PermissionError, - OSError, - subprocess.TimeoutExpired, - ValueError, - ) as error: - print(f"Error decrypting file: {type(error).__name__} - {error}") + except subprocess.CalledProcessError as error: + logging.error(f"Error decrypting file: {error}") return False except KeyboardInterrupt: - print("Decryption cancelled") + logging.info("Decryption cancelled") sys.exit(0) def verify_decrypt_file(self, file_to_decrypt: str): """Verify the decrypted file is the same as the original file""" - - # remove .enc from file_to_decrypt original_file_path = file_to_decrypt[:-4] - # Compute the SHA256 checksum of the *decrypted file* + # Compute the SHA256 checksum of the decrypted file hasher = hashlib.sha256() with open(self.decrypt_file_path, "rb") as f: while True: @@ -221,7 +211,7 @@ def verify_decrypt_file(self, file_to_decrypt: str): hasher.update(data) actual_checksum = hasher.hexdigest() - # Compute the SHA256 checksum of the *original file* + # Compute the SHA256 checksum of the original file hasher_original = hashlib.sha256() with open(original_file_path, "rb") as f: while True: @@ -233,9 +223,15 @@ def verify_decrypt_file(self, file_to_decrypt: str): # Compare the checksums if actual_checksum == expected_checksum: - print("File integrity verified: checksums match") + logging.info("File integrity verified: checksums match") else: - print("File integrity check failed: checksums do not match") + logging.error("File integrity check failed: checksums do not match") + + +# Configure logging +logging.basicConfig( + level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" +) def main():