create and download backup per invoke-restmethod does not work

Issue #2 resolved
Former user created an issue

on the line: Invoke-RestMethod -Method Post -Uri "https://$account.atlassian.net/rest/obm/1.0/runbackup" -WebSession $session -ContentType 'application/json' -Body (@{cbAttachments = $attachments} | ConvertTo-Json -Compress) | Out-Null

i always get the error: + Invoke-RestMethod -Method Post -Uri "https://$account.atlassian.net/rest/obm/1.0 ... + ~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], We bException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

login works. do i have to change something to get it to work?

Comments (13)

  1. Mike Barmettler

    maybe not exactly the same issue - as the login works so far.

    the script stucks in an endless loop at line 22:

    $status.alternativePercentage -match "(\d+)"

    "Cannot index into a null array."

  2. Mike Barmettler

    i have some new conclusions about my issue.

    i can download the ziip file from WebDAV properly.

    but the creation part with the do-while loop seems not to work properly - see reported errors.

  3. Antony Brooke-Wood

    @mike_barmettler Regarding your endless loop: I had to completely remove the section that tries to match the percentage complete, because large files cause the percentage to go above 100% and if you don't catch it right when it says 100%, it never ends. Have a look at my code below - I just invoke the backup and then periodically try to grab it, I don't try to check the status.

    $account     = 'MyCompany'
    $username    = 'MyName'
    $password    = 'MyPassword'
    $destination = 'C:\Backups'
    $attachments = $true
    
    $today       = Get-Date -format yyyyMMdd
    $credential  = New-Object System.Management.Automation.PSCredential($username, (ConvertTo-SecureString $password -AsPlainText -Force))
    
    # Login
    Write-Host 'Logging in to Atlassian.'
    Invoke-WebRequest -Method Post -Uri "https://$account.atlassian.net/login" -SessionVariable session -Body @{username = $username; password = $password} | Out-Null
    
    # Request backup
    Write-Host 'Triggering backup.'
    Invoke-RestMethod -Method Post -Uri "https://$account.atlassian.net/wiki/rest/obm/1.0/runbackup" -WebSession $session -ContentType 'application/json' -Body (@{cbAttachments = $attachments} | ConvertTo-Json -Compress) | Out-Null
    
    # View progress for troubleshooting purposes
    # https://$account.atlassian.net/wiki/rest/obm/1.0/getprogress
    
    # Wait for backup to finish.
    Write-Host 'Waiting for backup generation to finish.'
    $retries       = 60
    $retryInterval = 60
    $retryCount    = 0
    
      while ($retryCount -lt $retries) {
          try {            
              # Download the backup.
              Invoke-WebRequest "https://$account.atlassian.net/webdav/backupmanager/Confluence-backup-$today.zip" -Credential $credential -OutFile (Join-Path -Path $destination -ChildPath "Confluence-backup-$today.zip")
              Write-Host 'Backup complete.' -ForegroundColor Green
              break
              } 
          catch {
                Start-Sleep -Seconds $retryInterval    
                $retryCount++
                $percentage = ($retryCount/$retries)*100
                Write-Progress -Activity "Waiting..." -PercentComplete $percentage
              }
          }
    
        if ($retryCount -ge $retries){
            Throw 'Timeout waiting for backup.'
        }
    
  4. Mike Barmettler

    Hi Antony,

    thank you so much for your help on this.

    i'm currently testing your script which you posted. seems like it runs good with your solution - but i'm still on it.

    but the request for creating a backup works good.

    thank you as well for the information about the current progress on this. best regards

  5. Mike Barmettler

    close to the end of the download process (about 90 %) i get a timeout:

    Timeout waiting for backup.
    +         Throw 'Timeout waiting for backup.'
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (Timeout waiting for backup.:String) [], RuntimeExcep 
       tion
        + FullyQualifiedErrorId : Timeout waiting for backup.
    
  6. Antony Brooke-Wood

    So, it could be one of two things I have found:

    1. It might be that the backup you are generating is really large and hasn't actually finished yet. To check this, go to the status page and see if it is done (https://$account.atlassian.net/wiki/rest/obm/1.0/getprogress). If this is the cause, you can just increase the timeout (either the retries or the retry interval).

    2. It might be that you are looking for the wrong file. This happens to be quite frequently and it is because the Atlassian server is set to GMT +3 for it's timezone and your server may be set to something different. As a result, depending on when the backup is triggered, the script is looking for a backup with todays date and the server has created the backup with yesterdays date. The solution here is to run the backups when both your timezone and the timezone of the server are definitely on the same day. Alternatively, you can put logic in the script that looks for the file based on the server's time zone. I'm going to do the later, but I haven't got around to it yet.

  7. Mike Barmettler

    Hi Antony,

    thank you for your inputs. the link for checking the status was very helpful.

    seems like the backup could be created in a few minutes - and i think its not that big.

    here the result:

    <backupresult concurrentBackupInProgress="false" alternativePercentage="Estimated progress: 100 %" currentStatus="Zipping export files." size="0" fileName="temp/filestore/14007381-5b94-4ff4-a622-bd572e9d615e"/>
    

    but the script does not recognize that its already finished and ready for downloading. if i check the WebDav folder, i cant see the recent created backup. It only shows up the ones which have been created manually over the UI.

    to the second point - indeed that can cause as well the issue which should be taken care about.

  8. Antony Brooke-Wood

    Hi Mike,

    The script I had above only works with the old way of backing up. You have been moved to the new method - you can tell because there is nothing in the WebDav folder and the filename references the file store. I spoke to Atlassian and you can't opt out of the new method, so you are stuck with it.

    The good news is that someone has a work around which you can read about here: https://jira.atlassian.com/browse/CONF-45015

    I'm going to modify my script to include it and I can post it back here when I get around to it if you like.

  9. Mike Barmettler

    Hi Antony,

    thanks a lot for catching me up.

    i'm currently trying the script you provided. i'll give a quick feedback on this, soon as it worked out good. furthermore i thank you as well for the info about the JIRA Cloud backup possibility. I'll try this one out too.

    thanks again and best regards Mike

  10. Mike Barmettler

    Hi Antony,

    i tried out the trick with removing the "wiki" term in the script, to get the JIRA Backup created. but it seems that its still not working. i let some time pass - i hoped that we're getting updated, to get this script to work, as you described. but it seems that its not working for me. i assume it works for you and your JIRA cloud right?

    i also posted a comment here: https://jira.atlassian.com/browse/CONF-45015

    thank you & best regards

    Mike

  11. Log in to comment