最近,我的一个朋友遇到了 VPS 提供商突然倒闭而没有通知的问题。不幸的是,为了便宜,她选择了一个小众提供商——一个甚至不提供任何数据下载的提供商。更糟糕的是,她甚至没有定期下载备份的习惯。一夜之间,她的网站消失了,以及上面的所有数据。
对此感到震惊,我编写了一个 Powershell 脚本,它可以定期自动将备份从您的服务器下载到您的本地计算机,而无需购买任何额外的对象存储服务或花费一分钱。您只需要一台您经常使用(并且有大量硬盘空间)并且已连接到 Internet 的 Windows 计算机。
该脚本将自动清理 5 天前的备份。您可以根据需要设置自动备份间隔和本地保存的最旧备份时间。
$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
}
}
将上面的脚本保存为 scriptname.ps1,放在您要下载备份的路径下。尝试“使用 Powershell 运行”。如果成功,则可以继续下一步。
安排任务
- 搜索“计划任务”。
- 双击“添加计划任务”。此时将出现“计划任务向导”。
- 单击“下一步”,然后单击“浏览”。此时将出现“选择要计划的程序”对话框。
- 导航到您创建的脚本,单击它,然后单击“打开”。您将返回到“计划任务向导”。
- 为任务提供一个名称,或保留默认名称(即文件名),指定运行脚本的频率,然后单击“下一步”。
- 指定开始时间和日期(如果您指定了“每天”、“每周”或“每月”等),以及重复频率,然后单击“下一步”。此项应与您的 discourse 的自动备份周期匹配
- 输入运行脚本的帐户的用户名和密码,然后单击“下一步”。
- 如果要配置高级属性,请选中复选框,然后单击“完成”。


