Script to replace index file
Hii have found this script to replace index files from weekly backup of Whm but it dont replace index file inside subdirectory, any alternative?
#!/usr/bin/perl
#version 2
my $hasiozlib = 0;
my $hasarchivetar = 0;
eval {
require IO::Zlib;
$hasiozlib = 1;
};
eval {
require Archive::Tar;
$hasarchivetar = 1;
};
if (!($hasiozlib) || !($hasarchivetar)) {
system("/scripts/perlinstaller","IO::Zlib","Archive::Tar");
die "Please re-run this script. Modules were missing!\n";
}
use strict;
my $dir = '/backup/cpbackup/weekly';
opendir(CPB,$dir);
my @FS = readdir(CPB);
@FS = grep(/\.tar\.gz$/, @FS);
closedir(CPB);
my $restorecount = 0;
foreach my $fs (@FS) {
$fs =~ s/\.tar\.gz//g;
next if (!getpwnam($fs));
my $homedir = (getpwnam($fs))[7];
my $uid = (getpwnam($fs))[2];
my $gid = (getpwnam($fs))[3];
print "$fs $homedir\n";
my $tar = Archive::Tar->new;
$tar->read("$dir/${fs}.tar.gz",1);
print "Checking Archive for files!\n";
my @files = $tar->get_files("${fs}/homedir/public_html/index.html",
"${fs}/homedir/public_html/index.htm","${fs}/homedir/public_html/index.php");
foreach my $ft (@files) {
my $filename = $ft->name();
if ($filename =~ /index.html$/) {
open(RESTOREFILE,">${homedir}/public_html/index.html");
chown $uid, $gid, "${homedir}/public_html/index.html";
}
if ($filename =~ /index.htm$/) {
open(RESTOREFILE,">${homedir}/public_html/index.htm");
chown $uid, $gid, "${homedir}/public_html/index.htm";
}
if ($filename =~ /index.php$/) {
open(RESTOREFILE,">${homedir}/public_html/index.php");
chown $uid, $gid, "${homedir}/public_html/index.php";
}
print "Restoring $filename\n";
print RESTOREFILE $ft->get_content();
close(RESTOREFILE);
$restorecount++;
}
}
print "$restorecount file(s) restored!\n";