Plugin Directory

Changeset 3474147


Ignore:
Timestamp:
03/04/2026 04:15:15 AM (7 days ago)
Author:
siteskite
Message:

1.2.9: Fix incremental backup cleanup when triggered via schedule/cron - clean tmp, DB records (progress, full_backup_lock), empty chunk dirs; trigger cleanup action

Location:
siteskite
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • siteskite/tags/1.2.9/includes/Backup/BackupManager.php

    r3474141 r3474147  
    1017310173                ]);
    1017410174            }
     10175
     10176            // Clean all files under tmp folder for this backup (same as manual trigger)
     10177            try {
     10178                $this->cleanup_incremental_tmp_files($backup_id);
     10179            } catch (\Throwable $t) {
     10180                $this->logger->warning('Failed to cleanup tmp folder for incremental backup', [
     10181                    'backup_id' => $backup_id,
     10182                    'error' => $t->getMessage()
     10183                ]);
     10184            }
     10185
     10186            // Remove empty directories under chunks (same as manual trigger)
     10187            try {
     10188                $this->cleanup_empty_chunk_directories();
     10189            } catch (\Throwable $t) {
     10190                $this->logger->warning('Failed to cleanup empty chunk directories', [
     10191                    'backup_id' => $backup_id,
     10192                    'error' => $t->getMessage()
     10193                ]);
     10194            }
    1017510195           
    1017610196            // Clean up ALL external cron jobs for this backup (comprehensive cleanup)
     
    1024610266                delete_transient($completion_lock_key);
    1024710267            }
     10268
     10269            // Trigger cleanup action so tmp, DB records, and lock files are cleaned (same as manual trigger)
     10270            do_action('siteskite_cleanup_backup_files', $backup_id);
    1024810271           
    1024910272        } catch (\Exception $e) {
     
    1038210405                $deleted_options[] = 'incremental_progress';
    1038310406            }
    10384             // Some older code stored progress/last_stage with these keys
     10407            // Some older code stored progress/last_stage with these keys (ProgressTracker uses siteskite_progress_)
    1038510408            if (\delete_option('siteskite_progress_' . $backup_id)) {
    1038610409                $deleted_options[] = 'progress';
     
    1038810411            if (\delete_option('siteskite_last_stage_' . $backup_id)) {
    1038910412                $deleted_options[] = 'last_stage';
     10413            }
     10414
     10415            // Full backup lock options (used by process_full_backup_cron; sometimes stored with _incremental suffix)
     10416            $lock_keys = [
     10417                'siteskite_full_backup_lock_' . $backup_id,
     10418                'siteskite_full_backup_lock_' . $backup_id . '_incremental',
     10419            ];
     10420            foreach ($lock_keys as $lock_key) {
     10421                if (\delete_option($lock_key)) {
     10422                    $deleted_options[] = basename($lock_key);
     10423                }
    1039010424            }
    1039110425           
     
    1077210806                'backup_id' => $backup_id,
    1077310807                'error' => $t->getMessage()
     10808            ]);
     10809        }
     10810    }
     10811
     10812    /**
     10813     * Clean all temporary files under siteskite-backups/tmp for this backup (e.g. SQL dumps).
     10814     * Ensures scheduled incremental completion cleans tmp the same as manual.
     10815     */
     10816    private function cleanup_incremental_tmp_files(string $backup_id): void
     10817    {
     10818        $tmp_dir = SITESKITE_BACKUP_PATH . '/tmp';
     10819        if (!is_dir($tmp_dir)) {
     10820            return;
     10821        }
     10822        $prefix = $backup_id;
     10823        $entries = @scandir($tmp_dir);
     10824        if ($entries === false) {
     10825            return;
     10826        }
     10827        $deleted = 0;
     10828        foreach ($entries as $entry) {
     10829            if ($entry === '.' || $entry === '..') {
     10830                continue;
     10831            }
     10832            if (strpos($entry, $prefix) !== 0) {
     10833                continue;
     10834            }
     10835            $path = $tmp_dir . '/' . $entry;
     10836            if (is_file($path)) {
     10837                if (function_exists('wp_delete_file') && wp_delete_file($path)) {
     10838                    $deleted++;
     10839                } elseif (@unlink($path)) {
     10840                    $deleted++;
     10841                }
     10842            }
     10843        }
     10844        if ($deleted > 0) {
     10845            $this->logger->info('Cleaned tmp folder files for incremental backup', [
     10846                'backup_id' => $backup_id,
     10847                'deleted_count' => $deleted
     10848            ]);
     10849        }
     10850    }
     10851
     10852    /**
     10853     * Remove empty directories under siteskite-backups/chunks.
     10854     * Ensures scheduled incremental completion cleans empty chunk dirs the same as manual.
     10855     */
     10856    private function cleanup_empty_chunk_directories(): void
     10857    {
     10858        $chunks_base = SITESKITE_BACKUP_PATH . '/chunks';
     10859        if (!is_dir($chunks_base)) {
     10860            return;
     10861        }
     10862        $dirs = @scandir($chunks_base);
     10863        if ($dirs === false) {
     10864            return;
     10865        }
     10866        $removed = 0;
     10867        foreach ($dirs as $dir) {
     10868            if ($dir === '.' || $dir === '..' || !is_dir($chunks_base . '/' . $dir)) {
     10869                continue;
     10870            }
     10871            $sub_path = $chunks_base . '/' . $dir;
     10872            $files = @scandir($sub_path);
     10873            if ($files !== false && count($files) <= 2) {
     10874                if (@rmdir($sub_path)) {
     10875                    $removed++;
     10876                }
     10877            }
     10878        }
     10879        if ($removed > 0) {
     10880            $this->logger->info('Removed empty chunk subdirectories after incremental backup', [
     10881                'removed_count' => $removed
    1077410882            ]);
    1077510883        }
  • siteskite/tags/1.2.9/includes/Cleanup/CleanupManager.php

    r3474141 r3474147  
    451451            }
    452452           
     453            // Clean up full backup lock options (incremental and classic cron)
     454            if (delete_option('siteskite_full_backup_lock_' . $backup_id)) {
     455                $deleted_options[] = 'full_backup_lock';
     456            }
     457            if (delete_option('siteskite_full_backup_lock_' . $backup_id . '_incremental')) {
     458                $deleted_options[] = 'full_backup_lock_incremental';
     459            }
     460
    453461            // Clean up lock files if they still exist
    454462            $lock_files = [
  • siteskite/trunk/includes/Backup/BackupManager.php

    r3474141 r3474147  
    1017310173                ]);
    1017410174            }
     10175
     10176            // Clean all files under tmp folder for this backup (same as manual trigger)
     10177            try {
     10178                $this->cleanup_incremental_tmp_files($backup_id);
     10179            } catch (\Throwable $t) {
     10180                $this->logger->warning('Failed to cleanup tmp folder for incremental backup', [
     10181                    'backup_id' => $backup_id,
     10182                    'error' => $t->getMessage()
     10183                ]);
     10184            }
     10185
     10186            // Remove empty directories under chunks (same as manual trigger)
     10187            try {
     10188                $this->cleanup_empty_chunk_directories();
     10189            } catch (\Throwable $t) {
     10190                $this->logger->warning('Failed to cleanup empty chunk directories', [
     10191                    'backup_id' => $backup_id,
     10192                    'error' => $t->getMessage()
     10193                ]);
     10194            }
    1017510195           
    1017610196            // Clean up ALL external cron jobs for this backup (comprehensive cleanup)
     
    1024610266                delete_transient($completion_lock_key);
    1024710267            }
     10268
     10269            // Trigger cleanup action so tmp, DB records, and lock files are cleaned (same as manual trigger)
     10270            do_action('siteskite_cleanup_backup_files', $backup_id);
    1024810271           
    1024910272        } catch (\Exception $e) {
     
    1038210405                $deleted_options[] = 'incremental_progress';
    1038310406            }
    10384             // Some older code stored progress/last_stage with these keys
     10407            // Some older code stored progress/last_stage with these keys (ProgressTracker uses siteskite_progress_)
    1038510408            if (\delete_option('siteskite_progress_' . $backup_id)) {
    1038610409                $deleted_options[] = 'progress';
     
    1038810411            if (\delete_option('siteskite_last_stage_' . $backup_id)) {
    1038910412                $deleted_options[] = 'last_stage';
     10413            }
     10414
     10415            // Full backup lock options (used by process_full_backup_cron; sometimes stored with _incremental suffix)
     10416            $lock_keys = [
     10417                'siteskite_full_backup_lock_' . $backup_id,
     10418                'siteskite_full_backup_lock_' . $backup_id . '_incremental',
     10419            ];
     10420            foreach ($lock_keys as $lock_key) {
     10421                if (\delete_option($lock_key)) {
     10422                    $deleted_options[] = basename($lock_key);
     10423                }
    1039010424            }
    1039110425           
     
    1077210806                'backup_id' => $backup_id,
    1077310807                'error' => $t->getMessage()
     10808            ]);
     10809        }
     10810    }
     10811
     10812    /**
     10813     * Clean all temporary files under siteskite-backups/tmp for this backup (e.g. SQL dumps).
     10814     * Ensures scheduled incremental completion cleans tmp the same as manual.
     10815     */
     10816    private function cleanup_incremental_tmp_files(string $backup_id): void
     10817    {
     10818        $tmp_dir = SITESKITE_BACKUP_PATH . '/tmp';
     10819        if (!is_dir($tmp_dir)) {
     10820            return;
     10821        }
     10822        $prefix = $backup_id;
     10823        $entries = @scandir($tmp_dir);
     10824        if ($entries === false) {
     10825            return;
     10826        }
     10827        $deleted = 0;
     10828        foreach ($entries as $entry) {
     10829            if ($entry === '.' || $entry === '..') {
     10830                continue;
     10831            }
     10832            if (strpos($entry, $prefix) !== 0) {
     10833                continue;
     10834            }
     10835            $path = $tmp_dir . '/' . $entry;
     10836            if (is_file($path)) {
     10837                if (function_exists('wp_delete_file') && wp_delete_file($path)) {
     10838                    $deleted++;
     10839                } elseif (@unlink($path)) {
     10840                    $deleted++;
     10841                }
     10842            }
     10843        }
     10844        if ($deleted > 0) {
     10845            $this->logger->info('Cleaned tmp folder files for incremental backup', [
     10846                'backup_id' => $backup_id,
     10847                'deleted_count' => $deleted
     10848            ]);
     10849        }
     10850    }
     10851
     10852    /**
     10853     * Remove empty directories under siteskite-backups/chunks.
     10854     * Ensures scheduled incremental completion cleans empty chunk dirs the same as manual.
     10855     */
     10856    private function cleanup_empty_chunk_directories(): void
     10857    {
     10858        $chunks_base = SITESKITE_BACKUP_PATH . '/chunks';
     10859        if (!is_dir($chunks_base)) {
     10860            return;
     10861        }
     10862        $dirs = @scandir($chunks_base);
     10863        if ($dirs === false) {
     10864            return;
     10865        }
     10866        $removed = 0;
     10867        foreach ($dirs as $dir) {
     10868            if ($dir === '.' || $dir === '..' || !is_dir($chunks_base . '/' . $dir)) {
     10869                continue;
     10870            }
     10871            $sub_path = $chunks_base . '/' . $dir;
     10872            $files = @scandir($sub_path);
     10873            if ($files !== false && count($files) <= 2) {
     10874                if (@rmdir($sub_path)) {
     10875                    $removed++;
     10876                }
     10877            }
     10878        }
     10879        if ($removed > 0) {
     10880            $this->logger->info('Removed empty chunk subdirectories after incremental backup', [
     10881                'removed_count' => $removed
    1077410882            ]);
    1077510883        }
  • siteskite/trunk/includes/Cleanup/CleanupManager.php

    r3467625 r3474147  
    451451            }
    452452           
     453            // Clean up full backup lock options (incremental and classic cron)
     454            if (delete_option('siteskite_full_backup_lock_' . $backup_id)) {
     455                $deleted_options[] = 'full_backup_lock';
     456            }
     457            if (delete_option('siteskite_full_backup_lock_' . $backup_id . '_incremental')) {
     458                $deleted_options[] = 'full_backup_lock_incremental';
     459            }
     460
    453461            // Clean up lock files if they still exist
    454462            $lock_files = [
Note: See TracChangeset for help on using the changeset viewer.