Skip to content

Add runBackup function #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions lib/jira.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);