Skip to content

Instantly share code, notes, and snippets.

@cfxd
Created November 10, 2014 03:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfxd/1286bd0da6d267bda50f to your computer and use it in GitHub Desktop.
Save cfxd/1286bd0da6d267bda50f to your computer and use it in GitHub Desktop.
WordPress Database Sync (both ways) with Grunt. See http://cfxdesign.com/wordpress-database-sync-with-grunt#update
grunt.initConfig({
mysql: grunt.file.readJSON('mysql.json'),
timestamp: grunt.template.today('mm-dd-yyyy_HH-MM-ss'),
sshexec: {
dump_remote_db: {
options: {
host: '<%= mysql.remote.host %>',
username: '<%= mysql.remote.username %>',
agent: process.env.SSH_AUTH_SOCK
},
command: [
'cd <%= mysql.remote.save_path %>',
'mysqldump <%= mysql.remote.dbname %> -u <%= mysql.remote.dbuser %> -p<%= mysql.remote.dbpass %> > remote-<%= timestamp %>.sql'
].join(' && ')
},
cleanup_remote: {
options: {
host: '<%= mysql.remote.host %>',
username: '<%= mysql.remote.username %>',
agent: process.env.SSH_AUTH_SOCK
},
command: [
'cd <%= mysql.remote.save_path %>',
'rm local_migrated-<%= timestamp %>.sql'
].join(' && ')
},
cleanup_remote_dump: {
options: {
host: '<%= mysql.remote.host %>',
username: '<%= mysql.remote.username %>',
agent: process.env.SSH_AUTH_SOCK
},
command: [
'cd <%= mysql.remote.save_path %>',
'rm remote-<%= timestamp %>.sql'
].join(' && ')
},
import_migrated_local_dump: {
options: {
host: '<%= mysql.remote.host %>',
username: '<%= mysql.remote.username %>',
agent: process.env.SSH_AUTH_SOCK
},
command: [
'cd <%= mysql.remote.save_path %>',
'mysql -u <%= mysql.remote.dbuser %> -p<%= mysql.remote.dbpass %> <%= mysql.remote.dbname %> < local_migrated-<%= timestamp %>.sql'
].join(' && ')
},
},
exec: {
wget_remote_dump: {
command: 'wget -nv <%= mysql.remote.save_url %>/remote-<%= timestamp %>.sql'
},
import_migrated_remote_dump: {
command: 'mysql -u <%= mysql.local.dbuser %> -p<%= mysql.local.dbpass %> <%= mysql.local.dbname %> < remote_migrated-<%= timestamp %>.sql'
},
cleanup_local: {
command: 'rm -rf <%= mysql.local.dump_dir %>/local_migrated-<%= timestamp %>.sql'
},
cleanup_local_from_remote: {
command: 'rm -rf remote-<%= timestamp %>.sql remote_migrated-<%= timestamp %>.sql'
},
dump_local_db: {
command: 'mysqldump -u <%= mysql.local.dbuser %> -p<%= mysql.local.dbpass %> <%= mysql.local.dbname %> > <%= mysql.local.dump_dir %>local-<%= timestamp %>.sql'
},
scp_local_dump: {
command: 'scp <%= mysql.local.dump_dir %>local_migrated-<%= timestamp %>.sql <%= mysql.remote.username %>@<%= mysql.remote.host %>:<%= mysql.remote.save_path %>'
}
},
peach: {
search_replace_remote_dump: {
options: {
force: true
},
src: 'remote-<%= timestamp %>.sql',
dest: 'remote_migrated-<%= timestamp %>.sql',
from: '<%= mysql.remote.site_url %>',
to: '<%= mysql.local.site_url %>'
},
search_replace_local_dump: {
options: {
force: true
},
src: '<%= mysql.local.dump_dir_theme_relative %>local-<%= timestamp %>.sql',
dest: '<%= mysql.local.dump_dir_theme_relative %>local_migrated-<%= timestamp %>.sql',
from: '<%= mysql.local.site_url %>',
to: '<%= mysql.remote.site_url %>'
}
}
});
grunt.registerTask('sync_local_db', [
'sshexec:dump_remote_db', //dump remote database
'exec:wget_remote_dump', //download remote dump
'sshexec:cleanup_remote_dump', //delete remote dump
'peach:search_replace_remote_dump', //search and replace URLs in database
'exec:import_migrated_remote_dump', //import the migrated database
'exec:cleanup_local_from_remote' //delete local database dump files
]);
grunt.registerTask('sync_remote_db', [
'exec:dump_local_db', //dump local database
'peach:search_replace_local_dump', //search and replace URLs in database
'exec:scp_local_dump', //upload local dump
'exec:cleanup_local', //delete local database dump files
'sshexec:import_migrated_local_dump', //import the migrated database
'sshexec:cleanup_remote' //delete remote database dump file
]);
{
"remote": {
"host": "example.com",
"site_url": "http://example.com",
"username": "ssh_login",
"dbname": "example_db",
"dbuser": "example_user",
"dbpass": "password",
"dbhost": "localhost",
"save_path": "/home5/ssh_login/public_html/example.com/wp-content/themes/your-theme/peach",
"save_url": "http://example.com/wp-content/themes/your-theme/peach",
"upload_path": "/home5/ssh_login/public_html/example.com/wp-content/themes/your-theme/peach",
},
"local": {
"site_url": "http://example.dev",
"dbname": "example_local",
"dbuser": "local_db_user",
"dbpass": "password",
"dbhost": "localhost",
"dump_dir": "~/Sites/example.com/wp/wp-content/themes/your-theme/peach/",
"dump_dir_theme_relative": "peach/"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment