I have a script that I wrote, and I'm sure that there is a better way to do it, but can't for the life of me figure it out.
Here is the problem statement: I need to backup data to a remote location, but what actually gets backed up depends on what day of the week it is. I have a program that creates a folder with the date of business as the name (yyyymmdd). Usually this happens late at night, either at 11:55 pm or later. Which means when this scripts runs, it could possibly be the next day, though we are looking for a folder with the previous day's date. Finally, if the dated folder is for a Sunday, we need to back up the previous 3 days of data, i.e. Friday, Saturday, and Sunday.
I am using mostly if
statements, but there has to be a better way to this.
Any assistance would be appreciated.
Code:
# Get today's date
$currdate = [datetime]::Today.ToString("yyyyMMdd")
# If the current day is a Monday or (i.e. last DOB is Sunday),
# that means that we have to grab the last 3 days (Friday, Saturday, and Sunday) of data
$currdayofweek = [datetime]::Today.DayOfWeek
if ( ($currdayofweek -ne 'Sunday') -or ($currdayofweek -ne 'Monday')) {
# It is not Sunday or Monday, we do not have to backup the last 3 days,
# only the current or previous day's data
if ( !(Test-Path -Path "$iber\$currdate" -PathType Container )) {
# A dated sub folder for the current day does not exist
# It is after midnight and we need to use the previous day's
# date of business
$currdate = [datetime]::Today.AddDays(-1).ToString("yyyyMMdd")
# Copy the dated sub folder, archive and settlement
copyItems -sourceInput "$iber\$currdate" -destinationInput "$uncroot\$backup\"
Copy-Item -Path "$iber\ARCHIVE\$currdate.zip" -Destination "$uncroot\$backup\ARCHIVE"
Copy-Item -Path "$iber\EDC" -Destination "$uncroot\$backup\EDC" -Filter "$currdate.stl" -Recurse
RunGenPoll -inputfolder "$iber\$currdate"
} else {
# A dated sub folder the current day does exist
# It is before midnight and we can use the current day's
# date of business
# Copy the dated sub folder, archive and settlement
copyItems -sourceInput "$iber\$currdate" -destinationInput "$uncroot\$backup\"
Copy-Item -Path "$iber\ARCHIVE\$currdate.zip" -Destination "$uncroot\$backup\ARCHIVE"
Copy-Item -Path "$iber\EDC" -Destination "$uncroot\$backup\EDC" -Filter "$currdate.stl" -Recurse
RunGenPoll -inputfolder "$iber\$currdate"
}
} elseif ($currdayofweek -eq 'Monday') {
# Day of the week is Monday
# We need to see if it is Monday morning (dob Sunday), or
# Monday evening (dob Monday)
if ( !(Test-Path -Path "$iber\$currdate" -PathType Container) ) {
# It is Monday and after midnight on Sunday
# Copy Friday, Saturday and Sunday's data
$currdate = [datetime]::Today.AddDays(-1).ToString("yyyyMMdd")
LogInfo -LogLine "Copying Archive files to $uncroot\$backup\ARCHIVE"
$archivesubs = Get-ChildItem -Path "$iber\ARCHIVE" | Where-Object { $_ -like "20*" } | Sort-Object -Descending
$archivesubs | Select-Object -Index (1..$archivenum) | Copy-Item -Destination "$uncroot\$backup\ARCHIVE" -Recurse -Force
LogInfo -LogLine "Copying dated sub folders to $uncroot\$backup"
$datedsubs = Get-ChildItem -Path "$iber"| Where-Object { $_ -like "20*" } | Sort-Object -Descending
$datedsubs | Select-Object -Index (1..$datednum) | Copy-Item -Destination "$uncroot\$backup" -Recurse
LogInfo -LogLine "Copying EDC files"
Robocopy.exe "$env:EDCPATH" "$uncroot\$backup\EDC" *.* /XF $edcexclude
# Copy the dated sub folder, archive and settlement
RunGenPoll -inputfolder "$iber\$currdate"
} else {
# It is Monday and we have a dated sub folder for Monday
# Copy the dated sub folder, archive and settlement
copyItems -sourceInput "$iber\$currdate" -destinationInput "$uncroot\$backup\"
LogInfo -LogLine "Copying Archive files to $uncroot\$backup\ARCHIVE"
Copy-Item -Path "$iber\ARCHIVE\$currdate.zip" -Destination "$uncroot\$backup\ARCHIVE"
LogInfo -LogLine "Copying EDC files"
Copy-Item -Path "$iber\EDC" -Destination "$uncroot\$backup\EDC" -Filter "$currdate.stl" -Recurse
RunGenPoll -inputfolder "$iber\$currdate"
}
} else {
# It is Sunday
# Copy data for Friday, Saturday and Sunday
LogInfo -LogLine "Copying Archive files to $uncroot\$backup\ARCHIVE"
$archivesubs = Get-ChildItem -Path "$iber\ARCHIVE" | Where-Object { $_ -like "20*" } | Sort-Object -Descending
$archivesubs | Select-Object -Index (1..$archivenum) | Copy-Item -Destination "$uncroot\$backup\ARCHIVE" -Recurse -Force
LogInfo -LogLine "Copying dated sub folders to $uncroot\$backup"
$datedsubs = Get-ChildItem -Path "$iber"| Where-Object { $_ -like "20*" } | Sort-Object -Descending
$datedsubs | Select-Object -Index (1..$datednum) | Copy-Item -Destination "$uncroot\$backup" -Recurse
LogInfo -LogLine "Copying EDC files"
Robocopy.exe "$env:EDCPATH" "$uncroot\$backup\EDC" *.* /XF $edcexclude
RunGenPoll -inputfolder "$iber\$currdate"
}
Please let me know if anyone needs any clarification on anything.
Thanks
1 Answer 1
How about checking the current time instead of checking the existence of the subfolder.
The script below has a function to back up the data of the specified date.
function Backup-Data ([string]$Path, [string]$Dest, [datetime]$Date) {
$baseName = $Date.ToString("yyyyMMdd")
Copy-Item "$Path\$baseName" $Dest -Recurse
Copy-Item "$Path\ARCHIVE\$baseName.zip" "$Dest\ARCHIVE"
Copy-Item "$Path\EDC\$baseName.stl" "$Dest\EDC"
RunGenPoll -InputFolder "$Path\$baseName"
}
$backupPath = "$uncroot\$backup"
$now = Get-Date
$targetDate = if ($now.Hour -lt 12) { $now.AddDays(-1).Date } else { $now.Date }
if ($targetDate.DayOfWeek -eq "Sunday") {
-2..0 | ForEach-Object { Backup-Data $iber $backupPath $targetDate.AddDays($_) }
}
else {
Backup-Data $iber $backupPath $targetDate
}
If you run the script in the morning, the $targetDate
will be previous day.
If $targetDate.DayofWeek
is Sunday, it will backup data from two days ago to today.