Last active
January 30, 2022 15:09
-
-
Save marshyon/b73badf14469b284dbdf5e7e03f5805c to your computer and use it in GitHub Desktop.
script that runs forever and creates backup files based on time of day
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $timeItems = @("09", "13", "18", "22") | |
| Write-Host "starting and waiting for hours [" $timeItems "]" | |
| function New-Backup { | |
| $date = Get-Date | |
| $currentHour = Get-Date -Format HH | |
| $continue = 0 | |
| foreach ($t in $timeItems) { | |
| if ( $t -eq $currentHour ) { | |
| $continue = 1 | |
| } | |
| } | |
| if ($continue -eq 0 ) { | |
| return | |
| } | |
| $dateString = $date.tostring("yyyy-MM-dd-hh") | |
| $newBackupDir = $PSScriptRoot + "\backups-" + $dateString | |
| $newBackupZip = $PSScriptRoot + "\archive-" + $dateString + ".zip" | |
| if ( -Not ( Test-Path $newBackupDir ) ) { | |
| Write-Host "creating a directory ... ["$PSScriptRoot "] "$newBackupDir " " $newBackupZip | |
| $nDir = New-Item -ItemType Directory -Path $newBackupDir -Force | |
| if ( $nDir.Exists ) { | |
| Write-Host "created ok..." | |
| } | |
| else { | |
| Write-Host "Error creating directory " $Error | |
| Exit | |
| } | |
| Compress-Archive -Path $PSScriptRoot -DestinationPath $newBackupZip | |
| # encrypt / decrypt with | |
| # https://4sysops.com/archives/encrypt-and-decrypt-files-with-powershell-and-pgp/ | |
| # Add-Encryption -FolderPath C:\Users\USER\projects\powershell\important\ -Password VERYSECRET | |
| # Remove-Encryption -FolderPath C:\Users\USER\projects\powershell\important\ -Password VERYSECRET -Verbose | |
| # create a windows link with | |
| # powershell.exe -command "& 'C:\Users\USER\projects\powershell\make_dir_datestr.ps1' -MyArguments blah" | |
| # script creation of the link with | |
| # $WshShell = New-Object -comObject WScript.Shell | |
| # $Shortcut = $WshShell.CreateShortcut("C:\Users\USER\projects\powershell\RunBackupScript.lnk") | |
| # $Shortcut.TargetPath = "powershell.exe" | |
| # $Shortcut.Arguments = "-command `"& 'C:\Users\USER\projects\powershell\make_dir_datestr.ps1' -MyArguments blah`"" | |
| # $Shortcut.Save() | |
| } | |
| } | |
| while ($true) { | |
| New-Backup | |
| Start-Sleep -Seconds 30 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment