Quantcast
Channel: Powershell – TheITBros
Viewing all articles
Browse latest Browse all 91

How to Manage Backups using PowerShell

$
0
0
windows powershell

Quite often there is a situation when the backup is performed on Windows Server by using standard tools. Of course, the size of network folder is limited and standard tools are not able to delete backups older than a certain age.

This situation can be solved by using PowerShell scripts. If you are still using the .bat scripts, just note how PowerShell scripts are easier and more logical.

You don’t need to delete everything to do backup. Just delete the folders with Backup %date% names.

# set folder path
$dump_path = "servershareidWindowsImageBackupservername"
# set min age of files
 $max_days = "-3"
# get the current date
 $curr_date = Get-Date
# determine how far back we go based on current date
 $del_date = $curr_date.AddDays($max_days)
# delete the files
 Get-ChildItem $dump_path -Recurse | Where-Object { $_.LastWriteTime -lt $del_date -and $_.Attributes -eq 'Directory' -and $_.name -like 'Backup*'} | Remove-Item -Recurse

Please note that Remove-Item requests a confirmation for automatic agreement. So we don’t need to use –Confirm:$false and/or –Force flags. In our case we should use the –Recurse flag.

If you want to delete the backups that SQL server makes, you don’t need to delete the folders. Just delete the files from the folder (it’s no sense to specify file mask, because that folder has only .bak files).

# set folder path
 $dump_path = "serversharefolder"
# set min age of files
 $max_days = "-3"
# get the current date
 $curr_date = Get-Date
# determine how far back we go based on current date
 $del_date = $curr_date.AddDays($max_days)
# delete the files
 Get-ChildItem $dump_path -Recurse | Where-Object {$_.LastWriteTime -lt $del_date} | Remove-Item -Recurse

You need to know that all backups performed by using standard tools are placed in a local folder, where they stored for some time, and then transfer to the archive.

#Path to already connected share
 $sourceFullPath = "C:MSSQLBackupdatabase"
 $targetFullPath = "serversharesqldatabase"
# copy all files
 Get-ChildItem $sourceFullPath -Recurse | Move-Item -Destination $targetFullPath

If you want to store backups in a few places, they can be copied by using the Copy-Item command. Here is an example, where only fresh backups will be copied.

#Path to already connected share
 $sourceFullPath = "serversharesqldatabase"
# set target path
 $targetServer = "server"
 $targetFullPath = "$targetServersharesqldatabase"
 $targetUsername = "username"
 $targetPassword = "password"
net use $targetServer $targetPassword /USER:$targetUsername
# get the current date
 $curr_date = Get-Date
# set min age of files
 $max_days = "-1"
# determine how far back we go based on current date
 $del_date = $curr_date.AddDays($max_days)
# copy all files
 Get-ChildItem $sourceFullPath -Recurse | Where-Object {$_.LastWriteTime -gt $del_date} | Copy-Item -Destination $targetFullPath
#Disconnect shares
 net use $targetServer /delete

You can copy Windows Server backups in the same way (the folders will be overwrite).

#Path to already connected share
 $sourceFullPath = "servershareWindowsImageBackup"
# set target path
 $targetServer = "server"
 $targetFullPath = "$targetServersharewsb"
 $targetUsername = "user"
 $targetPassword = "password"
net use $targetServer $targetPassword /USER:$targetUsername
# copy all folder
 Copy-Item $sourceFullPath $targetFullPath -Recurse -Container -ErrorAction SilentlyContinue
#Disconnect shares
 net use $targetServer /delete

To back up the Hyper-V virtual machines, you should use HVBackup.

# set folder path
 $targetFullPath = "serversharevms"
# set min age of files
 $max_days = "-2"
# get the current date
 $curr_date = Get-Date
# determine how far back we go based on current date
 $del_date = $curr_date.AddDays($max_days)
# delete the files
 Get-ChildItem $targetFullPath -Recurse | Where-Object {$_.LastWriteTime -lt $del_date} | Remove-Item -Recurse
# Run HVBackup
C:HVBackup.HVBackup.exe --a --compressionlevel 0 --output $targetFullPath 1> lastlog_out.txt 2> lastlog_err.txt

You can copy these backups to another location in the same way as SQL backups.

These and other scripts can be folded in C:\Scripts folder and executed automatically by using Set-ExecutionPolicy RemoteSigned and Task Scheduler. To use these scripts automatically just open up Action settings. Specify Powershell as a Program/script and the path to the scripts as arguments.

edit action powershell

Please note that this particular case should be used in the test environments or in the real environments of the small or very small companies. In other case you have to use special software like System Center Data Protection Manager.

The post How to Manage Backups using PowerShell appeared first on TheITBros.


Viewing all articles
Browse latest Browse all 91

Trending Articles