Last active
April 17, 2018 13:42
-
-
Save ryan-frankel/11238132 to your computer and use it in GitHub Desktop.
WordPress Database Backup with Amazon S3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* OPEN A STD OUT STREAM, THIS IS BETTER THEN ECHOING | |
| * BECAUSE THERE IS NO BUFFERING | |
| * *********************************************************************/ | |
| $stdout = fopen( 'php://stdout', 'w' ); | |
| // Starting Processing Designator | |
| fwrite( $stdout, "========== BACKUP ==========\n" ); | |
| /* Use the Autoload from Composer | |
| * *********************************************************************/ | |
| fwrite( $stdout, " Autoload..." ); | |
| require_once( "./vendor/autoload.php" ); | |
| fwrite( $stdout, "complete\n" ); | |
| /* Load wp-config (DB CREDENTIALS) | |
| * *********************************************************************/ | |
| fwrite( $stdout, " Loading wp-config..." ); | |
| $parse_uri = explode( 'wp-content', __FILE__ ); | |
| require_once( $parse_uri[0] . 'wp-config.php' ); | |
| fwrite( $stdout, "complete\n" ); | |
| /* CREATE A TIME STAMPED FILENAME AND THE FULL PATH TO THAT FILE | |
| * *********************************************************************/ | |
| fwrite( $stdout, " File Setup..." ); | |
| $backupfile = DB_NAME . '-' . date("Y-m-d--H-i-s") . '.sql.gz'; | |
| $backupdir = dirname(__FILE__); | |
| $backupfile_fullpath = $backupdir . '/' . $backupfile; | |
| /* EDIT THESE VARIABLES BELOW: $bucket_name and $admin_email */ | |
| $bucket_name = 'your-bucket-name'; //like ryanfrankel.com-bucket that we created | |
| $admin_email = 'your-email-address' | |
| fwrite( $stdout, "complete\n" ); | |
| /* MYSQL DUMP THE DATABASE AND GZIP (YOU CAN COMPRESS HOW YOU WANT) | |
| * *********************************************************************/ | |
| fwrite( $stdout, " MySQL Dump..." ); | |
| $command = "-u " . DB_USER . " --password='" . DB_PASSWORD . "' " . DB_NAME; | |
| system( "mysqldump $command | gzip > $backupfile_fullpath" ); | |
| fwrite( $stdout, "complete\n" ); | |
| /* CREATE AN S3 CONNECTION (CLIENT) | |
| * *********************************************************************/ | |
| fwrite( $stdout, " Creating S3 Client..." ); | |
| use Aws\S3\S3Client; | |
| /* ADD YOUR ACCESS AND SECRET KEY HERE */ | |
| $access_key = "your-access-key"; | |
| $secret_key = "your-secret-key"; | |
| $s3 = S3Client::factory( array( | |
| 'key' => $access_key, | |
| 'secret' => $secret_key | |
| ) ); | |
| fwrite( $stdout, "complete\n" ); | |
| /* WRITE THE OBJECT (FILE) TO S3 | |
| * *********************************************************************/ | |
| fwrite( $stdout, " Writing $backupfile to S3..." ); | |
| // putObject returns an exception on failure so we can use try, catch. | |
| // If there is some sort of error, send an email alerting us that there | |
| // is an issue. You could add this to steps above if you wanted. | |
| try { | |
| // This is the function that puts the file on S3 | |
| $response = $s3->putObject( array( | |
| 'Bucket' => $bucket_name, | |
| 'Key' => $backupfile, | |
| 'SourceFile' => $backupfile_fullpath | |
| ) ); | |
| fwrite( $stdout, "complete\n" ); | |
| } catch( Exception $e ) { | |
| fwrite( $stdout, "exception error...\n\n" ); | |
| // Get the error message | |
| $error_message = $e->getMessage(); | |
| // email on error | |
| fwrite( $stdout, " Emailing Admin..." ); | |
| $email_message = "BACKUP FAILURE\n==========\n$backupfile\n$error_message\n==========\n"; | |
| $email_result = mail( $admin_email, 'RyanFrankel.com: BACKUP FAILURE', $email_message ); | |
| fwrite( $stdout, "complete\n" ); | |
| } | |
| /* DONE, CLOSE THE STD OUT | |
| * *********************************************************************/ | |
| fwrite( $stdout, "========== BACKUP COMPLETE ==========\n\n" ); | |
| fclose( $stdout ); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment