SomaFM commercial free internet radio All posts tagged 'powershell'

Paul Sturm

My notebook of discovered facts

Powershell + 7zip + SQL Server backups

I needed a way to shrink the backups in SQL Server 2005 (native compression doesn't exist until the 2008 version). 7zip compresses better than most anything, it's free (even for commercial use), its stable, and it's small. Powershell is my favorite tool builder. This is the result:

#set alias to 7zip - adjust path as necessary
set-alias sevenZip "c:\program files\7-zip\7z.exe"

#list of database filenames
$dbdirectories = "db1","db2","db3","db4"

$dbbackupDrive = "x:\"                              # this is where SQL Server stores its backups
$dbzdestination = "y:\7zipped_backups\"             # this is where we will store the 7z version

#Backups location
cd "$dbzdestination"                                # change directory 

#create some file masks based on date

$ybu = get-date (Get-Date).addDays(-1) -format "yyyyMMdd"    # this is the filename mask of yesterday's backups
$yz = get-date (Get-Date).addDays(-1) -format "yyyy.MM.dd"   # this is the filename mask of the zip destination
$yzr = get-date (Get-Date).addDays(-2) -format "yyyy.MM.dd"  # these are the day old 7zips (unused yet)
                                                             # I may want to remove older 7zips after copied offsite
                                                             
#$exclude1 = "-x!*.dif"                             # unused but could be appended to command 
#$exclude2 = "-x!*.7z"                              # unused but could be appended to command 

foreach ($dbdirectory in $dbdirectories)            # looping each db name in our list,
{  

    $backuppath = $dbbackupDrive + $dbdirectory + "\"            #build the path to the backup sources
    $backupfiles = $dbdirectory + "_backup_" + $ybu + "*.*"       #build the filename mask
    $input = $backuppath + $backupfiles                          #full path to source files (SQL Server's files)

    $outputfile = $dbdirectory + "_backup_" + $yz + ".7z"        #build the 7z filename (7zip file)

    cd "$dbzdestination$dbdirectory"               #change the path to the backup destination
    
    # command line switch descriptions (see http://dotnetperls.com/7-zip-examples)
    #   -t7z = 7zip type
    #   -v10m = split to 10 meg files
    #   -mmt = multithreading
    
    "7zip a -t7z -v10m -mmt=off $outputfile $input"               # echo out some progress    
    
    sevenZip a -t7z -v10m -mmt=off "$outputfile" "$input"         # 7zip them up

}

# we're all done, let's cleanup
Remove-Item Alias:sevenZip

Permalink | Comments (21) | Post RSSRSS comment feed

Sorting photos via powershell v1

I have a drive full of photos - over 300GB - that are sorted into folders for each location photographed. The folder name is a location code of sorts.  While NTFS can have billions of files in a folder, explorer will seize up for minutes as it retrieves data for this drive.

I needed a way to sort my folders into subfolders, just to speed up browsing the drive.  If a location code was DJKSASWW, I wanted that sorted into a folder name DJKS.  This would group much smaller sets of data and still allow for an intelligent, browseable structure if human intervention was necessary.

Powershell came thorough for me.

# source location of folder to copy/move
$source = "E:\IMAGEVAULT\"

# where we want these to wind up
$destination = "R:\imagevault\"

# get our objects (this is filtered for just those that start
# with the letter 'a' right now
$folders = Get-ChildItem -filter A* $source

# loop each folder object from our source
foreach ($folder in $folders){
    # the this specific folder name
    $foo = $folder.name
    # new folder name is going to be the first 4 characters
    $newfolder = [char]$foo[0]+[char]$foo[1]+[char]$foo[2]+[char]$foo[3]
    # set the path to this new destination folder
    $newDest = $destination + $newfolder

    # test to see if this path exists
    if(Test-Path $newDest)
        {   # it does so we will show what we're copying/moving
            $folder.FullName + " to " + $newDest
            # and perform the task
            Copy-Item $folder.FullName $newDest -recurse
        } else {
            # create path since it didn't exist
            New-Item $newDest -Type Directory
            # show what we're copying/moving
            $folder.FullName + " to " + $newDest
            # and perform the task
            Copy-Item $folder.FullName $newDest -recurse
        }
}

This allows me to set a source and destination and the script (while time consuming) will fix the entire structure.

All you'd have to do is change Copy-Item to Move-Item if you didn't want a 2nd set.


Categories: powershell | scripting
Permalink | Comments (21) | Post RSSRSS comment feed

Remove files based on age in days and email success message in Powershell

I have to cleanup a server share daily.  The following powershell script will remove old files and email a note to me telling me it ran.

#
# setup variables 
#
$ageInDays = 7
$fileShare = "\\server\share\subfolder"
#
# cleanout $fileShare and leave
#
$ageInDays worth of recent files
#
Get-ChildItem $fileShare -Recurse | where {$_.LastWriteTime -le (Get-Date).AddDays(-$ageInDays)} | Remove-Item -recurse
#
# setup to mail the results 
#
$SmtpClient = new-object system.net.mail.smtpClient
$SmtpServer = "smtp.domain.com"$SmtpClient.host = $SmtpServer 
$From = new-object System.Net.Mail.MailAddress("scheduledtask@domain.com", "Schedule Task")
$To = "me@domain.com"
$Title = "Snapshot cleanup run"
$Body = "The powershell snapshot cleanup of $fileShare successfully ran.
   This left $ageInDays days of snapshots. Script location is '\\server\share\cleanup_snapshots.ps1'" 
#
# mail the results 
#
$SmtpClient.Send($from,$to,$title,$Body) 

I need to get it to mail on failure, but I've not gotten that far.


Permalink | Comments (8) | Post RSSRSS comment feed