-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllDBBackup.ps1
25 lines (24 loc) · 1.25 KB
/
AllDBBackup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#VARIABLES
$path = "C:\Backups\Location\"
$Username = "ExampleUser"
$Password = ConvertTo-SecureString "ExamplePassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential($Username, $Password)
#FIND ALL SQL DATABASES ON SERVER INSTANCE
Get-SqlDatabase -ServerInstance localhost |
Where { $_.Name -ne 'tempdb' } | foreach{
#BACKS ALL DATABASES EXCLUDING TEMP TO A BACKUP LOCATION
Backup-SqlDatabase -DatabaseObject $_ -BackupFile "C:\Backups\Location\$($_.NAME)_db_$(Get-Date -UFormat %Y%m%d%H%M).bak"}
function create-7zip([String] $aDirectory, [String] $aZipfile){
[string]$pathToZipExe = "C:\Program Files\7-zip\7z.exe";
[Array]$arguments = "a", "-t7z", "$aZipfile", "$aDirectory";
& $pathToZipExe $arguments;
}
#7ZIPS DIRECTORY
create-7zip "C:\Backups\Location\*" "C:\Backups\Location\Web_$(Get-Date -UFormat %Y%m%d%H%M).7z"
#REMOVES THE .BAK FILE FROM THE DIRECTORY
get-childitem $path -include *.bak -recurse | foreach ($_) {remove-item $_.fullname}
#MOUNTS A NEW MAPPED DRIVE INSIDE OF POWERSHELL AND TRANSFERS 7ZIP TO IT
New-PSDrive –Name “K” –PSProvider FileSystem –Root “\\Final\Destination\DBs” –Credential $mycreds
Move-Item -path "$path*.7z" -Destination K:
#REMOVES THE MAPPED DRIVE
Remove-PSDrive K