Skip to content

Instantly share code, notes, and snippets.

@ijin82
Last active August 2, 2021 11:30
Show Gist options
  • Save ijin82/5dc057c144eb9fb89711e5803fed0a97 to your computer and use it in GitHub Desktop.
Save ijin82/5dc057c144eb9fb89711e5803fed0a97 to your computer and use it in GitHub Desktop.
Windows 10 - Files backup script for Windows Scheduler (+ 7zip version with folders exclude by name)
### Requires 7-zip https://www.7-zip.org/
###
### Windows scheduler task (as hidden daily job):
### Execute: powershell.exe
### Argument: -WindowStyle Hidden "L:\Backups\Backup-7z.PS1"
##########################################################
####################### Config ###########################
### 7-zip exe file path
$exe7Zip = "C:\Program Files\7-Zip\7z.exe"
### Destination folder for backups
$backupFolder = "L:\Backups"
### Current backup files copy
$backupFolderCurrent = "K:\YandexDisk\Backups_Work\Current"
### Do you want to use encryption? (1 for "yes", 0 for "no")
$backupEncryption = 1
### Password file path if needed you can change that location
$passwordFile = "L:\Backups\BackupPassword.key"
### Source folders for backups with their base filenames(!)
$foldersList = (
@{"BaseFileName"="Work"; "SourcePath"="X:\Work"}
)
### Exclude folders list (by name) from backup archive
$excludeFolders = (
"vendor",
"node_modules",
"DBDUMP",
"client_catalog_parse",
"client_catalog_temp",
".idea",
".bash",
".exe",
"storage")
##########################################################
###################### Code body #########################
### Check if 7z executable exists
if (-not (Test-Path $exe7Zip)) {
Write-Host "Required 7zip executable not found." -ForegroundColor white -BackgroundColor red
exit
}
### Check if encryption needed
$encryptionParam = ""
if ($backupEncryption) {
Write-Warning "Encryption enabled."
if (-not (Test-Path $passwordFile)) {
Write-Host "Required password file not found." -ForegroundColor white -BackgroundColor red
exit
}
$encrypted = Get-Content $passwordFile | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential("none", $encrypted)
$encryptionParam="-p" + $credential.GetNetworkCredential().Password
}
$excludeParam = ""
foreach ($excludeName in $excludeFolders) {
$excludeParam += "-xr!`"$excludeName`" "
}
$dtMark = Get-Date -f "yyyy-MM-dd HH:mm:ss"
Write-Output "Process started at: $dtMark"
$dateMark = Get-Date -f "yyyy-MM-dd"
$timeMark = Get-Date -f "HHmmss"
foreach ($folder in $foldersList) {
Write-Output "Archiving folder: $($folder.SourcePath) ..."
$dateFolder = "$backupFolder\$dateMark"
New-Item -ItemType Directory -Force -Path $dateFolder | Out-Null
$command = "&'$exe7Zip' a -tzip -mx5 $excludeParam $($encryptionParam) $dateFolder\$($folder.BaseFileName)-$($timeMark).zip $($folder.SourcePath)"
Invoke-Expression -Command $command | Out-null
#New-Item -ItemType Directory -Force -Path $backupFolderCurrent | Out-Null
#Copy-Item "$dateFolder\$($folder.BaseFileName)-$($timeMark).zip" "$backupFolderCurrent\$($folder.BaseFileName).zip"
}
$dtMark = Get-Date -f "yyyy-MM-dd HH:mm:ss"
Write-Output "Process complete at: $dtMark"
### Windows scheduler task (as hidden daily job):
### Execute: powershell.exe
### Argument: -WindowStyle Hidden "L:\Backups\Backup.PS1"
Add-Type -AssemblyName "System.IO.Compression.FileSystem"
### Destination folder for backups
$backupFolder = "L:\Backups"
### Source folders for backups with their base filenames(!)
$foldersList = (
@{"BaseFileName"="Documents"; "SourcePath"="X:\Documents"},
@{"BaseFileName"="Pictures"; "SourcePath"="X:\Pictures"},
@{"BaseFileName"="Work"; "SourcePath"="X:\Work"}
)
$dtMark = Get-Date -f "yyyy-MM-dd HH:mm:ss"
Write-Output "Process started at: $dtMark"
$dateMark = Get-Date -f "yyyy-MM-dd"
$timeMark = Get-Date -f "HHmmss"
foreach ($folder in $foldersList) {
Write-Output "Archiving folder: $($folder.SourcePath) ..."
$dateFolder = "$backupFolder\$dateMark"
New-Item -ItemType Directory -Force -Path $dateFolder | Out-Null
[IO.Compression.ZipFile]::CreateFromDirectory($folder.SourcePath, "$dateFolder\$($folder.BaseFileName)-$($timeMark).zip")
}
$dtMark = Get-Date -f "yyyy-MM-dd HH:mm:ss"
Write-Output "Process complete at: $dtMark"
### Create protected key file with password
### For usage with Backup-7z.PS1 with encryption enabled
$encrypted = Get-Content .\BackupPassword.key | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential("none", $encrypted)
echo $credential
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment