Skip to content

Instantly share code, notes, and snippets.

@mustafa0x
Last active December 14, 2015 05:40
Show Gist options
  • Save mustafa0x/5037248 to your computer and use it in GitHub Desktop.
Save mustafa0x/5037248 to your computer and use it in GitHub Desktop.
A simple script which backups up and restores a WordPress setup.
<?php
/**
* This is a simple script which backups up and resets
* a WordPress setup (or more specifically, a folder and database).
*
* Begin by installing and setuping up WordPress as you wish to freeze it.
* Once you've done so, create the backup. Make changes as you wish.
* When you wish to reset, simply visit this script and press the reset button.
*
* Note that this script assumes you know what you're doing.
* For example, placing a database dump somewhere that is accessible
* by everyone has its risks.
*/
$conf = array(
// The path to the directory you wish to backup
// Note that the backup dir is the same value, with '-back' appended.
'dir' => 'wp',
// The SQL file to backup to
'sql_file' => 'wp.sql',
// The database WordPress is using
'db' => 'wp',
// Database credentials
'db_user' => '',
'db_pass' => ''
);
if ( isset( $_GET['action'] ) ) {
if ( 'backup' == $_GET['action'] ) {
// Backup is useful for backuping up updates
// Procedure: reset, update, then backup.
shell_exec( sprintf( 'mysqldump --user=%s --password=%s %s > %s',
$conf['db_user'],
$conf['db_pass'],
$conf['db'],
$conf['sql_file'] ) );
shell_exec( sprintf( 'cp -r %s %1$s-back', $conf['dir'] ) );
}
elseif ( 'reset' == $_GET['action'] ) {
shell_exec( sprintf( 'mysql -u %s -p%s -h localhost %s < %s',
$conf['db_user'],
$conf['db_pass'],
$conf['db'],
$conf['sql_file'] ) );
shell_exec( sprintf( 'rm -rf %s', $conf['dir'] ) );
shell_exec( sprintf( 'cp -r %s-back %1$s', $conf['dir'] ) );
}
header( 'Location: ?'); // Prevent reloading the backup/reset page
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Reset WP</title>
<meta charset="utf-8">
<script type="text/javascript">
function $(id) {
return document.getElementById(id);
}
onload = function () {
$("backup").onclick = function () {
return confirm("Are you sure you wish to BACKUP the current setup?");
}
$("reset").onclick = function () {
return confirm("Are you sure you wish to RESET the current setup?");
}
};
</script>
<style type="text/css">
input {
display: block;
width: 300px;
height: 70px;
}
</style>
</head>
<body>
<form action="?" method="GET">
<input type="submit" name="action" value="reset" id="reset">
<input type="submit" name="action" value="backup" id="backup">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment