CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Find last-backup file within a directory using Perl

 



GAR
Novice

Sep 16, 2012, 6:19 AM

Post #1 of 10 (2949 views)
Find last-backup file within a directory using Perl Can't Post

I have to write a script in Perl that will output the last backup within a particular directory.

For ex

The parent directory is /home/Backup/

In /home/Backup/windows i have the following files for eg

Windows_Server_A_20120901
Windows_Server_A_20120902
Windows_Server_A_20120903
Windows_Server_B_20120901
Windows_Server_B_20120902
Windows_Server_B_20120903
Windows_Server_XZ_20120903
Windows_Server_XZ_20120901
Windows_Server_XZ_20120902
Windows_Server_XZ_20120903

The output should be

Windows_Server_A_20120903
Windows_Server_B_20120903
Windows_Server_XZ_20120903

In /home/Backup/Unix for eg i have the following files

Unix_Server_A_20120901.gz
Unix_Server_A_20120902.gz
Unix_Server_A_20120903.gz
Unix_Server_B_20120901.gz
Unix_Server_B_20120902.gz
Unix_Server_B_20120903.gz
Unix_Server_XY_20120901.gz
Unix_Server_XY_20120902.gz
Unix_Server_XY_20120903.gz
This output should be

Unix_Server_A_20120903.gz
Unix_Server_B_20120903.gz
Unix_Server_XY_20120903.gz

I have just take 2 example here. In /home/Backup/ i have other directories in which other backups are done.

Of course for each sub directory there will be a subroutine as the naming are different.

I am a beginner in perl scripting. Your help will be highly appreciated.

So far from my point of view what i wanted to do windows backup for ex

strip (_20120901) ie the last 9 characters from the filename
then search for the lastbackup.

Can someone guide me how can I achieve this using perl.

************
This is what i have done so far

I think i need first of all to recuperate all the names of the servers and later i will use it to get the last backup

This is what i have done so far but am getting duplicates names

#!/usr/bin/perl

use strict;
use warnings;

my @files = glob("Windows_Server*");

foreach my $file (@files) {

$file = substr($file,0,-9);

print "$file\n";

}
exit 0;

The output is
Windows_Server_A
Windows_Server_A
Windows_Server_A
Windows_Server_B
Windows_Server_B
Windows_Server_B
Windows_Server_XY
Windows_Server_XY
Windows_Server_XY

How do i remove the duplicate names and how do i specify the directory i.e /home/Backup/windows. I will be changing regularly in the script. Once i remove the duplicate names i will work on how to get the last backup

Thank you all.

In Reply To


Laurent_R
Veteran / Moderator

Sep 16, 2012, 9:36 AM

Post #2 of 10 (2920 views)
Re: [GAR] Find last-backup file within a directory using Perl [In reply to] Can't Post

The easiest way to remove duplicates is genberally to use a hash to store the items that you have already seen. When you see a server name, check if it is in the hash. If it already there, juste ignore it. If it is not there, then add it. At the end, your hash will contain server names without duplicate.

For example you could change your code as follows:


Code
my %servernames; 
# ...
foreach my $file (@files) {
$file = substr($file,0,-9);
$servernames{$file} = 1 unless exists $servernames{$file};
}
# the %servernames hash now contains on sample of every filename


Actually, the "unless exists..." part is not even mandatory, you'll have still only one sample of the server names it you don't put it, but I find the intent is clearer if you put it.


GAR
Novice

Sep 16, 2012, 11:42 PM

Post #3 of 10 (2897 views)
Re: [Laurent_R] Find last-backup file within a directory using Perl [In reply to] Can't Post

Hello Laurent

Thanks for your help.

I have modified the code

#!/usr/bin/perl

use strict;
use warnings;

my @files = glob("/home/backup/windows/Win*");
my %servernames;

foreach my $file (@files) {

$file = substr($file,0,-9);
$servernames{$file} = 1 unless exists $servernames{$file};

print %servernames;

}
exit 0;

the output is not good.

can u plz advice.

Also, do you think its the correct i am doing to find the last backup

Thanks in adv.

In Reply To


GAR
Novice

Sep 17, 2012, 6:24 AM

Post #4 of 10 (2854 views)
Re: [GAR] Find last-backup file within a directory using Perl [In reply to] Can't Post

Hello

Can someone please help. its a bit urgent.

Regards,
GAR


Laurent_R
Veteran / Moderator

Sep 17, 2012, 8:00 PM

Post #5 of 10 (2832 views)
Re: [GAR] Find last-backup file within a directory using Perl [In reply to] Can't Post

Your printing is at the wrong place: it should be after closing the foreach loop. Also, you need to print the values you have stored as keys of the hash, not the hash itself.

Try this:


Code
 
#!/usr/bin/perl

use strict;
use warnings;

my @files = glob("/home/backup/windows/Win*");
my %servernames;

foreach my $file (@files) {

$file = substr($file,0,-9);
$servernames{$file} = 1 unless exists $servernames{$file};
}

print "$_\n" foreach keys %servernames;
}



kola4help
Novice

Sep 20, 2012, 10:03 AM

Post #6 of 10 (2810 views)
Re: [Laurent_R] Find last-backup file within a directory using Perl [In reply to] Can't Post

Try this......



#!/usr/bin/perl

use strict;
use warnings;

my @files = glob("Windows_Server*");

my %files;

foreach my $file (sort @files) {
my $file_name = substr($file,0,-9);
$files{$file_name}=1;
}

foreach (sort keys %files) {
my @files_latest = glob("$_*");

print "$files_latest[$#files_latest]\n";
}

exit 0;


FishMonger
Veteran / Moderator

Sep 20, 2012, 11:40 AM

Post #7 of 10 (2808 views)
Re: [GAR] Find last-backup file within a directory using Perl [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings;


my @files = qw(
Windows_Server_A_20120901
Windows_Server_A_20120902
Windows_Server_A_20120903
Windows_Server_B_20120901
Windows_Server_B_20120902
Windows_Server_B_20120903
Windows_Server_XZ_20120903
Windows_Server_XZ_20120901
Windows_Server_XZ_20120902
Windows_Server_XZ_20120903
);

my %newest;

foreach my $file ( @files ) {
no warnings qw(uninitialized);
my ($name, $date) = $file =~ /^(.+)_(\d+)/;
$newest{$name} = $date if $date > $newest{$name};
}

while (my ($name, $date) = each %newest) {
print "${name}_$date\n";
}



(This post was edited by FishMonger on Sep 20, 2012, 11:42 AM)


GAR
Novice

Oct 2, 2012, 11:43 PM

Post #8 of 10 (2294 views)
Re: [kola4help] Find last-backup file within a directory using Perl [In reply to] Can't Post

Hello

Thanks for your help.

This code works fine


Code
#!/usr/bin/perl 

use strict;
use warnings;

my @files = glob("Win*");

my %files;

foreach my $file (sort @files) {
my $file_name = substr($file,0,-9);
$files{$file_name}=1;
}

foreach (sort keys %files) {
my @files_latest = glob("$_*");

print "$files_latest[$#files_latest]\n";
}

exit 0;


Can you explain me this part. I have some difficulties to understand it.

foreach (sort keys %files) {
my @files_latest = glob("$_*");

print "$files_latest[$#files_latest]\n";

Since i have several directories and different naming of files in seperate directories, in case it dont work, i will be able to debug.

Also i will be executing this script using crontab and i will output its content
to a text file.

The problem how i can specify the path in the script i.e i will be having different path.

eg

/home/backup/windows/
/home/backup/Unix/

when i populate @files like this

my @files = glob("/home/backup/windows/Win*")

I get the full pathname, which i dont want. Can you plz recommend me, what i can do

Thanks.


GAR
Novice

Oct 8, 2012, 10:16 PM

Post #9 of 10 (2206 views)
Re: [kola4help] Find last-backup file within a directory using Perl [In reply to] Can't Post

Hello

Can someone plz help ?

Thanks


Laurent_R
Veteran / Moderator

Oct 9, 2012, 10:55 AM

Post #10 of 10 (2196 views)
Re: [GAR] Find last-backup file within a directory using Perl [In reply to] Can't Post

 

Code
foreach (sort keys %files) { 
# the previous line iterates over the unique file names in the hash %files. Each name is stored
# in the $_ special variable
my @files_latest = glob("$_*");
# the previous line looks in the directory for files having each of the unique file names
#and stores the result in the @files_latest array
print "$files_latest[$#files_latest]\n";
# this prints the last element of the @files_latest array, i.e. the most recent backup



(This post was edited by Laurent_R on Oct 9, 2012, 10:58 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives