Recovering your dignity with perl

Due to human error (mine), a boatload of user images got removed from our company address book. Some scripting magic saved the day.

On the last day of the year a process that had never caused a problem ran and whacked the profile and thumbnail images of a large number of users in our company address book.

To recover from this we unarchived our backup from the day before and then compared what we found with the existing state of things. Anything that was in the backup but not found in the live image folders was restored.

Here’s the perl script that did the job.

use strict;
use List::Compare;

my $todayprofdir = "/var/www/html/directory/public/img/profiles";
my $backupprofdir = "/data/tmp/profs-20131230";
my $todaythumbdir = "/var/www/html/directory/public/img/thumbails";
my $backupthumbdir = "/data/tmp/thumbs-20131230";

opendir(DIR, "$todayprofdir") or die $!;
my @todayfiles = readdir DIR;
closedir DIR;

opendir(DIR, "$backupprofdir") or die $!;
my @backupfiles = readdir DIR;
closedir DIR;

my $lc = List::Compare->new(\@backupfiles, \@todayfiles);
my @backuponly = $lc->get_unique;
my $count =0;

foreach my $profname(@backuponly) {
   # print $profname, "\n";
   my ($id, $extension) = split('_', $profname);
   my $thumbname = $id . ".jpg";
   print $thumbname, "\n";

   `cp -f $backupprofdir/$profname $todayprofdir`;
   `cp -f $backupthumbdir/$thumbname $todaythumbdir`;
print "Total unique: ", $count, "\n";

This entry was posted in System Administration on by .

About phil

My name is Phil Lembo. In my day job I’m an enterprise IT architect for a leading distribution and services company. The rest of my time I try to maintain a semi-normal family life in the suburbs of Raleigh, NC. E-mail me at philipATlembobrothersDOTcom. The opinions expressed here are entirely my own and not those of my employers, past, present or future (except where I quote others, who will need to accept responsibility for their own rants).