Recently, a friend of mine encountered the problem of the VPS provider suddenly going out of business without notice. Unfortunately, for the sake of cheap, she chose a niche provider - one that didn’t even offer any data downloads. What’s worse is that she doesn’t even have the habit of downloading backups regularly. Overnight, her site disappeared, along with all the data on it.
Shocked by this, I wrote a Powershell script that will automatically download backups from your server to your local computer regularly without having to purchase any additional object storage services or spend a penny on other things. All you need is a Windows computer that you use regularly (and has plenty of hard drive space) and is connected to the Internet.
The script will automatically clean up backups from 5 days ago. You can set the automatic backup interval and the oldest backup time saved locally according to your needs.
$ssh_port = 22
$ssh_address = "username@your.site"
Write-Output "Starting Discourse backup download task..."
Write-Output '------------------------'
Write-Output "Fetching the latest backup file..."
Write-Output ''
while ($true) {
$filename = ''
while ($true) {
try {
Write-Output "> ssh -p $ssh_port $ssh_address 'cd /var/discourse/shared/standalone/backups/default/ && ls -t | head -n 1'"
Write-Output ''
$filename = ssh -p $ssh_port "$ssh_address" 'cd /var/discourse/shared/standalone/backups/default/ && ls -t | head -n 1'
break
}
catch {
$filename = ''
Write-Output "Failed to fetch... Here is the log:"
Write-Output '-------------'
Write-Output $_
$answer = Read-Host "Do you want to re-fetch? (y/N)"
if ($answer -ne 'y') {
break
}
Write-Output ''
}
}
if ([String]::IsNullOrEmpty($filename)) {
Write-Output "Error: Failed to fetch file name $filename"
Write-Output ''
$answer = Read-Host 'Retry?(y/N)'
if ($answer -eq 'y') {
}
else {
exit 1
}
}
else {
Write-Output "Latest backup: $filename"
Write-Output ''
$need_download = $true
if (Test-Path ".\backups\$filename") {
$answer = Read-Host ".\backups\$filename already exists. Do you want to download it again?(y/N)"
Write-Output ''
if ($answer -ne 'y') {
$need_download = $false
}
}
if ($need_download) {
Write-Output "Start downloading..."
Write-Output ''
while ($true) {
try {
Write-Output "scp -p $ssh_port ${ssh_address}:/var/discourse/shared/standalone/backups/default/$filename .\backups\"
Write-Output ''
scp -p $ssh_port "${ssh_address}:/var/discourse/shared/standalone/backups/default/$filename" .\backups\
Write-Output "Download completed"
Write-Output ''
break
}
catch {
Write-Output "Download failed >_<... The following is the log:"
Write-Output ''
Write-Output $_
$answer = Read-Host "Download again? (y/N)"
Write-Output ''
if ($answer -ne 'y') {
break
}
}
}
}
Write-Output "Start trying to clean old backup files..."
Write-Output ''
$count = 0
$backupfiles = Get-ChildItem -Path .\backups\
foreach ($file in $backupfiles) {
if ($file.CreationTime -le (Get-Date).AddDays(-5)) {
try {
Write-Output "Delete old backup file $file ..."
Write-Output ''
$file.Delete()
$count = $count + 1
} catch {
Write-Output "An error occurred while deleting old backup file $file >_<"
Write-Output '-------------------'
Write-Output $_
Write-Output '-------------------'
}
}
}
if ($count -ge 0) {
Write-Output "Cleaned $count old backup files"
Write-Output ''
}
else {
Write-Output 'No old backup files need to clean up'
Write-Output ''
}
Pause
exit 0
}
}
Save the above script as scriptname.ps1
in the path where you wish to download the backup. Try “Run with Powershell”. If successful, then you can proceed to the next step.
To schedule a task
- Search “Scheduled Tasks”.
- Double-click Add Scheduled Task. The Scheduled Task Wizard appears.
- Click Next, then click Browse. The Select Program to Schedule dialog appears.
- Navigate to the script that you created click it, then Open. You are returned to the Scheduled Task Wizard.
- Provide a name for the task, or keep the default, which is the filename, specify how often to run the script, then click Next.
- Specify the starting time and date (if you specified Daily, Weekly, or Monthly, or etc.) and recurrence, then click Next. This item should match the automatic backup cycle of your discourse
- Type the user name and password for the account that will run the script, then click Next.
- If you want to configure advanced properties, select the check box, then click Finish.