From 5064ebd304ad6527f7bf8041e34f2f381631483c Mon Sep 17 00:00:00 2001 From: stefannowak Date: Fri, 29 May 2015 15:31:08 +0200 Subject: [PATCH] added dump support --- package.json | 12 ++++---- tasks/lib/util.js | 25 ++++++++++++++++ tasks/wordpressdeploy.js | 65 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 5272ee3..99b3e60 100644 --- a/package.json +++ b/package.json @@ -1,24 +1,24 @@ { "name": "grunt-wordpress-deploy", "description": "Deploy Wordpress without pain using Grunt.", - "version": "0.0.6", - "homepage": "https://github.com/webrain/grunt-wordpress-deploy", + "version": "0.0.1", + "homepage": "https://github.com/stefnw/grunt-wordpress-deploy", "author": { "name": "Dario Ghilardi", - "email": "darioghilardi@webrain.it", - "url": "http://www.webrain.it" + "email": "info@snapz.de", + "url": "http://www.snapz.de" }, "repository": { "type": "git", "url": "git@github.com:webrain/grunt-wordpress-deploy.git" }, "bugs": { - "url": "https://github.com/webrain/grunt-wordpress-deploy/issues" + "url": "https://github.com/stefnw/grunt-wordpress-deploy/issues" }, "licenses": [ { "type": "MIT", - "url": "https://github.com/webrain/grunt-wordpress-deploy/blob/master/LICENSE-MIT" + "url": "https://github.com/stefnw/grunt-wordpress-deploy/blob/master/LICENSE-MIT" } ], "main": "Gruntfile.js", diff --git a/tasks/lib/util.js b/tasks/lib/util.js index 41ddb72..a5a4a02 100644 --- a/tasks/lib/util.js +++ b/tasks/lib/util.js @@ -42,6 +42,17 @@ exports.init = function (grunt) { grunt.log.oklns("Sync completed successfully."); }; + exports.rsync_dump = function (config) { + grunt.log.oklns("Dumping data from '" + config.from + "' to '" + config.to + "' with rsync."); + + var cmd = exports.rsync_dump_cmd(config); + grunt.log.writeln(cmd); + + shell.exec(cmd); + + grunt.log.oklns("Sync completed successfully."); + }; + exports.generate_backup_paths = function(target, task_options) { var backups_dir = task_options['backups_dir'] || "backups"; @@ -218,10 +229,24 @@ exports.init = function (grunt) { return cmd; }; + exports.rsync_dump_cmd = function (config) { + var cmd = grunt.template.process(tpls.rsync_dump, { + data: { + rsync_args: config.rsync_args, + from: config.from, + to: config.to, + exclusions: config.exclusions + } + }); + + return cmd; + }; + var tpls = { backup_path: "<%= backups_dir %>/<%= env %>/<%= date %>/<%= time %>", mysqldump: "mysqldump -h <%= host %> -u<%= user %> -p<%= pass %> <%= database %>", mysql: "mysql -h <%= host %> -u <%= user %> -p<%= pass %> <%= database %>", + rsync_dump: "rsync <%= rsync_args %> --delete <%= exclusions %> <%= from %> <%= to %>", rsync_push: "rsync <%= rsync_args %> --delete -e 'ssh <%= ssh_host %>' <%= exclusions %> <%= from %> :<%= to %>", rsync_pull: "rsync <%= rsync_args %> -e 'ssh <%= ssh_host %>' <%= exclusions %> :<%= from %> <%= to %>", ssh: "ssh <%= host %>", diff --git a/tasks/wordpressdeploy.js b/tasks/wordpressdeploy.js index c3ef240..1225cb6 100644 --- a/tasks/wordpressdeploy.js +++ b/tasks/wordpressdeploy.js @@ -13,6 +13,71 @@ var util = require('../tasks/lib/util.js').init(grunt); module.exports = function(grunt) { + /** + * DB DUMP + * dumps local database + */ + grunt.registerTask('dump_db', 'Dumping Database', function () { + var task_options = grunt.config.get('wordpressdeploy')['options']; + + var target = grunt.option('target') || task_options['target']; + + if (typeof target === "undefined" || typeof grunt.config.get('wordpressdeploy')[target] === "undefined") { + grunt.fail.warn("Invalid target specified. Did you pass the wrong argument? Please check your task configuration.", 6); + } + + // Grab the options + var target_options = grunt.config.get('wordpressdeploy')[target]; + var local_options = grunt.config.get('wordpressdeploy').local; + + // Generate required backup directories and paths + var dump_path = { + dir: target_options.path, + file: target_options.path+'_db_dump.sql' + }; + + grunt.log.subhead("Dumping database from 'Local' to '" + dump_path.dir + "'"); + + // Dump local DB + util.db_dump(local_options, dump_path); + + // Search and Replace database refs + util.db_adapt(local_options.url, target_options.url, dump_path.file); + + grunt.log.subhead("Operations completed"); + + }); + + /** + * DB FILES + * dumps local files + */ + grunt.registerTask("dump_files", "Dump files with rsync.", function () { + + var task_options = grunt.config.get('wordpressdeploy')['options']; + var target = grunt.option('target') || task_options['target']; + + if (typeof target === "undefined" || typeof grunt.config.get('wordpressdeploy')[target] === "undefined") { + grunt.fail.warn("Invalid target provided. I cannot push files from nowhere! Please checked your configuration and provide a valid target.", 6); + } + + // Grab the options + var target_options = grunt.config.get('wordpressdeploy')[target]; + var local_options = grunt.config.get('wordpressdeploy').local; + var rsync_args = util.compose_rsync_options(task_options.rsync_args); + var exclusions = util.compose_rsync_exclusions(task_options.exclusions); + + var config = { + rsync_args: task_options.rsync_args.join(' '), + from: local_options.path, + to: target_options.path, + exclusions: exclusions + }; + + util.rsync_dump(config); + + grunt.log.subhead("Operations completed"); + }); /** * DB PUSH * pushes local database to remote database