Hi there --
A colleague and I wrote a perl script that is designed to change to a specific directory, and once there proceed to tar a directory, encrypt the newly created tar file, and then tar to tape media the encrypted tar file. Once that was done, the script would go back to the source directory, remove the tar and encrypted tar files, and repeat the process on all additional directories in the same location.
The command syntax that is used to accomplish this is the following:
Code:
#!/usr/local/bin/perl
# The purpose of this script is to run a tar backup on
# every file wihtin a specific directory. Each file in
# the directory will be tarred, and then encrypted.
# Once that is done, the encrypted file will then be
# copied to tape. After that, the encrypted and original
# tar files will then be deleted from the directory.
# This process will be repeated for every file within
# the directory.
# Uncomment the line below to debug the script.
# set -x
# Prompt the user for the directory in question:
print "Please enter the full path of the directory: \n";
$path = <STDIN>;
chomp $path;
print "You have entered the following path: $path \n";
# Confirm the directory that was entered is the correct one.
print "Is $path the correct directory? [y/n] \n";
$confirm = <STDIN>;
chomp $confirm;
# NOTE: The chomp command was inserted immediately after $confirm to ensure
# there was no end-of-line character (\n) that would intefere with script
# operations.
if ($confirm ne "y")
{exit;}
# Prompt the user for the full pathname of the destination tape drive
# echo "Please enter the full devicefile name of the tape drive: "
# read devicefile
print "Please enter the full path of the device file: \n";
$device = <STDIN>;
chomp $device;
print "You have entered the following device file: $device \n";
# Confirm the device file that was entered is the correct one.
print "Is $device the correct device file? [y/n] \n";
$confirm = <STDIN>;
chomp $confirm;
# NOTE: The chomp command was inserted immediately after $confirm to ensure
# there was no end-of-line character (\n) that would intefere with script
# operations.
if ($confirm ne "y")
{exit;}
# Remind the user there should already be a tape in the destination drive
# or else the script will fail.
print "Verify there is a tape in the destination drive before proceeding \n";
print "Shall we proceed? [y/n] \n";
$confirm = <STDIN>;
chomp $confirm;
# NOTE: The chomp command was inserted immediately after $confirm to ensure
# there was no end-of-line character (\n) that would intefere with script
# operations.
if ($confirm ne "y")
{exit;}
# Prompt the user for the password that will be assigned to encrypted
# files that will be created and backed up to tape
print "Please enter the password that will be assigned to all of the \n";
print "encrypted files that will be backed up to tape. \n";
print "\n";
print "***************************************************************\n";
print "WARNING: BE SURE TO KEEP A GOOD AND SECURE RECORD OF THE PASSWORD\n";
print "THAT IS ASSOCIATED WITH THIS BACKUP. LOSING THIS RECORD WILL RESULT\n";
print "IN THE BACKED UP FILES NO LONGER BEING RETRIEVABLE.\n";
print "***************************************************************\n";
$password = <STDIN>;
chomp $password;
print "You have entered the following password: $password \n";
print "Shall we proceed? [y/n] \n";
$confirm = <STDIN>;
chomp $confirm;
if ($confirm ne "y")
{exit;}
opendir(DIR, $path) or die "Can't open $path \n";
# The previous line determines if the path in question is accessible.
# If the path is not accessible, the script immediately exits.
my @list = sort readdir(DIR);
# This will provide a list of the contents of the directory in question,
# and store it in the @list array.
$first=1;
# The $first line tells the script whether the tar archive is being initially
# created, or appended to. This is necessary in order for the tar backup to
# append the directories to the archive rather than overwrite them.
for (@list){
chomp;
next if $_ eq ".";
next if $_ eq "..";
# The item in the @list array is stored in the $_ variable. This variable
# is used for the directories that are listed below the parent. All will
# be handled on an individual basis.
# The two next lines tell the script to not attempt to encrypt the directory
# entries "." and "..". These two are not real files, they are simply place
# holders that should not be backed up.
# The command syntax shown below will have the script change to the appropriate
# directory, and once there run the tar command and pipe it through openssl to
# create an encrypted tar file, .des3, that will be ready for copying to the
# tape drive.
$cmd= "(cd $path; tar -cvf - $_|openssl des3 -salt -k $password > $_.des3)";
print `$cmd`;
if ($first){
$cmd= "(cd $path; tar -cvf $device $_.des3)";
} else{
$cmd= "(cd $path; tar -rvf $device $_.des3)";
}
$RC=$?;
print `$cmd`;
$cmd= "(cd $path; rm $_.des3)";
print `$cmd`;
$first=0;
}
During a testing of the script, indications were the above process completed successfully. There was write activity on the tape drive, and no errors appeared on-screen. After the session was completed, the tape media was rewound, and the tar -tvf command was ran to list the contents of the tape. The output indicated that the first directory was backed up successfully, but there was no indication of any additional directories being backed up to tape. The mt -f <DEVICE> fsf 1 command was used to go to the end of the archive, and the tar -tvf command was reran to see if there were additional archives to the initial one on the tape, but there was no indication of any.
One thought that came to mind was to remove, or initially comment out, from the script the tar -rvf command, and rerun the script along with the follow-up tvf command. Would this approach work, or is there something else that needs to be done? Thanks.