## This Script is used to backup folder/files and delete the old backup files. ## Author: Stefanie ## Last Update Date: 07/10/2013 ## Copyright (c) Stefanie, All Rights Received. # Declare datetime for now $now = get-date # Declare today string: "20130101" $today = (get-date -UFormat %Y%m%d) # Log file path $logFilePath = "d:\VMBackup\Log_$today.txt" # Network Map Drive, Declare drive name, UserName and Password $destDrive = "f:" $User = "BackupUser" $Password = "password01!" ########################## Common Function ######################################## # Log function function AddToLog{ param ($ErrorMsg) $ErrorMsg = "$now:`r`n $ErrorMsg" Write-Output $ErrorMsg Add-Content $logFilePath $ErrorMsg } # Copy file to other place function CopyFile{ param($sourceFilePath,$destinationFolder) $net = new-object -ComObject WScript.Network $net.MapNetworkDrive($destDrive, $destinationFolder, $false, $User, $Password) Try { $destFolder = $destDrive +“\$today\" if (!(Test-Path -path $destFolder)) { New-Item $destFolder -Type Directory } copy-item -Force $sourceFilePath $destFolder } Catch { AddToLog "ERROR Copying $sourceFilePath to $destinationFolder`r`n ExceptionMsg: $_” } $net.RemoveNetworkDrive($destDrive,"true","true") } # Copy folder and subfolder to other palce function CopyFolder{ param($sourceFolder,$destinationFolder,$exclude) $net = new-object -ComObject WScript.Network $net.MapNetworkDrive($destDrive, $destinationFolder, $true, $User, $Password) Try { $destFolder = $destDrive+“\$today\" if (!(Test-Path -path $destinationFolder)) { New-Item $destFolder -Type Directory } Get-ChildItem $sourceFolder -Recurse -Exclude $exclude | % { $filePath = $_.FullName $subFolder = $filePath.Replace($sourceFolder, "") Copy-Item $filePath -Destination "$destFolder\$subFolder" -Force } } Catch { AddToLog "ERROR Copying Folder $sourceFolder to $destinationFolder`r`n ExceptionMsg: $_” } $net.RemoveNetworkDrive($destDrive,"true","true") } # Delete Old backup file function DeleteItems{ param($folderPath,[int]$remainDays) $net = new-object -ComObject WScript.Network $net.MapNetworkDrive($destDrive, $folderPath, $false, $User, $Password) Try { $dayStr = Get-Date ($now.AddDays(-$remainDays)) -UFormat "%Y%m%d" $deleteItems = Get-ChildItem "$destDrive" | Where { [int]$_.Name -le [int]$dayStr} foreach($item in $deleteItems){ Remove-Item $item.FullName -Recurse } } Catch { AddToLog "ERROR, Removing the item $folderPath`r`n ExceptionMsg: $_” } $net.RemoveNetworkDrive($destDrive,"true","true") } # Delete Old Database file # ADDB_backup_2013_07_04_010006_2927760.bak function DeleteDBItems{ param($folderPath,[int]$remainDays) $net = new-object -ComObject WScript.Network $net.MapNetworkDrive($destDrive, $folderPath, $false, $User, $Password) Try { $day = Get-Date ($now.AddDays(-$remainDays)) -UFormat "%Y%m%d" $deleteItems = Get-ChildItem "$destDrive" #| Where-Object { $_.LastWriteTime -le $day} foreach($item in $deleteItems){ $itemName = $item.Name $itemLen = $itemName.Length #Get file create day by file name. if([int]$itemLen -gt 29){ $fileDay = $itemName.Substring( $itemLen - 29,10).Replace("_","") #Compare with remain day to check if the file need to delete if($fileDay -le $day){ Remove-Item $item.FullName -Recurse #$item.FullName } } } } Catch { AddToLog "ERROR, Deleting the DB item $folderPath`r`n ExceptionMsg: $_” } $net.RemoveNetworkDrive($destDrive,"true","true") } ########################## Common Function ########################## ########################## For Debug ########################## #CopyFile "D:\VM\test\Drupal.bak" "\\192.168.1.6\BackupFolder" #CopyFolder "D:\VM\test\" "\\192.168.1.6\BackupFolder" "2.1*" #DeleteItems "\\192.168.1.6\BackupFolder" 1 #DeleteDBItems "\\192.168.1.6\BackupFolder" 1 ########################## For Debug ##########################