BackupBuddy: Behind the Scenes

From iThemes Codex
Revision as of 20:58, 2 November 2011 by Dustin (talk | contribs) (Migrating Database Data)
Jump to: navigation, search

Backing Up


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.

Starting Backup

  • 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).
  • start_backup_process()
    • Handles initiating the backup process and calling functions to set things up.
    • Calls pre_backup()
  • pre_backup()
    • set_greedy_script_limits()
    • 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.
    • backup_create_dat_file()
      • Creates the data file with information about the source site.
      • backupbuddy_dat.php
      • 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).
    • cron_next_step()
      • 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.
  • process_backup()
    • 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.

Backup 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...
      • exec:
        • 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.
      • PCLZip:
        • 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.

Migrating Database Data

The following lists what data in the database must be migrated for each migration path.


Standalone: A normal single site standalone installation; normal WordPress install.

Network ( is_multisite === true ): A full Multisite Network backed up from the Network admin; contains sites within.

MS Export ( is_multisite_export === true ): A single site exported from a Multisite environment.

MS Import: Bringing a standalone OR MS Export into a Multisite network.


  • Site URL
  • Conditional: Home URL if differing from Site URL AND has changed.

Standalone » Standalone

  • Nothing additional.

Multisite Network » Multisite Network (full Multisite migration)

  • Update domain & path in wp_blogs.

Standalone » Multisite Import

  • Update domain & path in wp_blogs.
  • Users tables are global with different prefix.
  • Standalone uploads path (/wp-content/uploads/) to Multisite uploads path (/wp-content/blogs.dir/##/files/)

Multisite Export » Multisite Import

  • Upload the blog ID number (##) in blogs.dir format Multisite uploads path (/wp-content/blogs.dir/##/files/)

Multisite Export » Standalone Site

  • Multisite uploads path (/wp-content/blogs.dir/##/files/) to Standalone uploads path (/wp-content/uploads/)

Additional Resources

  1. Purchase BackupBuddy
  2. PluginBuddy Tutorials
  4. Support Forums