Skip to content

Instantly share code, notes, and snippets.

@arberg
Forked from dansmith65/Get-TodoistBackup.ps1
Last active June 11, 2018 19:21
Show Gist options
  • Save arberg/5c905272dd3ba41d767e0907b46cd675 to your computer and use it in GitHub Desktop.
Save arberg/5c905272dd3ba41d767e0907b46cd675 to your computer and use it in GitHub Desktop.
Powershell script to download the latest backup from Todoist - With logging and win10 notifications on failure
# Get-TodoistBackup.ps1
# Created By: Daniel Smith dan@dansmith65.com
#
# Download the latest backup from Todoist
#
# https://gist.github.com/dansmith65/7a753ddb89c9db145d41b0c4b3c7fac0
# https://gist.github.com/arberg/5c905272dd3ba41d767e0907b46cd675
# Powershell Notifications: Install-Module -Name BurntToast
$useToast=(Get-Command New-BurntToastNotification -errorAction SilentlyContinue)
$logFile="$PSScriptRoot\data\backup.log"
"$(Get-Date -format "yyyy.MM.dd_HH:mm:ss") Last executed todoist fetch" | Out-File $logFile
$token = ""
$headers = @{
Authorization = "Bearer $token"
}
# get list of backups from Todoist
$response = Invoke-WebRequest -UseBasicParsing -Uri https://todoist.com/API/v7/backups/get -Headers $headers
$code = $response.StatusCode
If ( $code -ne "200" )
{
if ($useToast) {
New-BurntToastNotification -Text "Todoist backup failed", $response -AppLogo $( (Get-Item .\TodoistIcon.png).FullName)
}
(Get-Item .\TodoistIcon.png).FullName
Write-Error "unexpected response status code: $code"
"$(Get-Date -format "yyyy.MM.dd_HH:mm:ss") unexpected response status code: $code" | Out-File -a $logFile
Exit
}
$backupDir = "$PSScriptRoot\data"
$filePath = "$backupDir\$date.$fileExtension"
$backupDirFileGlob = "$backupDir\*.$fileExtension"
# 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"
"$(Get-Date -format "yyyy.MM.dd_HH:mm:ss") no backups seem to exist" | Out-File -a $logFile
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
if (Test-Path $filePath) {
"$(Get-Date -format "yyyy.MM.dd_HH:mm:ss") Completed succesfully" | Out-File -a $logFile
} else {
if ($useToast) {
New-BurntToastNotification -Text "Todoist backup failed", "Unable to download file" -AppLogo $( (Get-Item .\TodoistIcon.png).FullName)
}
"$(Get-Date -format "yyyy.MM.dd_HH:mm:ss") Failed, no file downloaded" | Out-File -a $logFile
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment