Update '7zip_archiver_v2.ps1'

This commit is contained in:
Frank Bischof 2023-03-20 11:19:08 +01:00
parent 78025a917a
commit ef86c2427f

View File

@ -1,6 +1,6 @@
# 7zip_archiver_v2.ps1 # 7zip_to_netapp_v2.ps1
# Author: Meer-Web (info@meer-web.nl) # Author: F. Bischof (info@meer-web.nl)
# Version 2.1.0 # Version 2.2.0
# Set global vars # Set global vars
$TIMESTAMP = Get-Date -Format "yyyyMMddHHmm" $TIMESTAMP = Get-Date -Format "yyyyMMddHHmm"
@ -18,6 +18,7 @@ function WRITELOG {
$LOG_TIMESTAMP = (Get-Date).toString("yyyy/MM/dd HH:mm:ss") $LOG_TIMESTAMP = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
$LOGMESSAGE = "${LOG_TIMESTAMP}: ${LOGSTRING}" $LOGMESSAGE = "${LOG_TIMESTAMP}: ${LOGSTRING}"
Add-content $LOGFILE -value $LOGMESSAGE Add-content $LOGFILE -value $LOGMESSAGE
Write-Host "${LOGMESSAGE}"
} }
WRITELOG "Archiving started" WRITELOG "Archiving started"
@ -32,13 +33,14 @@ if (test-path $CSV) {
} }
# Create alias # Create alias
WRITELOG "Creating 7zip alias"
set-alias 7z "$env:ProgramFiles\7-Zip\7z.exe" set-alias 7z "$env:ProgramFiles\7-Zip\7z.exe"
# Create empty folder for robocopy to mirror over # Create empty folder for robocopy to mirror over
WRITELOG "Creating empty temp folder"
$EMPTY_FOLDER = "$env:TEMP\emptyfolder" $EMPTY_FOLDER = "$env:TEMP\emptyfolder"
WRITELOG "Creating empty temp folder" ${EMPTY_FOLDER}
if (test-path $EMPTY_FOLDER) { if (test-path $EMPTY_FOLDER) {
WRITELOG "${EMPTY_FOLDER} already exists, purging old one."
Remove-Item $EMPTY_FOLDER -Force -Confirm:$false -Recurse Remove-Item $EMPTY_FOLDER -Force -Confirm:$false -Recurse
New-Item -ItemType Directory $EMPTY_FOLDER New-Item -ItemType Directory $EMPTY_FOLDER
} else { } else {
@ -63,11 +65,13 @@ foreach ($SOURCE_ENTRY in $CSVFILE) {
WRITELOG "Creating stage folder $TEMPFOLDER_RUNNING" WRITELOG "Creating stage folder $TEMPFOLDER_RUNNING"
if (!(test-path $TEMPFOLDER_RUNNING)){ if (!(test-path $TEMPFOLDER_RUNNING)){
New-Item -ItemType Directory $TEMPFOLDER_RUNNING New-Item -ItemType Directory $TEMPFOLDER_RUNNING
} else {
WRITELOG "$TEMPFOLDER_RUNNING already exists! Please check or delete this folder! Exiting..."
exit
} }
# Adjust ACL so that users cannot access the archived folder anymore # Adjust ACL so that users cannot access the archived folder anymore
WRITELOG "Locking ACL on $SOURCE" WRITELOG "Locking ACL on $SOURCE"
Write-Output "Set ACL on $SOURCE"
$ACL = get-acl -Path $SOURCE $ACL = get-acl -Path $SOURCE
$ACL.SetAccessRuleProtection($True, $False) $ACL.SetAccessRuleProtection($True, $False)
$LOCALADMIN_FULLCONTROL = New-Object system.security.accesscontrol.filesystemaccessrule("builtin\Administrators", "FullControl", "ContainerInherit,ObjectInherit", "none", "Allow") $LOCALADMIN_FULLCONTROL = New-Object system.security.accesscontrol.filesystemaccessrule("builtin\Administrators", "FullControl", "ContainerInherit,ObjectInherit", "none", "Allow")
@ -76,21 +80,17 @@ foreach ($SOURCE_ENTRY in $CSVFILE) {
# Close down open files # Close down open files
WRITELOG "Closing OpenFiles on $SOURCE" WRITELOG "Closing OpenFiles on $SOURCE"
Write-Output "Closing open files"
$OPENFILES = Get-SmbOpenFile | Where-Object -Property path -like *$SOURCE* $OPENFILES = Get-SmbOpenFile | Where-Object -Property path -like *$SOURCE*
$OPENFILES_COUNT = $OPENFILES.count $OPENFILES_COUNT = $OPENFILES.count
WRITELOG "$OPENFILES_COUNT files closed" WRITELOG "$OPENFILES_COUNT files closed"
Write-Output "OpenFiles will be closed: $OPENFILES_COUNT"
$OPENFILES | Close-SmbOpenFile -Force $OPENFILES | Close-SmbOpenFile -Force
# Start archiving # Start archiving
WRITELOG "7ZIP $SOURCE" WRITELOG "7ZIP $SOURCE"
Write-Output "7ZIP $SOURCE"
7z a -mx3 -t7z -r "$TARGET\$TARGET_FILENAME" "$SOURCE\*" 7z a -mx3 -t7z -r "$TARGET\$TARGET_FILENAME" "$SOURCE\*"
# Validate number of files in archive and DFS # Validate number of files in archive and DFS
WRITELOG "Compare the number of files in DFS and archive" WRITELOG "Compare the number of files in DFS and archive"
write-output "Compare the number of files in DFS and archive"
## 7zip ## 7zip
WRITELOG "Counting files in 7zip archive" WRITELOG "Counting files in 7zip archive"
$7ZIP_FILECOUNT = '0' $7ZIP_FILECOUNT = '0'
@ -106,9 +106,10 @@ foreach ($SOURCE_ENTRY in $CSVFILE) {
$dirandfilelist = Get-ChildItem -Recurse -force $SOURCE $dirandfilelist = Get-ChildItem -Recurse -force $SOURCE
$DFS_FILECOUNT = ($dirandfilelist | Where-Object { ! $_.PSIsContainer }).count $DFS_FILECOUNT = ($dirandfilelist | Where-Object { ! $_.PSIsContainer }).count
Write-Output "${SOURCE}: ${7ZIP_FILECOUNT} / ${DFS_FILECOUNT}" WRITELOG "${SOURCE}: ${7ZIP_FILECOUNT} / ${DFS_FILECOUNT}"
WRITELOG "Comparing source and target files" WRITELOG "Comparing source and target files"
if (($7ZIP_FILECOUNT -eq $DFS_FILECOUNT)) { if ($7ZIP_FILECOUNT -eq $DFS_FILECOUNT) {
# Compare OK
WRITELOG "Number of files matching! Cleaning source folder" WRITELOG "Number of files matching! Cleaning source folder"
Write-Output "Cleaning up $SOURCE" Write-Output "Cleaning up $SOURCE"
robocopy /MIR $EMPTY_FOLDER $SOURCE robocopy /MIR $EMPTY_FOLDER $SOURCE
@ -119,8 +120,8 @@ foreach ($SOURCE_ENTRY in $CSVFILE) {
Rename-Item -Path $TEMPFOLDER_RUNNING -NewName $TEMPFOLDER_DONE Rename-Item -Path $TEMPFOLDER_RUNNING -NewName $TEMPFOLDER_DONE
} }
} else { } else {
# Compare mismatch
WRITELOG "CRITICAL - Compare failed, the number of files are not matching. Counted: $7ZIP_FILECOUNT / $DFS_FILECOUNT" WRITELOG "CRITICAL - Compare failed, the number of files are not matching. Counted: $7ZIP_FILECOUNT / $DFS_FILECOUNT"
Write-Output "Skipping $SOURCE as file counts are not matching!"
if (!(test-path $TEMPFOLDER_FAILED)){ if (!(test-path $TEMPFOLDER_FAILED)){
WRITELOG "CRITICAL - Renaming stage folder to $TEMPFOLDER_FAILED" WRITELOG "CRITICAL - Renaming stage folder to $TEMPFOLDER_FAILED"
Rename-Item -Path $TEMPFOLDER_RUNNING -NewName $TEMPFOLDER_FAILED Rename-Item -Path $TEMPFOLDER_RUNNING -NewName $TEMPFOLDER_FAILED