File Backups to Dropbox with PowerShell

I have had a number of instances where I had a virtual server somewhere, and I wanted to have a backup strategy, but the paid plans were overkill for what I needed. I have all my source code in Github, so everything can be rebuilt. I just need to be able to archive database backups and uploaded files. I decided to use a Dropbox account and set up a scheduled task that calls a PowerShell script that accesses the Dropbox web API.

Setting up Dropbox

You will need to have a Dropbox subscription set up and then you can go to the developer's section at the following link:

{% embed url="https://www.dropbox.com/developers" %}

On the main page, select create apps. On the following page, you will have the option to select the scope of the app. Here you can decide to only access a single folder or give access to all of the Dropbox functions. Either option will work, in my example, I used the full access scope, so I would have the option of putting different file types in different locations.

On your app settings page, select the permissions tab a the top and set the Files and folders permissions for files.content.write and files.content.read as shown below.

Next, go back to the settings tab and go to the generate access token section and click the button to get your token. You will also want to set the token expiration to No Expiration.

You will want to copy your access token and keep it in a safe place. If anyone gets access to the token, they will have access to the scope of your Dropbox account you have set for the app.

Server Configuration

Since we don't want someone getting access to the token somehow, we are going to place it in an environment variable on the server. After remoting into the server type the Win + R keys to open the run box and enter the following command:

rundll32.exe sysdm.cpl,EditEnvironmentVariables

Under the System variables, select New and enter DropBoxAccessToken in the Variable name field, and paste the token you copied from Dropbox into the Variable value field

Once you click OK, this variable will be available to new Powershell windows. If you have an existing Powershell window open, you will need to close it and reopen it to access the variable.

Powershell Script

The next step is to create the Powershell script on the server. Create a C:\Tasks on the server and create a file with the name DropBoxUpload.ps1 and past the following script in the file.

<#
.DESCRIPTION
This Powershell script will upload file to DropBox 
using their REST API with the parameters you provide.

.PARAMETER SourceFilePath
The path of the file to upload.
.PARAMETER TargetFilePath
The path of the file on DropBox.

.ENV PARAMETER DropBoxAccessToken
The DropBox access token.
#>
function SendToDropbox {
    
Param(
    [Parameter(Mandatory=$true)]
    [string]$SourceFilePath,
    [Parameter(Mandatory=$true)]
    [string]$TargetFilePath
)

$arg = '{ "path": "' + $TargetFilePath + '", "mode": "add", "autorename": true, "mute": false }'
$authorization = "Bearer " + (get-item env:DropBoxAccessToken).Value

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", $authorization)
$headers.Add("Dropbox-API-Arg", $arg)
$headers.Add("Content-Type", 'application/octet-stream')
 
Invoke-RestMethod -Uri https://content.dropboxapi.com/2/files/upload -Method Post -InFile $SourceFilePath -Headers $headers
}

$files = Get-ChildItem "C:\Sql\Backup" -Filter *.bak

foreach ($f in $files){
    $target = "/SvrBack/sql/Backup/" + $f.Name
    Write-Host $f.FullName
    Write-Host $target
    SendToDropbox -SourceFilePath $f.FullName -TargetFilePath $target
}

This script has a function that takes a source parameter and a target parameter and uses the environment variable where we stored our token. Then it calls the dropbox web api and uses the upload method to send the file.

Below the function, you will see the hardcoded path to the SQL backup files as well as the hardcoded destination for the target directory in Dropbox. You will want to change these to match the location of the files you are trying to copy. There is also a filter on line 37 that only gets files with a .bak extension.

Once you have the paths changed, you can execute the script from a Powershell window and test that you are able to connect and save files to your Dropbox account.

Scheduled Task

The final step is you create a scheduled task to run your script. Open the task scheduler by typing win + R and entering taskschd.msc in the box. In the right pane of the window click on Create Task.

On the General Tab, enter a Name for the task and select Run whether user is logged on or not. When you save the task, you will be required to enter the password for the user.

On the Triggers tab, you need to click the New button, and you will want to determine how often you want the backup script to run. In my example, I have it set to run every Sunday at 2 am.

On the actions tab, we will set up the execution of the script. In the Program/script box, enter powershell, and in the Add Arguments box add -File C:\Tasks\DropBoxUpload.ps1

The remaining tabs can be left at their defaults. After saving the task, you can right-click on the Task Scheduler Library item in the tree on the left side and select refresh. Now you should be able to see your task in the list. Right-click on your new task, and select Run, and your script should execute and update your Dropbox account with your latest files.

Comments

  1. You have done good work by publishing this article here. I found this article too much informative, and also it is beneficial to enhance our knowledge. Grateful to you for sharing an article like this. Dropbox transfer

    ReplyDelete

Post a Comment

Popular posts from this blog

Asp.Net Core with Extended Identity and Jwt Auth Walkthrough

Dynamic Expression Builder with EF Core