Subversion Repositories sysadmin_scripts

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 rodolico 1
#! /usr/bin/env perl
2
 
3
# archvieProjects.pl
4
# Author: R. W. Rodolico
5
# Date: 20180603
6
# Copyright: 2018, Vanduzen Enterprises, Dallas TX
7
 
8
# Script designed to be run from a cron job, which checks if any projects
9
# are ready to be archived. A project is defined as a directory under
10
# the root of $localProjectDirectory.
11
 
12
# If found, all directories are moved into the staging area and 
13
# an md5 checksum is calculated for the entire tree.
14
# After all projects are moved, a second process looks in the staging
15
# area and copies the files (using rsync for reliability) into the staging
16
# area of $targetServer. When a project has been copied, a checksum is
17
# calculated on the remote copy and compared to the checksum calculated
18
# in the first stage and, if it passes, the project is then moved to the 
19
# $targetProjectDirectory.
20
# After the copy and move, the project and its MD5 sum file are moved
21
# to the $trashDirectory (which is cleaned on the next invocation of
22
# the script).
23
 
24
# Script does NOT handle the situation where projects are being moved
25
# while the script is running, so the script should be run at a time
26
# when there is no other activity on the server.
27
#
28
# Version: 1.0
29
 
30
use warnings;
31
use strict;
32
use Cwd qw();
33
use File::Copy qw(move);
34
use File::Basename;
35
 
36
my $localProjectDirectory = '/home/samba/transfers/denver_to_dallas/Archive_to_DaVinci';
37
my $trashDirectory = "$localProjectDirectory/.Trash";
38
my $stagingArea = "$localProjectDirectory/.Staging";
39
my $targetProjectDirectory = '/home/samba/archives/fromDenver/';
40
my $targetStagingArea = '/home/samba/archives/fromDenver/.Staging/';
41
my $targetServer = 'davinci';
42
my $md5suffix = '.md5sum';
43
 
44
my @ProjectsToMove;
45
 
46
# look in the projects to move directory and see if there is anything 
47
# new in there.
48
sub getProjects {
49
   my $rootDir = shift;
50
   opendir( my $dh, $rootDir ) or die "Could not open directory $rootDir: $!\n";
51
   my @dirs = grep { ! /^\./ && -d "$rootDir/$_" } readdir( $dh );
52
   return @dirs;
53
}
54
 
55
# calculate the checksum of a directory by
56
# 1. calculate checksum of each individual file in the entire tree
57
# 2. Grab the first column, which is the checksum
58
# 3. sort the result since Linux will not always return them in the same order
59
# 4. do a checksum of the checksums
60
#
61
# This is highly unlikely to give the same answer if any file changes
62
# in the process of the copy
63
sub calcMD5 {
64
   my $directory = shift;
65
   return -1 unless -d $directory;
66
   my $md5 = `find '$directory' -type f -exec md5sum \\{\\} \\; | cut -d' ' -f1 | sort | md5sum | cut -d' ' -f1`;
67
   chomp $md5;
68
   return $md5;
69
}
70
 
71
# moves project to staging area and puts the md5 sum into a file
72
# with the same name, but a .md5sum suffix
73
sub moveToStaging {
74
   my ( $project, $stagingArea, $md5 ) = @_;
75
   mkdir $stagingArea unless -d $stagingArea;
76
   move( "$localProjectDirectory/$project", "$stagingArea/$project" );
77
   my $md5File = "$stagingArea/$project" . $md5suffix;
78
   open DATA,">$md5File" or die "Could not create md5sum file [$md5File]: $!\n";
79
   print DATA "$md5\n";
80
   close DATA;
81
   return;
82
}
83
 
84
# verifies the project is correct on the server by comparing the checksums
85
# calculated locally and remote server. If valid, moves it into the final
86
# location on the remote server
87
sub validateTarget {
88
   my ( $remoteServer, $remoteStaging, $remoteTarget, $project, $checksum ) = @_;
89
   my $md5sum = `ssh $remoteServer "find '$remoteStaging/$project' -type f -exec md5sum \\{\\} \\; | cut -d' ' -f1 | sort | md5sum | cut -d' ' -f1"`;
90
   chomp $md5sum;
91
   if ( $checksum eq $md5sum ) {
92
      my $command = "ssh $remoteServer \"mv '$remoteStaging/$project' '$remoteTarget'\"";
93
      if ( system( $command ) == 0 ) {
94
         return 1;
95
      } else {
96
         &logit( "Unable to move $project to $remoteServer:$remoteTarget" );
97
         return 0;
98
      }
99
   } else {
100
      &logit( "Invalid checksum moving project $project" );
101
      return 0;
102
   }
103
}
104
 
105
# reads the checksum file
106
sub getCheckSum {
107
   my ( $project, $staging )  = @_;
108
   $project .= $md5suffix;
109
   if ( open DATA, "<$staging/$project" ) {
110
      my $cksum = <DATA>;
111
      chomp $cksum;
112
      close DATA;
113
      return $cksum;
114
   } 
115
   &logit( "Could not open $staging/$project: $!" );
116
   return '';
117
}
118
 
119
# simple little logger that records some information   
120
sub logit {
121
   my $message = shift;
122
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
123
   my $now = sprintf( "%04d-%02d-%02d %02d:%-2d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec );
124
   open LOG, ">>/tmp/archiveProjects.log" or die "could not write to archiveProjects.log: $!\n";
125
   print LOG "$now\t$message\n";   
126
   close LOG;
127
}
128
 
129
# simply remove everything from the trash directory
130
sub cleanTrash {
131
   my $trashDir = shift;
132
   `rm -fR $trashDir/*`;
133
}
134
 
135
 
136
#&cleanTrash( $trashDirectory ) if &getProjectsToMove( $trashDirectory );
137
 
138
 
139
# first, check and see if we have any projects we need to move
140
@ProjectsToMove = &getProjects( $localProjectDirectory );
141
 
142
foreach my $project ( @ProjectsToMove ) {
143
   my $md5 = &calcMD5( "$localProjectDirectory/$project" );
144
   &logit( "New Project $md5\t$project" );
145
   &moveToStaging( $project, $stagingArea, $md5 );
146
}
147
 
148
# done with that, now we need to see if there is anything in the staging area
149
# that needs to be sent to the remote server
150
opendir( my $dh, $stagingArea ) or die "Could not read $stagingArea: $!\n";
151
my @directories;
152
my @toMove = grep { /$md5suffix$/ } readdir( $dh );
153
foreach my $project ( @toMove ) {
154
   $project =~ m/^(.*)\.md5sum/;
155
   $project = $1;
156
   my $md5sum = &getCheckSum( $project, $stagingArea );
157
   next unless $md5sum;
158
   my $rsync = "rsync -av '$stagingArea/$project' $targetServer:$targetStagingArea/ > /tmp/lastrsync.log";
159
   &logit( $rsync );
160
   if ( system ( $rsync ) == 0 ) { # we succeeded
161
      if ( &validateTarget( $targetServer, $targetStagingArea, $targetProjectDirectory, $project, $md5sum ) ) {
162
         `mkdir -p $trashDirectory` unless -d $trashDirectory;
163
         move( "$stagingArea/$project", "$trashDirectory/$project" );
164
         $project .= $md5suffix;
165
         move( "$stagingArea/$project", "$trashDirectory/$project" );
166
         &logit( "Successfully moved project $project to $targetServer" );
167
      } else {
168
         &logit( "Unable to validate target for $project" );
169
      }
170
   } else {
171
      &logit( "Unknown error attempting to rsync $project" );
172
   }
173
}
174
 
175
 
176
1;