Skip to content

Instantly share code, notes, and snippets.

@gavinengel
Last active January 18, 2016 05:16
Show Gist options
  • Save gavinengel/2ca72704a3b30a07347a to your computer and use it in GitHub Desktop.
Save gavinengel/2ca72704a3b30a07347a to your computer and use it in GitHub Desktop.
OpenShift/WordPress daily backup script
<?php
/**
* @author [email protected]
* @version 2014-08-10-1254
* @homepage https://gist.github.com/gavinengel/2ca72704a3b30a07347a
*/
// Q: So, what is this php file?
// A: This helps you create both a files and a database dump backup every X days.
// (I don't trust WordPress backup plugins.)
// This is meant for OpenShift. Persistent folders in the "data" folder are backed up as well as repo "php/" files.
// You will need to call a url once a day to accomplish this.
// Instructions:
// 1. place this file in the data/backups folder
// 2. symlink this file to the webroot
// # ln -s ${OPENSHIFT_DATA_DIR}/backups/openshift-daily-backup.php ${OPENSHIFT_REPO_DIR}/php/daily-backup.php;
// 3. create a php file for settings:
// # vim ${OPENSHIFT_DATA_DIR}/backups/openshift-daily-backup-settings.php
// 4. call it at least once a day (every hour is fine), perhaps using ping service like http://www.monitor.us/en/website-monitoring
// # http://www.example.com/daily-backup.php?pass=foobarbaz123
// Example ./openshift-daily-backup-settings.php:
//<?php
// $password = 'foobarbaz123'; # required after '?pass=' in the cron url you call
// $period = 4; // interval between backups in days
// #$notify_email = '[email protected]'; # emails on completed backup
// #$database_name = 'ifyouneedtospecifynewdbname'; # if not set, will use OPENSHIFT_APP_NAME which is probably what you want anyway
// #$debug = true; // displays output on the webpage when calling the cron url. Don't leave this on true!
// #$force_backup = true; // remove existing backup and recreate each cron run
require(dirname(__file__).'/openshift-daily-backup-settings.php');
if (!$password) exit('Missing settings password.');
if ($password != $_REQUEST['pass']) exit('Incorrect URL password.');
// define script vars
$flag = "/tmp/daily-backup-complete.txt";
$os = get_openshift_vars();
// determine if backup should be run right now
$period = (int) $period;
if (!$period) $period = 1;
$flag_is_too_old = false;
$flag_expiration_time = filectime($flag)+(86400 * $period);
if ($flag_expiration_time < time() || $force_backup) {
$flag_is_too_old = true;
}
// Your cron should search for this string to verify it is running correctly
echo "Beginning daily backup script. ";
if ($flag_is_too_old) {
// use specified db name, or use default to $OPENSHIFT_APP_NAME
if (!$database_name) $database_name = $os['OPENSHIFT_APP_NAME'];
$date = date('d'); # 01 - 31
// remove the existing backup of the same name
run("mv {$os['OPENSHIFT_DATA_DIR']}backups/$date/ /tmp/daily-backup-".time().';', $debug);
run("mkdir -p {$os['OPENSHIFT_DATA_DIR']}backups/$date/additional/;", $debug);
// backup the app directory
$app_directory = 'php';
echo 'begin copy app directory... ';
run("cp -R {$os['OPENSHIFT_REPO_DIR']}$app_directory {$os['OPENSHIFT_DATA_DIR']}backups/$date/;", $debug);
echo 'begin zip... ';
run("zip -r {$os['OPENSHIFT_DATA_DIR']}backups/$date/$app_directory {$os['OPENSHIFT_DATA_DIR']}backups/$date/$app_directory", $debug);
run("rm -rf {$os['OPENSHIFT_DATA_DIR']}backups/$date/$app_directory;", $debug);
// backup additional directories found in the data directory & zip them
echo 'begin copy additional directories... ';
$d = dir(dirname(__file__).'/../');
while (false !== ($entry = $d->read())) {
if (in_array($entry, array('backups', '.', '..'))) continue;
run("cp -R {$os['OPENSHIFT_DATA_DIR']}$entry {$os['OPENSHIFT_DATA_DIR']}backups/$date/additional/;", $debug);
}
$d->close();
echo 'begin zip of additional... ';
run("zip -r {$os['OPENSHIFT_DATA_DIR']}backups/$date/additional {$os['OPENSHIFT_DATA_DIR']}backups/$date/additional", $debug);
run("rm -rf {$os['OPENSHIFT_DATA_DIR']}backups/$date/additional;", $debug);
// dump database
$mysqldump = "mysqldump -u {$os['OPENSHIFT_MYSQL_DB_USERNAME']} -p{$os['OPENSHIFT_MYSQL_DB_PASSWORD']} -h {$os['OPENSHIFT_MYSQL_DB_HOST']} $database_name | gzip > {$os['OPENSHIFT_DATA_DIR']}backups/$date/latest-$date.sql.gz;";
run($mysqldump, $debug);
touch($flag);
$msg = "New backup completed at: backups/$date for: ".print_r($_SERVER['SERVER_NAME'], true);
echo $msg;
if ($notify_email) mail($notify_email, 'New backup completed from: '.__file__, $msg);
} else {
// not time for a backup.
echo "Not time for a backup. ";
}
echo "Finished at: ".date('Y-m-d H:i:s');
/// functions below ///
/**
* Wrapper for running shell commands. TODO: use debug by default, remove param
*/
function run($cmd = '', $debug = false) {
if ($cmd) {
echo "\n";
if ($debug) echo '<p>'.$cmd.'</p>';
`$cmd`;
echo "\n";
}
}
/**
* Fill an array with OpenShift shell variables. Unused ones are left for reference.
*/
function get_openshift_vars() {
$os = array();
$predefined = array(
#'OPENSHIFT_APP_DNS',
#'OPENSHIFT_FRONTEND_HTTP_PLUGINS',
#'OPENSHIFT_MYSQL_LD_LIBRARY_PATH_ELEMENT',
'OPENSHIFT_APP_NAME',
#'OPENSHIFT_GEAR_DNS',
#'OPENSHIFT_NAMESPACE',
#'OPENSHIFT_APP_SSH_KEY',
#'OPENSHIFT_GEAR_MEMORY_MB',
#'OPENSHIFT_NODE_PLUGINS',
#'OPENSHIFT_APP_SSH_PUBLIC_KEY',
#'OPENSHIFT_GEAR_NAME',
#'OPENSHIFT_PHP_DIR',
#'OPENSHIFT_APP_UUID',
#'OPENSHIFT_GEAR_UUID',
#'OPENSHIFT_PHP_IDENT',
#'OPENSHIFT_AUTO_DEPLOY',
#'OPENSHIFT_HOMEDIR',
#'OPENSHIFT_PHP_IP',
#'OPENSHIFT_BROKER_HOST',
#'OPENSHIFT_HTTP_CONF_DIR',
#'OPENSHIFT_PHP_LOG_DIR',
#'OPENSHIFT_BUILD_DEPENDENCIES_DIR',
#'OPENSHIFT_KEEP_DEPLOYMENTS',
#'OPENSHIFT_PHP_PATH_ELEMENT',
#'OPENSHIFT_CARTRIDGE_SDK_BASH',
#'OPENSHIFT_LOG_DIR',
#'OPENSHIFT_PHP_PORT',
#'OPENSHIFT_CARTRIDGE_SDK_RUBY',
'OPENSHIFT_MYSQL_DB_HOST',
#'OPENSHIFT_PHP_VERSION',
#'OPENSHIFT_CLOUD_DOMAIN',
#'OPENSHIFT_MYSQL_DB_LOG_DIR',
#'OPENSHIFT_PRIMARY_CARTRIDGE_DIR',
#'OPENSHIFT_CRON_DIR',
'OPENSHIFT_MYSQL_DB_PASSWORD',
#'OPENSHIFT_PYPI_MIRROR_URL',
#'OPENSHIFT_CRON_IDENT',
'OPENSHIFT_MYSQL_DB_PORT',
'OPENSHIFT_REPO_DIR',
'OPENSHIFT_DATA_DIR',
#'OPENSHIFT_MYSQL_DB_SOCKET',
#'OPENSHIFT_RHCSH_IDLE_TIMEOUT',
#'OPENSHIFT_DEPENDENCIES_DIR',
#'OPENSHIFT_MYSQL_DB_URL',
#'OPENSHIFT_SECRET_TOKEN',
#'OPENSHIFT_DEPLOYMENT_BRANCH',
'OPENSHIFT_MYSQL_DB_USERNAME',
#'OPENSHIFT_TMP_DIR',
#'OPENSHIFT_DEPLOYMENTS_DIR',
#'OPENSHIFT_MYSQL_DIR',
#'OPENSHIFT_UMASK',
#'OPENSHIFT_DEPLOYMENT_TYPE',
#'OPENSHIFT_MYSQL_IDENT',
);
foreach ($predefined as $v) {
$os[$v] = trim(`echo \${$v}`);
}
return $os;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment