Skip to content

Instantly share code, notes, and snippets.

@PaulHalliday
Forked from dansmith65/Get-TodoistBackup.ps1
Last active June 11, 2018 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulHalliday/39f6b92164ae9c8df0016509971b4f93 to your computer and use it in GitHub Desktop.
Save PaulHalliday/39f6b92164ae9c8df0016509971b4f93 to your computer and use it in GitHub Desktop.
FIX: Powershell script to download the latest backup from Todoist
# Get-TodoistBackup.ps1
# Created By: Daniel Smith dan@dansmith65.com
#
# Download the latest backup from Todoist
#
$token = ""
$headers = @{
Authorization = "Bearer $token"
}
# get list of backups from Todoist
$response = Invoke-WebRequest -Uri https://todoist.com/api/v7/backups/get -Headers $headers
$code = $response.StatusCode
If ( $code -ne "200" )
{
Write-Error "unexpected response status code: $code"
Exit
}
# extract first backup
$backups = ConvertFrom-Json $response.Content
$url = $backups[0].url
$date = Get-Date $backups[0].version -Format FileDateTime
If ( -not $url )
{
Write-Error "no backups seem to exist"
Exit
}
# specify output file path/name
$fileExtension = $url.split(".")[-1]
If ( $fileExtension.length -gt 5 -or $fileExtension.length -lt 2 )
{
Write-Host "using default file extension of zip"
$fileExtension = "zip"
}
$filePath = "$PSScriptRoot\data\$date.$fileExtension"
# create directory, if needed
$parentDir = Split-Path -Path $filePath -Parent
If ( (Test-Path $parentDir) -eq 0 )
{
Write-Host "creating directories: $parentDir"
New-Item -Path $parentDir -Type directory
}
# download file
Invoke-WebRequest -Uri $url -Method Get -OutFile $filePath -Headers $headers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment