Last active
July 5, 2021 10:38
-
-
Save diversen/f18e63e0f9052f3bb127c0b3331ddf12 to your computer and use it in GitHub Desktop.
Single file wordpress backup script using rsync and mysqldump
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
| #!/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' | |
| * Backup will be placed in e.g: | |
| * backup/wordpress_live_dir | |
| * | |
| * Database backup is created using 'mysqldump'. | |
| * It gets the following name: | |
| * backup/Y-m-d-database_name-backup.sql | |
| * | |
| */ | |
| 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); | |
| $date = date("Y-m-d"); | |
| $rsync_command = "rsync -az --stats --exclude='video' --exclude='wp-content/uploads' --delete-before --progress $wp_dir backup/"; | |
| $mysql_command = "mysqldump --add-drop-table -h $wp_config[dbhost] -u $wp_config[dbuser] -p$wp_config[dbpass] $wp_config[dbname] > ./backup/$date-$wp_config[dbname]-backup.sql"; | |
| system($rsync_command); | |
| system($mysql_command); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment