Skip to content

Instantly share code, notes, and snippets.

@cfxd
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfxd/2e8e7d060ac6dc6ed3d5 to your computer and use it in GitHub Desktop.
Save cfxd/2e8e7d060ac6dc6ed3d5 to your computer and use it in GitHub Desktop.
WordPress Database Sync with Grunt. See http://cfxdesign.com/wordpress-database-sync-with-grunt
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 remote-<%= 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 remote-<%= timestamp %>.sql remote_migrated-<%= timestamp %>.sql'
}
},
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 %>'
},
}
mysql: grunt.file.readJSON('mysql.json'),
timestamp: grunt.template.today('mm-dd-yyyy_HH-MM-ss')
grunt.registerTask('sync_local_db', [
'sshexec:dump_remote_db', //dump remote database
'exec:wget_remote_dump', //download remote dump
'sshexec:cleanup_remote', //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' //delete local database dump files
]);
{
"remote": {
"site_url": "http://example.com",
"dbname": "example_db",
"dbuser": "example_user",
"dbpass": "password",
"dbhost": "localhost",
"host": "ssh_example.com",
"username": "ssh_user",
"save_path": "/home5/ssh_user/public_html/example.com",
"save_url": "http://example.com"
},
"local": {
"site_url": "http://example.dev",
"dbname": "example_local",
"dbuser": "local_dbuser",
"dbpass": "password",
"dbhost": "localhost"
}
}
$ npm install grunt-exec grunt-peach grunt-ssh --save-dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment