Files
Py-Backup/pybackup-cli.py

113 lines
4.1 KiB
Python

#!/usr/bin/python3
import argparse
import sys
import os
from queue import Queue
import threading
from core.backup_manager import BackupManager
from shared_libs.logger import app_logger
from core.pbp_app_config import AppConfig
# A simple logger for the CLI that just prints to the console
class CliLogger:
def log(self, message):
print(f"[CLI] {message}")
def init_logger(self, log_method):
pass # Not needed for CLI
def main():
parser = argparse.ArgumentParser(
description="Py-Backup Command-Line Interface.")
parser.add_argument(
"--backup-type", choices=['user', 'system'], required=True, help="Type of backup to perform.")
parser.add_argument("--destination", required=True,
help="Destination directory for the backup.")
parser.add_argument(
"--source", help="Source directory for user backup. Required for --backup-type user.")
parser.add_argument("--mode", choices=['full', 'incremental'],
default='incremental', help="Mode for system backup.")
parser.add_argument("--encrypted", action='store_true',
help="Flag to indicate the backup should be encrypted.")
parser.add_argument(
"--key-file", help="Path to the key file for unlocking an encrypted container.")
parser.add_argument(
"--password", help="Password for the encrypted container (use with caution). If --key-file is not provided, this will be used.")
parser.add_argument("--compressed", action='store_true',
help="Flag to indicate the backup should be compressed.")
args = parser.parse_args()
if args.backup_type == 'user' and not args.source:
parser.error("--source is required for --backup-type 'user'.")
if args.encrypted and not (args.key_file or args.password):
parser.error(
"For encrypted backups, either --key-file or --password must be provided.")
cli_logger = CliLogger()
backup_manager = BackupManager(cli_logger)
queue = Queue() # Dummy queue for now, might be used for progress later
source_path = "/" # Default for system backup
if args.backup_type == 'user':
source_path = args.source
if not os.path.isdir(source_path):
cli_logger.log(
f"Error: Source path '{source_path}' does not exist or is not a directory.")
sys.exit(1)
# Determine password or key_file to pass
auth_password = None
auth_key_file = None
if args.encrypted:
if args.key_file:
auth_key_file = args.key_file
if not os.path.exists(auth_key_file):
cli_logger.log(
f"Error: Key file '{auth_key_file}' does not exist.")
sys.exit(1)
elif args.password:
auth_password = args.password
cli_logger.log(f"Starting backup with the following configuration:")
cli_logger.log(f" Type: {args.backup_type}")
cli_logger.log(f" Source: {source_path}")
cli_logger.log(f" Destination: {args.destination}")
cli_logger.log(f" Mode: {args.mode}")
cli_logger.log(f" Encrypted: {args.encrypted}")
cli_logger.log(f" Compressed: {args.compressed}")
if auth_key_file:
cli_logger.log(f" Auth Method: Key File ({auth_key_file})")
elif auth_password:
cli_logger.log(f" Auth Method: Password (REDACTED)")
# Call the backup manager
backup_thread = backup_manager.start_backup(
queue=queue,
source_path=source_path,
dest_path=args.destination,
is_system=(args.backup_type == 'system'),
is_dry_run=False,
exclude_files=None, # Excludes are handled by AppConfig.MANUAL_EXCLUDE_LIST_PATH
source_size=0, # Not accurately calculable in CLI without scanning, set to 0
is_compressed=args.compressed,
is_encrypted=args.encrypted,
mode=args.mode,
password=auth_password,
key_file=auth_key_file
)
# Wait for the backup thread to complete
backup_thread.join()
cli_logger.log("CLI backup process finished.")
if __name__ == "__main__":
main()