Skip to content

Instantly share code, notes, and snippets.

@DieselTech
Last active August 17, 2023 03:50
Show Gist options
  • Save DieselTech/54803654dc4ab0db90b6f50764b43b87 to your computer and use it in GitHub Desktop.
Save DieselTech/54803654dc4ab0db90b6f50764b43b87 to your computer and use it in GitHub Desktop.
Creates a scheduled task to backup mylar3 using robocopy.
##############
# Mylar Backup using built-in robocopy. This creates 2 files, MylarRobocopyBackup.bat and MylarRobocopyBackup.ps1. The bat file is just used to call the .ps1 due to task scheduler not liking native .ps1 fiels somehow in 2023.
# The .ps1 file will stop the docker mylar3 container and then run robocopy on the db and ini files, then start the container back up.
# Dump the backup portion of the script.
$CreatePowershell = @'
# Setting variables - Change these to your mylar folder and where you want the backups to go to.
$mylarSource= "D:\Projects\Docker\mylar\mylar"
$mylarBackup= "D:\Projects\Backups\Mylar"
# Nothing to change below this line
#_____________________________________________
$starttime= get-date
$endtime = get-date
$duration = ($endtime - $starttime)
docker stop mylar3
robocopy $mylarSource $mylarBackup *.db *.ini
if ($lastexitcode -eq 0)
{write-eventlog -logname "MylarBackup" -source Robocopy -eventID 0 -entrytype Warning -message "Robocopy found nothing to copy. This is probably bad. The job took $duration" -category 1 -rawdata 10,20}
elseif ($lastexitcode -eq 1)
{write-eventlog -logname "MylarBackup" -source Robocopy -eventID 1 -entrytype Information -message "Successful Backup. The job took $duration" -category 1 -rawdata 10,20}
elseif ($lastexitcode -eq 2)
{write-eventlog -logname "MylarBackup" -source Robocopy -eventID 2 -entrytype Warning -message "Unsucessful Backup. The job took $duration" -category 1 -rawdata 10,20}
elseif ($lastexitcode -eq 8)
{write-eventlog -logname "MylarBackup" -source Robocopy -eventID 8 -entrytype Warning -message "Robocopy has ran into errors. This is very bad. Copy errors occurred and the retry limit was exceeded. The job took $duration" -category 1 -rawdata 10,20}
elseif ($lastexitcode -eq 16)
{write-eventlog -logname "MylarBackup" -source Robocopy -eventID 666 -entrytype Warning -message "Unsucessful Backup. This shouldn't happen. The job took $duration" -category 1 -rawdata 10,20}
else
{write-eventlog -logname "MylarBackup" -source Robocopy -eventID 10 -entrytype Warning -message "Robocopy has finished a job with an unknown exit code. The job took $duration" -category 1 -rawdata 10,20}
docker start mylar3
'@
$CreateBatch = @'
powershell.exe -ExecutionPolicy Bypass -File "%userprofile%\.mylarbackupscript\MylarRobocopyBackup.ps1"
'@
# Create the folder for the script to live in.
If ( !( Test-Path -Path "~\.mylarbackupscript\" ) ) { New-Item -Path "~\.mylarbackupscript\" -ItemType Directory 2>&1 | Out-Null }
# It goes to it's home.
Set-Content -Path "~\.mylarbackupscript\MylarRobocopyBackup.ps1" -Value $CreatePowershell
Set-Content -Path "~\.mylarbackupscript\MylarRobocopyBackup.bat" -Value $CreateBatch
# Building the scheduled task.
$action = New-ScheduledTaskAction -Execute '"%userprofile%\.mylarbackupscript\MylarRobocopyBackup.bat'
$trigger = New-ScheduledTaskTrigger -Daily -At 4am
# Putting together the scheduled task
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "MylarBackup" -Description "Daily backup of mylar3 using robocopy."
# Raising to admin console to create the eventlog receiver.
New-EventLog -source Robocopy -Logname "MylarBackup"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment