Wednesday 24 April 2019

Archiving files and folders using powershell

So I work for a company with many 100s of terabytes of files on our file servers (if I counted our SQL, Workstation and Avid environments I would potentially weep but ignorance is not bliss and due to our Mac and Avid environment I have worked on this solution), so every once in a while we need to archive off recent jobs (a successful campaign for example).

Because of this I wrote a dirty powershell script (a Linux shell script is available which I wrote for a previous employer but I am appeasing the masses for this one) where the script looks for a folder naming convention (in this case ARC_ at the beginning) and moves this data to the archive location. The below will run on a local file server ( I have it deployed as a file server role in a Windows Cluster Environment but an easy rewrite to meet UNC paths instead of local drives is an easy task).

######

# A little script to archive all folders with "ARC_" at the beginning of the folder to a new location

# USAGE:
#
# Specify the directory to search in using the sourceDir (source folder) and targetDir (target Directory)you want to copy to. Make sure they are in quotes, especially if they have spaces
# For example:
#    'C:\TEMP\Mikes Porn' - is GOOD!
#    C:\TEMP\Mikes Porn  - is BAD!
#
# By Mike Donaldson (tekctrl@gmail.com) 
#
######

#Specifiy the source and target folders

$Client = Read-Host "Which client do you want to archive? (The name you enter has to match the client folder name)"
$sourceDir = '<production share letter>:\Shares\<share name>\' + $Client
$targetDir = '<archive share letter>:\Shares\<share name>\' + $Client

#Find all folders that have ARC_ at the beginning of their name. 
Get-ChildItem $sourceDir -filter "ARC_*" -recurse -Attributes Directory |
#For each folder found run through and create a new item and move the content over. Due to the renaming and removing of the ARC_ the original folder is left behind so a remove has to be run at the end.
    foreach{
        $targetFile = $targetDir + $_.FullName.SubString($sourceDir.Length);
$targetFile = $targetFile -replace "ARC_","";
$sourceFile = $_.FullName
echo  $sourceFile
        New-Item -ItemType Directory -Path $targetFile -Force;
robocopy.exe $sourceFile $targetFile /COPY:DAT /E /MOVE /R:1 /W:1
$targetFile += "`r`n"
$reportLocation += $sourceFile, "has been moved to", $targetFile
}
#Send the email out informing IT what has been archived
$body = "The following folders have been archived:`r" + $reportLocation
Send-MailMessage -From "it.systems@whopaysthebills.com" -To "whogetspaid@whopaysthebills.com" -Subject "Clients Archive" -Body $body -SmtpServer "willyouletme.thiswontwork"
echo "Archiving has completed! Now bugger off to the pub and have a well earnt Appletini"

Now obviously you can drink whatever you want (this script was written many years ago when Scrubs was my main TV viewing) but once this data is moved to another drive, it still exists! For a few businesses I have assisted, this simply moved the data from A to B but still required storing, backing up and hosting. All of which does not remove the headache. So an extra step is required.

Once this data is backed up to offsite (and offline to air gap any ransomware) then we can remove the content and leave the file name behind, making it easy for anyone to ask for the file back (they have the file path and original file name after all as they can see it visually in the Archive location via SMB or AFP etc) and we once again run a separate powershell script that will clear the content ( I have always found its never best to run these two scripts as one due to the damage they can cause) and save you the space of hosting many terabytes over many years of a successful career in IT (competition breads innovation after all!). You will obviously need to rewrite some aspects such as the target location (Z:\ may not be your favorite choice of an archive drive letter) and you may have a more flat file structure and therefore not require a client variable.

######

# A little script to wipe content from all data from the folder you specify...be careful!

# USAGE:
#
# Specify the client name as listed on the file server. If it does not match, then you could wipe many #terabytes of data you have yet to keep offline, elsewhere. If that happens then it's your fault for #randomly running a script you found on the internet without understanding what they are capable of. # I will always put comments in my scripts to help everyone understand my random typing. It is the  #true sign of a mad man after all and if you don't understand it and still run it..cool....FFS!
#
# By Mike Donaldson (tekctrl@gmail.com) 
#
######

#Specifiy the source folder
$Client = Read-Host "Which client folder are you clearing up?"
$JobNumber = Read-Host "Which Job folder are you clearing up?"
$sourceDir = ('Z:\' + $Client + "\" + $JobNumber )
$sourceSearch = ($sourceDir + '*')
$illegalCharacters = "[\[\]+]'" 
echo $sourceSearch

#Find all folders that have been marked for being dumb 
Get-ChildItem $sourceSearch -recurse -Force |
#For each item thats recursively in the sourceSearch, we wipe the content of the file, leaving the file name and thats it. 0kb for each file makes it much easier to look after.
    foreach{
        if ( $_.Name -match $illegalCharacters )
        {
        rename-item -LiteralPath $_.FullName -NewName ($_.Name -replace $illegalCharacters,'-')
        }
       clear-content $_.FullName
}

No comments:

Post a Comment