Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Jira Backup using REST API

Niraj Lathiya
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 12, 2023

Using below code we use to take backup for quite a long however now it's not longer working I tried reading documentations and community discussion but couldn't find subsential solution I am getting error 


Error :  You cannot call a method on a null-valued expression.
At line:56 char:9
+ $InitiateBackup = $_.Exception.Response.GetResponseStream()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull



[System.Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

if(!(Test-Path -path $destination)){
write-host "Folder is not present, creating folder"
mkdir $destination #Make the path and folder is not present
}
else{
write-host "Path is already present"
}

#Convert credentials to base64 for REST API header
function ConvertTo-Base64($string) {
$bytes = [System.Text.Encoding]::UTF8.GetBytes($string);
$encoded = [System.Convert]::ToBase64String($bytes);
return $encoded;
}

$b64 = ConvertTo-Base64($username + ":" + $token);
$auth = $b64;

$string = "cbAttachments:true, exportToCloud:true"
$stringbinary = [system.Text.Encoding]::Default.GetBytes($String) | %{[System.Convert]::ToString($_,2).PadLeft(8,'0') }

$body = @{
cbAttachments=$attachments
exportToCloud=$cloud
}
$bodyjson = $body | ConvertTo-Json

if ($PSVersionTable.PSVersion.Major -lt 4) {
throw "Script requires at least PowerShell version 4. Get it here: https://www.microsoft.com/en-us/download/details.aspx?id=40855"
}

# Create header for authentication
[string]$ContentType = "application/json"
[string]$URI = "https://$account.atlassian.net/rest/backup/1/export/runbackup"

#Create Header
$header = @{
"Authorization" = "Basic "+$auth
"Content-Type"="application/json"
}

# Request backup
try {
$InitiateBackup = Invoke-RestMethod -Method Post -Headers $header -Uri $URI -ContentType $ContentType -Body $bodyjson -Verbose | ConvertTo-Json -Compress | Out-Null
} catch {
$InitiateBackup = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($InitiateBackup)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
}

$responseBody

$GetBackupID = Invoke-WebRequest -Method Get -Headers $header https://$account.atlassian.net/rest/backup/1/export/lastTaskId
$LatestBackupID = $GetBackupID.content


# Wait for backup to finish
do {
$status = Invoke-RestMethod -Method Get -Headers $header -Uri "https://$account.atlassian.net/rest/backup/1/export/getProgress?taskId=$LatestBackupID"
$statusoutput = $status.result
$separator = ","
$option = [System.StringSplitOptions]::None
$s

if ($status.progress -match "(\d+)") {
$percentage = $Matches[1]
if ([int]$percentage -gt 100) {
$percentage = "100"
}
Write-Progress -Activity 'Creating backup' -Status $status.progress -PercentComplete $percentage
}
Start-Sleep -Seconds 5
} while($status.status -ne 'Success')

# Download
if ([bool]($status.PSObject.Properties.Name -match "failedMessage")) {
throw $status.failedMessage
}

$BackupDetails = $status.result
$BackupURI = "https://$account.atlassian.net/plugins/servlet/$BackupDetails"

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]"tls12, tls11, tls"
$i=1;
$tries=5;
$worked = $false
while((-not $worked) -and ($i -le $tries))
{
try {
write-host "Download starting"
Invoke-WebRequest -Method Get -Headers $header -WebSession $session -Uri $BackupURI -OutFile (Join-Path -Path $destination -ChildPath "JIRA-backup.zip")
write-host "Download completed"
$worked = $true
} catch [Exception] {
echo $_.Exception.GetType().FullName, $_.Exception.Message
$i=$i+1;
}
}

1 answer

0 votes
Niraj Lathiya
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 12, 2023

I've attempted to use alternative methods from here https://bitbucket.org/atlassianlabs/automatic-cloud-backup/src/master/, but I'm receiving a 401 error. I've double-checked that the username and API token are correct.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events