perl scripts


perl scripts hugo

1. To copy data

perldatacopy.pl
use strict;
use warnings;
use File::Copy::Recursive qw(fcopy rcopy dircopy);

#script for coping data from copylist.txt

my $cnt;
my $inputfile="ILFS_N.txt";
my $backupdst="G:/ILFS";

print "The input file is: $inputfile\n";
open (my $fh, '<', $inputfile) or die 'Could not read file';

$cnt++ while <$fh>;
print "Number of directories to copy: $cnt\n";
close $fh;

open (my $fh1, '<', $inputfile) or die 'Could not read file';
my $linecnt=1;

while (<$fh1>) {
print "\n==================\n";
print "copying $linecnt out of $cnt";
print "\n==================\n";
print "line is "."$_\n";
chomp $_;
if ( -d "$_" ) { #if source exists {
	unless ( -d "$backupdst/$_" ) { #if dstdir exists skip the copy
	dircopy("$_","$backupdst/$_") && print "$_ is copied.\n";  # or die "Copy failed: $!";
	sleep 15;
	} else {
	print "$_ exists in $backupdst dir\n";
	sleep 1;
	}
} else {
print "skipping $_ as it does not exists in src dir\n";
sleep 1;
}
$linecnt++;
}

2. Check disk space

dfSpace.pl
#!/usr/bin/perl
use strict;
use warnings;

my $mailfrom = "xxxxx\@hotmail.com";
my $mailto = "xxxxx\@hotmail.com";
my $above = 80; #90;
my $mail;
my @New;

use FileHandle; 

my @check=`df -h`;

##### Checks for lines higher than $above
foreach (@check) {
	if (m/(\d+)\%/ and $1 >=$above) {
		push @New,$_;
	}
}
# debug
foreach (@New) {
	print "$_\n";
}

##### If there is anything found, send mail 
if (@New) {
	$mail = new FileHandle; 
	$mail->open("| /usr/sbin/sendmail -t") || die "Cannot open: $!";
	$mail->print("From: $mailfrom\n");
	$mail->print("To: $mailto\n");
	$mail->print("Subject: Disk space is low!\n\n");
	$mail->print(@New, "\n");
	$mail->close();
}

3. svn export

svn_export.pl
#!/usr/bin/perl
# Script for exporting svn data.

use strict;
use warnings;

my $cnt;
my $inputfile=$ARGV[0];
print "The input file is: $inputfile\n";
open (my $fh, '<', $inputfile) or die 'Could not read file';

$cnt++ while <$fh>;
print "Number of directories to export: $cnt\n";
close $fh;

# my @arr2;
# while (my $line = <$fh>) {  #read each line
	# chomp $line;
	# push @arr2, $line;
# }
open (my $fh1, '<', $inputfile) or die 'Could not read file';
my $linecnt=1;
#print "$linecnt\n";
while (<$fh1>) {
print "\n==================\n";
print "exporting $linecnt out of $cnt\n";
print "\n==================\n";
print "line is "."$_\n";
system("$_");
my $percent=(100 * $linecnt/$cnt); #calculate the percentage done.
my $rounded=printf ("%.1f", $percent);
print "$rounded\% is completed\n";
$linecnt++;
sleep 30;
}

4. Data copy with robocopy script

data_copy_robocopy.pl
#!/bin/perl
# script for coping data from copylist.txt
# date: 21-11-2019
# Owner: Wilson Rodrigues
# version: 0.2.1
# copy/move with robocopy

use strict;
use warnings;
use File::Copy::Recursive qw(fcopy rcopy dircopy);
use v5.28.0;
use autodie;

my $cnt;
my $inputfile="copythis.txt";
my $srcdir1="F:/CBSE/11/";
my $srcdir2="F:/Elementary/";
my $srcdir3="F:/HDD_8-1/hurix_missing_data/SVN/";
my $backupdst="Z:\\UnrestrictedNuggets";

say "The input file is: $inputfile";
open (my $fh, '<', $inputfile) or die 'Could not read file';

my $outputfile="data_copy_robocopy_log.txt";
open (my $fhout, '>>', $outputfile) or die 'Could not read file';

#check if source directories exists.
unless ( -d "$srcdir1" and -d "$srcdir2" and -d "$srcdir3" ) {
say "Source directories not available";
sleep 5;
die "Aborting";
}

#check if destination directory exists.
unless ( -d "$backupdst" ) {
say "Destination directory not available";
sleep 5;
die "Aborting";
}

$cnt++ while <$fh>;
say "Number of directories to copy: $cnt";
close $fh;

open (my $fh1, '<', $inputfile) or die 'Could not read file';
my $linecnt=1;

while (<$fh1>) {
say "==================";
say "copying $linecnt out of $cnt";
say "==================";
#say "line is "."$_";
chomp $_;

if ( -d "$srcdir1/$_" ) {
	say "Source $_ exist in $srcdir1 : will begin copy";
	sleep 1.5;
	unless ( -d "$backupdst/$_" ) { #if $_ exists in $backupdst then do not run the robocopy
	system ("ROBOCOPY $srcdir1\\$_ $backupdst\\$_ /r:4 /w:8 /tee /e") or print $fhout "$srcdir1$_ $backupdst\\$_  not copied.\n" and next; #for windows (copy data)
	#system ("ROBOCOPY $srcdir1\\$_ $backupdst\\$_ /s /move") or print $fhout "$srcdir1$_ $backupdst\\$_  not copied.\n"  and next; #for windows (move data)
	print $fhout "$srcdir1$_ $backupdst\\$_  copied.\n";
	} else { print $fhout "$backupdst\\$_  already exists and copy skipped. Check and delete $srcdir1\\$_ \n"; }
	sleep 1.5
} elsif ( -d "$srcdir2/$_" ) {
	say "Source $_ exist in $srcdir2 : will begin copy";
	sleep 1.5;
	unless ( -d "$backupdst/$_" ) { #if $_ exists in $backupdst then do not run the robocopy
	system ("ROBOCOPY $srcdir2\\$_ $backupdst\\$_ /r:4 /w:8 /tee /e") or print $fhout "$srcdir2$_ $backupdst\\$_  not copied.\n" and next; #for windows (copy data)
	#system ("ROBOCOPY $srcdir2\\$_ $backupdst\\$_ /s /move") or print $fhout "$srcdir2$_ $backupdst\\$_  not copied.\n"  and next; #for windows (move data)
	print $fhout "$srcdir2$_ $backupdst\\$_  copied.\n";
	} else { print $fhout "$backupdst\\$_  already exists and copy skipped. Check and delete $srcdir2\\$_ \n"; }
	sleep 1.5;
} elsif ( -d "$srcdir3/$_" ) {
	say "Source $_ exist in $srcdir3 : will begin copy";
	sleep 1.5;
	unless ( -d "$backupdst/$_" ) { #if $_ exists in $backupdst then do not run the robocopy
	system ("ROBOCOPY $srcdir3\\$_ $backupdst\\$_ /r:4 /w:8 /tee /e") or print $fhout "$srcdir3$_ $backupdst\\$_  not copied.\n" and next; #for windows (copy data)
	#system ("ROBOCOPY $srcdir3\\$_ $backupdst\\$_ /s /move") or print $fhout "$srcdir3$_ $backupdst\\$_  not copied.\n"  and next; #for windows (move data)
	print $fhout "$srcdir3$_ $backupdst\\$_  copied.\n";
	} else { print $fhout "$backupdst\\$_  already exists and copy skipped. Check and delete $srcdir3\\$_ \n"; }
	sleep 1.5;
} else {
	say ">>>>>>>>> $_ does not exist in $srcdir1 or $srcdir2 or $srcdir3 : will skip copy";
	print $fhout ">>>>>>>>> $_ does not exist in $srcdir1 or $srcdir2 or $srcdir3 : will skip copy.\n";
}
$linecnt++;
#sleep 3;
}

5. download patches for slackware on central server

slackpatches.sh
#!/bin/sh
# Script to download slackware patches.
cd /var/www/htdocs/slackpatches
#rsync -av --delete --exclude=isolinux/ --exclude=kernels/ --exclude=slackware/ --exclude=source/ --exclude=usb-and-pxe-installers/ rsync://slackware.osuosl.org/slackware/slackware-12.2/ slackware-12.2/.
#rsync -av --delete --exclude=isolinux/ --exclude=kernels/ --exclude=slackware/ --exclude=source/ --exclude=usb-and-pxe-installers/ rsync://slackware.osuosl.org/slackware/slackware-13.0/ slackware-13.0/.
#rsync -av --delete --exclude=isolinux/ --exclude=kernels/ --exclude=slackware/ --exclude=source/ --exclude=usb-and-pxe-installers/ rsync://slackware.osuosl.org/slackware/slackware-13.1/ slackware-13.1/.
#rsync -av --delete --exclude=isolinux/ --exclude=kernels/ --exclude=slackware/ --exclude=source/ --exclude=usb-and-pxe-installers/ rsync://slackware.osuosl.org/slackware/slackware64-13.1/ slackware64-13.1/.
#rsync -av --delete --exclude=isolinux/ --exclude=kernels/ --exclude=slackware/ --exclude=source/ --exclude=usb-and-pxe-installers/ rsync://slackware.osuosl.org/slackware/slackware-13.37/ slackware-13.37/.
#rsync -av --delete --exclude=isolinux/ --exclude=kernels/ --exclude=slackware/ --exclude=source/ --exclude=usb-and-pxe-installers/ --exclude=extra/ rsync://slackware.osuosl.org/slackware/slackware-14.0/ slackware-14.0/.
rsync -av --delete --exclude=isolinux/ --exclude=kernels/ --exclude=slackware/ --exclude=source/ --exclude=usb-and-pxe-installers/ --exclude=extra/ rsync://slackware.osuosl.org/slackware/slackware64-14.1/ slackware64-14.1/.
rsync -av --delete --exclude=isolinux/ --exclude=kernels/ --exclude=slackware64/ --exclude=source/ --exclude=usb-and-pxe-installers/ --exclude=pastures/ --exclude=extra/ --exclude=testing/ rsync://slackware.osuosl.org/slackware/slackware64-14.2/ slackware64-14.2/.

6. perl one liner

Find .aep files of a nugget eg. N2118
perl -nwl -e '(/N(\s|_)2118\D/i or /(N)2118\D/i) and (/.aep\z/i) and print "$ARGV => $_" ;' 3dlist/*.txt firesafe_hdd/*.txt svnlist/*.list