)i<dZddlmZddlmZddlmZmZmZmZddl Z ddl m Z ddl m Z ddlmZGd d ZdS) zt Backup Module - Integration with Automation Daemon Manages backup operations, scheduling, and database integration )Path)datetime)DictAnyOptionalCallableN)logger)Database)TimeMachineBackupcLeZdZdZdefdZdedeefdZ ddedee d ee de e e ffd Zdded eed eede e e ffd Zdede e e ffdZ ddede de de dee de e e ff dZdedede e e ffdZdZdS) BackupModulezo Backup module for unified daemon Integrates TimeMachineBackup with database and scheduling system dbcJ||_i|_tjddS)zo Initialize backup module Args: db: Database instance for CRUD operations zBackupModule initializedN)rbackup_enginesr info)selfrs 3c:\PYTHON\Scheduler\modules\backup\backup_module.py__init__zBackupModule.__init__s*<> .///// profile_idreturnc||jvr |j|S|j|}|stjd|ddS t |d}||j|<tjd|d|d|S#t$r%}tjd |d|Yd}~dSd}~wwxYw) z Get or create backup engine for a profile Args: profile_id: Backup profile ID Returns: TimeMachineBackup instance or None if profile not found Profile not foundN backup_root)rz"Created backup engine for profile : name+Failed to create backup engine for profile )rrget_backup_profiler errorr r Exception)rrprofileenginees r get_enginezBackupModule.get_engine!s , , ,&z2 2',,Z88  L:J::: ; ; ;4 &7=3IJJJF.4D  + K\Z\\7SY?\\ ] ] ]M    LXzXXUVXX Y Y Y44444 sAB B?B::B?N backup_nameprogress_callbackc |j|}|sd|d}tj|d|dS|dsd|d}tj|d|dS||}|sd|}tj|d|dSt j}|j|d|pd | d d | }tj d |d|d|d t|dtrtj|dn|d} d} |dr=t|dtrtj|dn|d} || || |d|jd|pd | d } | st%dt j} |j|d| | dd| ddz| dd| dd| dd| ddtj d|d d|||| dd| ddz| dd| dd| dd| dd| |z d!d"S#t$$ri} t| }tjd|d#||j|d$t j|%d|||d&cYd} ~ Sd} ~ wwxYw)'aR Execute a backup operation Args: profile_id: Backup profile ID backup_name: Optional custom backup name (default: auto-generated timestamp) progress_callback: Optional callback for progress updates Returns: Dict with operation results (id, status, stats) rrFsuccessr enabledz is disabledrbackupzbackup-z%Y-%m-%d-%H%M%Srunning)roperation_typer&status start_timezStarting backup operation for profile rrsourcesNexclude_patternsT) source_dirsr&excludeprogressprogress_precountbackupsz'Backup completed but metadata not found completed files_linkedr files_copiedfiles_in_store total_size) operation_idr/end_time files_totalr:r;r<total_size_byteszBackup operation  completed successfully)r@r:r;r<rAduration_seconds)r*r>rr&stats failed: failedr>r/r? error_messager*r>rr )rrr r warningr%rnowcreate_backup_operationstrftimer isinstancestrjsonloadsget create_backupmetadatar!update_backup_operation total_seconds)rrr&r'r" error_msgr#r0r>r2r3 backup_infor?r$s rexecute_backupzBackupModule.execute_backup?s$',,Z88 :9:999I L # # #$y99 9y! :;:;;;I N9 % % %$y99 9,, :RjRRI L # # #$y99 9\^^ w66!##Y'Y1D1DEV1W1W'Y'Y! 7    kkkJkkZabhZikklllI 8B79CUWZ8[8[sdj!3444ahirasG $ {{-.. \NXY`asYtvyNzNz$\4:g6H.I#J#J#JAHI[A\   #'(*"& !   !/)488QQ)<)<=N)O)OQQK K IJJJ |~~H G + +)"!'OONA>>Q_abAcAcc(__^Q??(__^Q??*/?CC!,q!A!A ,    KQLQQQ R R R ,(*#.??>1#E#E XfhiHjHj#j$/OONA$F$F$/OONA$F$F&1oo6F&J&J(3 a(H(H)1J)>(M(M(O(O       AI LO\OOIOO P P P G + +)!' ,   ! ,("        s IM!! O+AO OOkeepolder_than_daysc,|j|}|sd|d}tj|d|dS||}|sd|}tj|d|dSt j}|j|dd|}tjd |d | | || |j |d t j tjd|dd||dS#t$ri} t| }tjd|d||j |dt j|d|||dcYd} ~ Sd} ~ wwxYw)a Execute backup pruning operation Args: profile_id: Backup profile ID keep: Keep only N most recent backups older_than_days: Remove backups older than N days Returns: Dict with operation results rrFr)rpruner-rr.r/r0zStarting prune operation r1)rZr[r9r>r/r?zPrune operation rBTr*r>rrErFrGrIN) rrr r r%rrKrLr prune_backupsrUr!rO) rrrZr[r"rWr#r0r>r$s r execute_prunezBackupModule.execute_prunes%',,Z88 :9:999I L # # #$y99 9,, :RjRRI L # # #$y99 9\^^ w66!"! 7    W WW:WWXXX#   dO L L L G + +)"! ,    KP<PPP Q Q Q ,(     AI LNLNN9NN O O O G + +)!' ,   ! ,("        s =A"D F*AFFFc&|j|}|sd|d}tj|d|dS||}|sd|}tj|d|dSt j}|j|dd|}tjd |d | | |j |d t j tjd |dd||dS#t$ri}t|}tjd |d||j |dt j|d|||dcYd}~Sd}~wwxYw)z Execute garbage collection on backup store Args: profile_id: Backup profile ID Returns: Dict with operation results rrFr)rgcr-r^zStarting GC operation r1r9r_z GC operation rBTr`rErFrGrIN) rrr r r%rrKrLrgc_storerUr!rO)rrr"rWr#r0r>r$s r execute_gczBackupModule.execute_gcs',,Z88 :9:999I L # # #$y99 9,, :RjRRI L # # #$y99 9\^^ w66!! 7    T\TT TTUUU#  OO    G + +)"! ,    KM MMM N N N ,(     AI LKKK KK L L L G + +)!' ,   ! ,("        s =AD F'AF F F restore_typedestination_path source_pathc &|j|}|sd|d}tj|d|dS||}|sd|}tj|d|dSt j} |j|||||d| } tjd| d | |d kr| || nr|d kr*|std | |||nB|dkr*|std| |||ntd||j | dt jtjd| dd| ||dS#t$ri} t| }tjd| d||j | dt j|d| ||dcYd} ~ Sd} ~ wwxYw)a Execute backup restore operation Args: profile_id: Backup profile ID backup_name: Name of backup to restore restore_type: 'full', 'file', or 'directory' destination_path: Where to restore files source_path: Source file/directory within backup (for file/directory restore) Returns: Dict with operation results rrFr)rr-)rr&rgrirhr/r0zStarting restore operation r1full) restore_pathfilez%source_path required for file restore) restore_to directoryz*source_path required for directory restorezInvalid restore_type: r9) restore_idr/r?zRestore operation rBT)r*rprr&rErF)rpr/r?rH)r*rprr N)rrr r r%rrKcreate_backup_restorerrestore_backup ValueError restore_filerestore_directoryupdate_backup_restorer!rO) rrr&rgrhrir"rWr#r0rpr$s rexecute_restorezBackupModule.execute_restoreHs*',,Z88 :9:999I L # # #$y99 9,, :RjRRI L # # #$y99 9\^^ W22!#%#-!3    W*WW:WWXXX/ v%%%%k@P%QQQQ''"N$%LMMM##KIY#ZZZZ,,"S$%QRRR((kN^(____ !H,!H!HIII G ) )%"! *    KPZPPP Q Q Q ((*     AI LNjNN9NN O O O G ) )%!' *   !(("        s CF H'AH H H schedule_id target_idcftjd|d|||}|S)a& Execute scheduled backup operation Called by daemon when a schedule triggers Args: schedule_id: Schedule ID that triggered target_id: Backup profile ID (from schedule.target_id) Returns: Dict with execution results z Schedule z triggered for backup profile )r)r rrY)rrxryresults r schedule_itemzBackupModule.schedule_itemsB  V VV9VVWWW$$ $:: rc`|jtjddS)zCleanup module resourceszBackupModule cleanup completedN)rclearr r)rs rcleanupzBackupModule.cleanups. !!### 455555r)NN)N)__name__ __module__ __qualname____doc__r rintrr r%rOrrrrYrbrfrwr|rrrr r s 08 0 0 0 0SX6G-HB&*04 yyyc]y$H- y c3h yyyyvFFF8C=FZbcfZgFswx{~AyAtBFFFFPDSDT#s(^DDDDX&* ^^^^ ^  ^ c] ^ c3h^^^^@c3h&66666rr )rpathlibrrtypingrrrrrPlogurur core.databaser modules.backup.backup_enginer r rrrrs 000000000000 """"""::::::n6n6n6n6n6n6n6n6n6n6r