#!/usr/bin/perl # Copyright (c) 2009, Nate Kettlewell, FishNet Security, Inc. # All rights reserved. # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the nor the # names of its contributors may be used to endorse or promote products # derived from this software without specific prior written permission. # THIS SOFTWARE IS PROVIDED BY FishNet Security, Inc. ''AS IS'' AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ;) use strict; # We'll need this for sending our file use Net::FTP; # Define our filename for our backup, I store them in /var/tmp, they'll be moved off after I'm done. my $archive_location = '/var/tmp/'; # This is where our backupX folders are stored. my $backup_location = '/var/netscreen/dbbackup/'; # Define our FTP info my $ftp_host = 'X.X.X.x'; my $ftp_user = 'user'; my $ftp_pass = 'pass'; my $ftp_directory = '/'; ### # # Program Start # ### # Generate random string of numbers for filename uniqueness my $length = '5'; my $random_string = ''; my $count = ''; while ($count < $length) { $random_string .= int(rand(9)); $count++; } # Generate a timstamp for our filename, I know, it's not the best way.. my $date_stamp = ''; chomp($date_stamp = qx/date +%m%d%y/); # Get the day of the week for the appropriate backup folder, this assumes the NSM is set to 7 backups, once for each day of the week. my $day_of_week = ''; chomp($day_of_week = qx/date +%u/); # Define our remote and local filenames for the FTP transfer my $remote_filename = 'NSM-' . $date_stamp . '-' . $random_string . '.tar.gz'; my $local_filename = $archive_location . 'NSM-' . $date_stamp . '-' . $random_string . '.tar.gz'; ### # # Create our archive. # ## # Define our backup folder name my $backup_folder = $backup_location . 'backup' . $day_of_week; # Create a tar gzip archive of the folder contents `tar -czvf $local_filename $backup_folder/*`; ### # # Archive is created, ready to send # ### # Create a new Net::FTP object to handle the transaction my $ftp = Net::FTP->new("$ftp_host", Debug => 0) or die "Cannot connect to $ftp_host: $@"; # Login $ftp->login("$ftp_user","$ftp_pass") or die "Cannot login ", $ftp->message; # Change to BINARY MODE, very important. $ftp->binary(); # Change to our specified remote directory $ftp->cwd("$ftp_directory"); # Send our file $ftp->put("$local_filename","$remote_filename") or die "send failed ", $ftp->message; # Hangup our connection $ftp->quit; # We're done remove the archive we created `rm $local_filename`; exit;