Written on 2. January 2023

Synology Active Backup for Business – Last backup to USB drive

Here is a PowerShell script that can be used to copy the last Active Backup for Business backup from your Synology to an external USB drive:

Synology – Active Backup for Business
# Set the path to the 7-zip executable
$sevenZipExe = "C:\Program Files\7-Zip\7z.exe"

# Set the path to the USB drive
$usbDrive = "E:"

# Set the path to the location where the backup should be saved on the USB drive
$backupPath = "$usbDrive\Backups"

# Set the path to the log file
$logFile = "$backupPath\Backup_Log.txt"

# Create the backup path on the USB drive if it doesn't already exist

if (!(Test-Path -Path $backupPath))
{
    New-Item -ItemType Directory -Path $backupPath
}

# Create the log file if it doesn't already exist
if(!(Test-Path $logFile))
{
    New-Item -ItemType File -Path $logFile
}

# Set the path to the location of the folders to be backed up
$sourcePath = "\\synology\ActiveBackupData\VM-vSphere-Task-1"

# Get the newst folder in the source path
$newestFolder = Get-ChildItem -Path $sourcePath | Sort-Object LastWriteTime -Descending | Select-Object -First 1

# Set the name for the backup file
$backupFileName = "$($newestFolder.Name)_Backup_$(Get-Date -Format yyyy-MM-dd).zip"

# Write log
Add-Content -Path $logFile -Value "$(Get-Date): Starting backup process"

# Remove old folders in the backup path
Get-ChildItem -Path $backupPath -Directory | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} | Remove-Item -Recurse | Add-Content -Path $logFile

# Create a zip archive of the newst folder and save it to the USB drive
& $sevenZipExe a -tzip "$backupPath\$backupFileName" "$sourcePath\$newestFolder" | Add-Content -Path $logFile

# Confirm that the backup was created successfully and add a message to the log file
if (Test-Path -Path "$backupPath\$backupFileName")
{
    Add-Content -Path $logFile -Value "$(Get-Date): Backup of newest folder successfully saved to $backupPath\$backupFileName on USB drive"
}
else 
{
    Add-Content -Path $logFile -Value "$(Get-Date): Error: Backup of newest folder failed to save to $backupPath\$backupFileName on USB drive."
}

The script assumes that the source path is \\synology\ActiveBackupData\VM-vSphere-Task-1 and the destination path is E:\Backups. You will need to modify these paths to match your specific setup.

The script first gets a list of all backups in the source folder, then sorts them by date in descending order and selects the latest backup. It then constructs the full paths for the source and destination of the latest backup, and uses 7-Zip to archive and copy the folder/file.

If usefull you can extend the script to send you the Log-File as E-Mail:

# Set SMTP server and port
$smtpServer = "smtp.stangneth.com"
$smtpPort = 587

# Set the sender address
$sender = "backup@stangneth.com"

# Set the email address for the recipient
$recipient = "logs@stangneth.com"

# Set the subject and body of the E-Mail
#subject = "Backup Log-File"
$body = "Attached the latest backup log!"

# Send the email with log as attachement
Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -Usessl -From $sender -To $recipient -Subject $subject -Body $body -Attachments $logFile

No Comments on Synology Active Backup for Business – Last backup to USB drive

Leave a Reply

Your email address will not be published. Required fields are marked *