Powershell Script - Grandfather-Father-Son Backup

floydtheduck

Junior Member
Jun 6, 2013
7
1
66
I'm new here and I hope not to be one of those "Post Count:3" members who is a parasite to communities like this. My friend referred me here because it's been around a while and has a lot of helpful people.
Anyhow.

My past experience has always been finding scripts (VB or, at my new job, Powershell) online and then modifying them to meet my needs. However, my current task is to setup a weekly script to do a Grandfather-Father-Son backup and Google searches have turned me up with few results. Here’s the setup:

IFS/Oracle Database (on Windows 2k8) has a hot backup every morning at 0430 to a iSCSI SAN. This SAN is then replicated across a WAN. What we would like is to retain 3 quarters, 3 month end, and 3 weeks of these backups. So, in essence, at the end of January 2013, we would have:
(3 quarters) — March 31st 2012, June 30th 2012, Sept 30th 2012
(3 months) — Oct 31st 2012, Nov 30th 2012, Dec 2012
(3 weeks) — Jan 5, 12, and 19 of 2013 (the first 3 Saturdays, as the last will likely be close to the EOM)

So, basically it is a Windows file backup that follows the schedule above, logs the success/failures and emails the results. But, being new to powershell, I'm not sure the best way to go about finding the last day of the month, etc.

Here's my current thoughts:
1. Run the GFFS backup each Saturday
2. The script will backup to a weekly folder and will delete any backups older than 3 weeks
3. If it is the last Saturday of the month, it will set the destination to a folder such as \GFFS-Backup\$month
4.Then, if a $month folder is older than 90 days and is not March, June, September, or December (end of quarter) then it will be deleted.
Using something like:
Code:
#Will be used to backup every Saturday
$today = (Get-Date)
$folderbackup = "{0}_{1}_{2}" -f $today.year,$today.month,$today.day
$pathbackup = "C:\IFS Recovery\oradata_hot\LIVE\BACKUPSET\$folderbackup\"
#Run a backup script to $pathbackup
#Will be used to archive old folders
$olddate = (Get-Date).AddDays(-28)
#using Get-ChildItem properties, see if a folder is older than $olddate and delete if true
#
#If its the end of the month, backup to the GFFS monthly folder
$Dayname = "Saturday"
$LastDayOfMonth = (Get-Date -Year (Get-Date).Year -Month (Get-Date).Month -Day 1).AddMonths(1).AddDays(-1)
If($LastDayOfMonth.DayOfWeek -eq $DayName)
{
$Answer = $LastDayOfMonth
} #End If
Else {
While($Answer -eq $Null)
{
$LastDayOfMonth = $LastDayOfMonth.AddDays(-1)
If($LastDayOfMonth.DayOfWeek -eq $DayName)
{
$Answer = $LastDayOfMonth
} #End If
}#End While
}#End Else

#Get Today's date and do some date formatting
$Date = ((get-date).ToShortDateString())
$LastSaturday = ($Answer.date)
#If Today's date is the last Saturday of the month, put the backup in the GFFS backup folder
If ($Date -ge $LastSaturday){
#run a script here to move the backup to X:\GFFS-backup\$month
} #End If

Any help is appreciated

Thanks,
Floyd
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
Because I think differently I might write the code differently. My idea would be to have three folders, weekly, monthly and quarterly. I would get the days date, check if it was quarterly through a IsQuarter function and then do the copy of the new file and delete of the old. Then do it again for isweekly and is monthly. When moving the file give it a unique filename based on the date, then loop through the files in the folder and delete the oldest one.

Code:
$today = (Get-Date)

function IsQuarter($date1){

       $LastDayOfMonth = (Get-Date -Year (Get-Date).Year -Month (Get-Date).Month -Day 1).AddMonths(1).AddDays(-1)

      if($date1.Month % 3 -eq 0){ # month mod 3 leaves 0 remainder for March 3, June 6 etc.
        if($date1.DayOfWeek -eq 6 -and $LastDayOfMonth.Subtract($date1).TotalDays -lt 6) {  #is a saturday and within 6 days of end of the month
            Return $True
        }
      }

      Return $False

}

$test = IsQuarter($today)
# write new functions for these checks
#  $testm = IsWeekly($today)
# $testw = IsMonthly($today)

if($test -eq $True){
    Write-Host "Backing up quarterly file"
    #do the file move   using [System.IO.File]::Move
}


#if($testm -eq $True){
 #   Write-Host "Backing up monthly file"
    #do the file move   using [System.IO.File]::Move
#}
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |