· 7 years ago · Apr 24, 2018, 11:44 PM
1<?php
2/* OPEN A STD OUT STREAM, THIS IS BETTER THEN ECHOING
3 * BECAUSE THERE IS NO BUFFERING
4 * *********************************************************************/
5$stdout = fopen( 'php://stdout', 'w' );
6
7// Starting Processing Designator
8fwrite( $stdout, "========== BACKUP ==========\n" );
9
10/* Use the Autoload from Composer
11 * *********************************************************************/
12fwrite( $stdout, " Autoload..." );
13 require_once( "./vendor/autoload.php" );
14fwrite( $stdout, "complete\n" );
15
16/* Load wp-config (DB CREDENTIALS)
17 * *********************************************************************/
18fwrite( $stdout, " Loading wp-config..." );
19 $parse_uri = explode( 'wp-content', __FILE__ );
20 require_once( $parse_uri[0] . 'wp-config.php' );
21fwrite( $stdout, "complete\n" );
22
23/* CREATE A TIME STAMPED FILENAME AND THE FULL PATH TO THAT FILE
24 * *********************************************************************/
25fwrite( $stdout, " File Setup..." );
26 $backupfile = DB_NAME . '-' . date("Y-m-d--H-i-s") . '.sql.gz';
27 $backupdir = dirname(__FILE__);
28 $backupfile_fullpath = $backupdir . '/' . $backupfile;
29 /* EDIT THESE VARIABLES BELOW: $bucket_name and $admin_email */
30 $bucket_name = 'your-bucket-name'; //like ryanfrankel.com-bucket that we created
31 $admin_email = 'your-email-address'
32fwrite( $stdout, "complete\n" );
33
34/* MYSQL DUMP THE DATABASE AND GZIP (YOU CAN COMPRESS HOW YOU WANT)
35 * *********************************************************************/
36fwrite( $stdout, " MySQL Dump..." );
37 $command = "-u " . DB_USER . " --password='" . DB_PASSWORD . "' " . DB_NAME;
38 system( "mysqldump $command | gzip > $backupfile_fullpath" );
39fwrite( $stdout, "complete\n" );
40
41/* CREATE AN S3 CONNECTION (CLIENT)
42 * *********************************************************************/
43fwrite( $stdout, " Creating S3 Client..." );
44 use Aws\S3\S3Client;
45 /* ADD YOUR ACCESS AND SECRET KEY HERE */
46 $access_key = "your-access-key";
47 $secret_key = "your-secret-key";
48 $s3 = S3Client::factory( array(
49 'key' => $access_key,
50 'secret' => $secret_key
51 ) );
52fwrite( $stdout, "complete\n" );
53
54/* WRITE THE OBJECT (FILE) TO S3
55 * *********************************************************************/
56fwrite( $stdout, " Writing $backupfile to S3..." );
57// putObject returns an exception on failure so we can use try, catch.
58// If there is some sort of error, send an email alerting us that there
59// is an issue. You could add this to steps above if you wanted.
60try {
61 // This is the function that puts the file on S3
62 $response = $s3->putObject( array(
63 'Bucket' => $bucket_name,
64 'Key' => $backupfile,
65 'SourceFile' => $backupfile_fullpath
66 ) );
67 fwrite( $stdout, "complete\n" );
68} catch( Exception $e ) {
69 fwrite( $stdout, "exception error...\n\n" );
70 // Get the error message
71 $error_message = $e->getMessage();
72
73 // email on error
74 fwrite( $stdout, " Emailing Admin..." );
75 $email_message = "BACKUP FAILURE\n==========\n$backupfile\n$error_message\n==========\n";
76 $email_result = mail( $admin_email, 'RyanFrankel.com: BACKUP FAILURE', $email_message );
77 fwrite( $stdout, "complete\n" );
78}
79
80/* DONE, CLOSE THE STD OUT
81 * *********************************************************************/
82fwrite( $stdout, "========== BACKUP COMPLETE ==========\n\n" );
83fclose( $stdout );
84?>