Created: 2008-07-29 02:03
Updated: 2019-02-17 17:15



This program will backup file paths and MySQL databases to Amazon's S3 storage cloud service.

I used this program in production for several years without problems. But, your results may vary.

Deprecation Notice

I am no longer maintaining this project. I have since switched to the much more robust backup/backup project. And have also created a couple simple Docker images to facilitate database backups. See: pg-dump-to-s3 and mysqldump-to-s3.

That said, please feel free to fork the code and use it however you like!


  • Simple to use
  • Backups all databases on a MySQL server
  • Backups and compresses (using bzip2) directories and files
  • Uses URL type storage keys, so it's easy to browse the backup bucket
  • (optionally) Removes old backups according to a grandfather-father-son based schedule


  1. Copy the backup.dist.php file to a new file name, like backup.php
  2. Add your Amazon AWS access key, password, and bucket in the backup.php file provided.
  3. Customize the files and database servers that are backed up.
  • You may optionally create any number of .php files inside the "backups" directory. Each one will be executed.
  1. Upload to your server.
  2. Setup a cronjob to run the backups for you!

For example, create a file called /etc/cron.daily/backup and add this code to it:

/usr/bin/php /path/to/script/backup.php    
exit 0

NOTE: Make sure to set the /etc/cron.daily/backup file to be executable. Like this:

chmod +x /etc/cron.daily/backup

About this script

This set of scripts ships with three files:

  • backup.dist.php--This is an example of how to call the backup functions
  • include/
    • the backup functions are in here
    • S3.php--This is the library to access Amazon S3

(optional) Daily backup deletion schedule

A unique feature of this script is the way it will retain (and eventually delete) old backups to conserve space, and yet maintain significant backup history. In this method you will have the following full backups:

  • Everyday for the past two weeks (14 backups)
  • Every saturday for the past 2 months (8 or 9 backups)
  • First day of the month going back forever

This allows you to keep a very detailed history of your files during the most recent time, but progressively remove backups as time goes on to save space. This feature can be turned off if you want to store all your backups, all the time.

To disable this feature, comment out the following line from


Hourly backups

You can optionally run the backups every hour. Hourly backups will be available for the last 72 hours. Beyond that, Midnight (00) hour backups will be available according to the daily backup deletion schedule outlined above.

Weekly backup deletion schedule

You can optionally run the backups each week. Weekly backups will be retained according to the following schedule:

  • Each week going back 12 weeks (~3 months) (12 backups)
  • Each month going back 36 weeks (~9 months) (9 backups)
  • One backup per year going back forever


There are several constants that may be set to change the function of the backup script. Below is a list of them.


  • awsAccessKey - string - Your Amazon Access Key. Required.
  • awsSecretKey - string - Your Amazon Secret Key. Required.
  • awsBucket - string - The name of the bucket to put backups into. Required.
  • schedule - string - One of "hourly", "daily", or "weekly". Required.


  • awsEndpoint - string - Set the endpoint URL of the server that speaks the S3 API. Default is Optional.
  • debug - boolean - Whether or not to emit debug messages. Default is false. Optional.
  • mysqlDumpOptions - string - Any options that you want to send to the mysqldump command during backups. Optional.
  • schedule - string - One of: 'weekly', 'daily', or 'hourly' - Tells the script how often you are backing up so that it can do the right thing to remove old backups.
  • timezone - string - System timezone, default is 'America/Los_Angeles', Optional.


  • PHP 5 or higher
  • PHP curl
  • PHP mysql
  • GNU/Linux environment
  • Other POSIX environments should work, but not tested
  • An AWS account with permissions to upload, list, and delete from an S3 bucket. See below.

S3 IAM Requirements

In order for the backup script to be able to communicate with S3 you will need at least the following IAM permissions:

  "Version": "2012-10-17",
  "Statement": [
      "Action": [
      "Sid": "<uniquename>",
      "Resource": [
      "Effect": "Allow"

Note that if you disable the backup deletion schedule then you will not need the s3:ListBucket or s3:DeleteObject actions above. In addition you can take out the arn:aws:s3:::<your_bucket_here> resource.

Chef cookbook

There is a Chef cookbook that will assist in setting up phpBackupS3 on your nodes:

About the author

My name is Ian. I wrote this small program to help backup my virtual machines hosted at various places. I figured someone might be able to make use of it, so I've published it as open source. I also wanted a way to store my version history, learn to use git, and do it for free. Thanks github!


This program is licensed under the MIT license. See the include/ file for a copy of the license.


  • Add ability to use S3 Reduced Redundancy Storage to save money.
Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more