windows powershell

How to Manage Backups using 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.

READ ALSO  IE10 Doesn't Work With Microsoft Dynamics CRM 4.0
#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.

READ ALSO  Clean Out Temporary Outlook Files via PowerShell

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.


You may also like:

Grant Full Access to All Mailboxes in Exchange 200... This is a short tutorial on how to add full access to all mailboxes in Exchange 2007 and 2010 for a an additional user. This can come in handy when sa...
Add Calendar Permissions in Office 365 via Powersh... This is a tutorial on adding calendar permissions in Office 365 for your users via Powershell. You can add permissions onto a specific mailbox, or you...
How to copy files with BITS using PowerShell If you use local (and global) networks, you might know that files between systems are transferring by using SMB, FTP or HTTP protocols. The problem wi...
How to Backup and Restore Group Policy Objects Group policies have an important role in the Active Directory domain management, their damage or accidental deletion can lead to unpredictable results...
Manage Start Screen with Group Policy in Windows 1... Hello guys! In this tutorial we will show you how to export an existing start screen for Windows 10 and then use the Group Policy to ensure those sett...