Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Single file wordpress backup script using rsync and mysqldump
#!/usr/bin/env php
<?php
/**
* Wordpress backup without any configuration
*
* USAGE:
*
* ./wordpress-backup.php path/to/basepath/of/wordpress/
*
* Will backup all files using 'rsync'
* And database using 'mysqldump'
*/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if (!isset($argv[1])) {
echo "Please specify the dir where wordpress lives\n";
exit(1);
}
$wp_dir = $argv[1];
$wp_config_file = $wp_dir . "/wp-config.php";
if (!file_exists($wp_dir)) {
echo "No wp-config.php found: $wp_config_file\n";
exit(1);
}
function wpconfigFromFile($wp_config_file)
{
$c = file_get_contents($wp_config_file);
preg_match('#define.*DB_NAME.*\'(.*)\'#', $c, $m);
$dbname = $m[1];
preg_match('#define.*DB_USER.*\'(.*)\'#', $c, $m);
$dbuser = $m[1];
preg_match('#define.*DB_PASSWORD.*\'(.*)\'#', $c, $m);
$dbpass = $m[1];
preg_match('#define.*DB_HOST.*\'(.*)\'#', $c, $m);
$dbhost = $m[1];
return [
'dbname' => $dbname,
'dbuser' => $dbuser,
'dbpass' => $dbpass,
'dbhost' => $dbhost
];
}
$wp_config = wpconfigFromFile($wp_config_file);
$mysql_command = "mysqldump --add-drop-table -h $wp_config[dbhost] -u $wp_config[dbuser] -p$wp_config[dbpass] $wp_config[dbname] > backup.sql";
$rsync_command = "rsync -az --stats --exclude='wp-content/uploads' --delete-before --progress $wp_dir backup/";
system($mysql_command);
system($rsync_command);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment