BackupBuddy: Behind the Scenes
This document outlines the behind the scenes processes that take place whenever a user initiates a backup.
BackupBuddy tests for the available zip methods prior to backing up by actually attempting to make a small ZIP file with a single text file in it using each zip method. This is the best way to determine is a zip method is available.
The backup process is essentially linear. It begins when the user triggers a backup or a scheduled backup occurs. The 'Starting Backup' section below is what happens in the first 'step' that happens during the page load on view_backup-run_backup-perform.php. This page is the one sent to the user's browser. Subsequent steps are handled behind the scenes in discrete PHP processes launched by an HTTP loopback connection. The user's browser periodically contacts the server via AJAX querying to the latest status of the backup. The time between updates is currently 5 seconds and may reduce in the near future to 2-3 seconds as the data transferred and load created by this is really minimal.
- User manually runs a backup via the backup page (view_backup-run_backup-perform.php) or a scheduled backup triggers via cron (an action triggers, calling backup.php). Note that all actions taken in backup.php (and any instantiated zipbuddy.php) here are logged for sending to the browser via $this->status( action, value).
- Handles initiating the backup process and calling functions to set things up.
- Calls pre_backup()
- Generates all data needed for backup process such as settings, filenames, etc. Placed in $this->_backup array.
- Array of steps/functions to call per PHP cron step are placed in $this->_backup['steps']. Ex: array( 'function' => 'function_name', 'args' => array() );
- anti_directory_browsing() on sensitive directories, backup directory.
- Creates the data file with information about the source site.
- Contains a base64 encoded serialized array of data.
- Stored in temp directory /wp-content/uploads/backupbuddy_temp/SERIAL/ (SERIAL is a 10 digit random char string).
- Schedules a cron event for this backup. Next the cron-triggered backup step will happen.
- The next steps will happen in their own unique PHP processes triggered via cron.
Cron-triggered Backup Action
- Scheduled cron activates on action pb_backupbuddy-cron_process_backup & calls the function cron_process_backup() in backupbuddy.php.
- Calls process_backup() in classes/backup.php.
- Pulls out the next function in $this->_backup['steps'] and calls with call_user_func_array().
- Runs cron_next_step() to continue on running the defined steps.
- STEP: backup_create_database_dump()
- Creates db_1.sql. In future this step should be broken up into multiple steps as needed to handle an unlimited size of databases.
- Generates list of tables, specifically including/excluding based on settings (not yet implemented in settings page so is either WP prefixed tables or ALL tables).
- Makes CREATE/INSERT/etc statements to write to SQL file, by SELECTing data and iterating.
- STEP: backup_zip_files()
- Generates exclusion list & calls (via ZipBuddy lib) the add_directory_to_zip() method.
- Uses best zip methods in order, falling back as needed...
- Builds the command line command including formatting the exclusion list into the proper format for the command line zip command.
- Reformats paths of Windows (adjusts slashes) and calls zip.exe instead of just zip.
- Calls command via exec() function.
- IF exec() returns success AND zip file exists, return true; Else fall back (continue) on to the next zip method that is available.
- Provided with WordPress in wp-admin/includes/class-pclzip.php
- Generate arguments for settings.
- call_user_func_array() on PCLZip::add.
- STEP: post_backup()
- Clean up some temp directories.
- Run trim_old_archives() to clean out any excess backups that exceed the number of backups / size limits set in the Settings.
- Run mail_notify_[manual/schedule]() to notify that a backup was triggered if applicable based on settings.
- Run wp_schedule_single_event() for calling the cleanup function in 12 hours.
- Cleans up temp directories, log files, data structure, etc.
- Runs in 12 hours to give all steps and various things a reasonable amount of time to complete.
- STEP (optional): send_remote_destination()
- Handles sending the backup to a remote destination if defnied (ex: Amazon S3, Dropbox)
- STEP (optional): post_remote_delete()
- For scheduled backups there is the option to delete the remote copy of the archive after it is sent to a remote destination.