Update '7zip_archiver_v2.ps1'
This commit is contained in:
parent
78025a917a
commit
ef86c2427f
@ -1,6 +1,6 @@
|
||||
# 7zip_archiver_v2.ps1
|
||||
# Author: Meer-Web (info@meer-web.nl)
|
||||
# Version 2.1.0
|
||||
# 7zip_to_netapp_v2.ps1
|
||||
# Author: F. Bischof (info@meer-web.nl)
|
||||
# Version 2.2.0
|
||||
|
||||
# Set global vars
|
||||
$TIMESTAMP = Get-Date -Format "yyyyMMddHHmm"
|
||||
@ -18,6 +18,7 @@ function WRITELOG {
|
||||
$LOG_TIMESTAMP = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
|
||||
$LOGMESSAGE = "${LOG_TIMESTAMP}: ${LOGSTRING}"
|
||||
Add-content $LOGFILE -value $LOGMESSAGE
|
||||
Write-Host "${LOGMESSAGE}"
|
||||
}
|
||||
WRITELOG "Archiving started"
|
||||
|
||||
@ -32,13 +33,14 @@ if (test-path $CSV) {
|
||||
}
|
||||
|
||||
# Create alias
|
||||
WRITELOG "Creating 7zip alias"
|
||||
set-alias 7z "$env:ProgramFiles\7-Zip\7z.exe"
|
||||
|
||||
# Create empty folder for robocopy to mirror over
|
||||
WRITELOG "Creating empty temp folder"
|
||||
$EMPTY_FOLDER = "$env:TEMP\emptyfolder"
|
||||
WRITELOG "Creating empty temp folder" ${EMPTY_FOLDER}
|
||||
|
||||
if (test-path $EMPTY_FOLDER) {
|
||||
WRITELOG "${EMPTY_FOLDER} already exists, purging old one."
|
||||
Remove-Item $EMPTY_FOLDER -Force -Confirm:$false -Recurse
|
||||
New-Item -ItemType Directory $EMPTY_FOLDER
|
||||
} else {
|
||||
@ -63,11 +65,13 @@ foreach ($SOURCE_ENTRY in $CSVFILE) {
|
||||
WRITELOG "Creating stage folder $TEMPFOLDER_RUNNING"
|
||||
if (!(test-path $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
|
||||
WRITELOG "Locking ACL on $SOURCE"
|
||||
Write-Output "Set ACL on $SOURCE"
|
||||
$ACL = get-acl -Path $SOURCE
|
||||
$ACL.SetAccessRuleProtection($True, $False)
|
||||
$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
|
||||
WRITELOG "Closing OpenFiles on $SOURCE"
|
||||
Write-Output "Closing open files"
|
||||
$OPENFILES = Get-SmbOpenFile | Where-Object -Property path -like *$SOURCE*
|
||||
$OPENFILES_COUNT = $OPENFILES.count
|
||||
WRITELOG "$OPENFILES_COUNT files closed"
|
||||
Write-Output "OpenFiles will be closed: $OPENFILES_COUNT"
|
||||
$OPENFILES | Close-SmbOpenFile -Force
|
||||
|
||||
# Start archiving
|
||||
WRITELOG "7ZIP $SOURCE"
|
||||
Write-Output "7ZIP $SOURCE"
|
||||
7z a -mx3 -t7z -r "$TARGET\$TARGET_FILENAME" "$SOURCE\*"
|
||||
|
||||
# Validate number of files in archive and DFS
|
||||
WRITELOG "Compare the number of files in DFS and archive"
|
||||
write-output "Compare the number of files in DFS and archive"
|
||||
## 7zip
|
||||
WRITELOG "Counting files in 7zip archive"
|
||||
$7ZIP_FILECOUNT = '0'
|
||||
@ -106,9 +106,10 @@ foreach ($SOURCE_ENTRY in $CSVFILE) {
|
||||
$dirandfilelist = Get-ChildItem -Recurse -force $SOURCE
|
||||
$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"
|
||||
if (($7ZIP_FILECOUNT -eq $DFS_FILECOUNT)) {
|
||||
if ($7ZIP_FILECOUNT -eq $DFS_FILECOUNT) {
|
||||
# Compare OK
|
||||
WRITELOG "Number of files matching! Cleaning source folder"
|
||||
Write-Output "Cleaning up $SOURCE"
|
||||
robocopy /MIR $EMPTY_FOLDER $SOURCE
|
||||
@ -119,8 +120,8 @@ foreach ($SOURCE_ENTRY in $CSVFILE) {
|
||||
Rename-Item -Path $TEMPFOLDER_RUNNING -NewName $TEMPFOLDER_DONE
|
||||
}
|
||||
} else {
|
||||
# Compare mismatch
|
||||
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)){
|
||||
WRITELOG "CRITICAL - Renaming stage folder to $TEMPFOLDER_FAILED"
|
||||
Rename-Item -Path $TEMPFOLDER_RUNNING -NewName $TEMPFOLDER_FAILED
|
||||
|
Loading…
Reference in New Issue
Block a user