diff --git a/lib/jira.js b/lib/jira.js index 190e2661..577b79e9 100644 --- a/lib/jira.js +++ b/lib/jira.js @@ -2139,4 +2139,90 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor }); }; + /** + * Creates a new backup of JIRA and returns its URI + * User needs to be in the administrator group to run backups and access webdav + * + * @param callback + */ + this.runBackup = function(callback) { + + //build today's backup file URI + var today = new Date().toISOString().replace(/T.+/, '').replace(/-/g,''); + var todayBackupUri = this.makeUri('webdav/backupmanager/JIRA-backup-'+today+'.zip', '', ''); + var runBackupUri = this.makeUri('/runbackup', 'rest/obm/', '1.0'); + + //prepare a HEAD request to check if today's backup exists + var optionsTodayBackup = { + uri: todayBackupUri, + method: 'HEAD', + encoding: null, + json:false + }; + + this.doRequest(optionsTodayBackup, function(error, response) { + if (error) { + callback(error, reponse); + return; + } + + if (response.statusCode === 404) { + //today's backup does not exists - we need to run the backup + options = { + rejectUnauthorized: this.strictSSL, + uri: runBackupUri, + method: 'POST', + followAllRedirects: true, + json:true, + body: { + cbAttachments: true + } + }; + + this.doRequest(options, function(error, response) { + + if (error) { + callback(error, response); + return; + } + + if (response.statusCode === 404) { + callback('Invalid URL: '+options.uri, response); + return; + } + + if (response.statusCode !== 200) { + //if another backup has been made less than 48h ago in JIRA cloud + //a 500 is returned here with an explicit body message + callback(response.statusCode + ': Unable to run backup.', response); + return; + } else { + //check that the backup does exist now + this.doRequest(optionsTodayBackup, function(error, response) { + if (error, reponse) { + callback(error); + return; + } + + if (response.statusCode === 200) { + callback(null, todayBackupUri); + return; + } else { + callback('Unable to download file: '+todayBackupUri, reponse); + return; + } + }.bind(this)); + } + }); + } else { + if (response.statusCode === 200) { + return callback(null, todayBackupUri); + } else { + callback(options.statusCode+' error received', response); + return; + } + } + }.bind(this)); + } + }).call(JiraApi.prototype);