#!/usr/bin/perl
# Research into using Perl to connect to the database, to see if it exists/host exists...
# Make so that it detects where you are acessig script from
# add the settings sectino
# make it password protected...
# test it..
$webmaster = 'webmaster@ace-installer.com';
$send_mail_to_admin = 1;
$sendmail = "/usr/sbin/sendmail";
$debug = 0;
# if we have debug on ,this could output helpful info...
if ($debug) {
use CGI::Carp qw(fatalsToBrowser);
}
# get all the input ito CGI.pm
use CGI qw(:standard);
$input = new CGI;
# print a content header...
print $input->header();
# get all the vars we will need...
$action = $input->param('action');
$time = $input->param('time');
$file_name = $input->param('file_name');
$path_to_backup = $input->param('path_to_backup');
$title = $input->param('title');
$folder = $input->param('folder');
$database_name = $input->param('database_name');
$save_to = $input->param('save_to');
$username = $input->param('username');
$password = $input->param('password');
$host = $input->param('host');
$current_dir = $0; $current_dir =~ s/backup\.cgi//; # get the folder location of this script...
# if being run via cron/telnet, then need to skip the below, and just straight into it!
if (!$ENV{REQUEST_METHOD}) { &do_backup; }
# decide what to do...
if ($action eq "set_cron") { &set_cron; }
elsif ($action eq "set_cron_2") { &set_cron_2; }
elsif ($action eq "edit_folder") { &edit_folder_list; }
elsif ($action eq "add_directory") { &add_directory; }
elsif ($action eq "del_dir") { &delete_directory; }
elsif ($action eq "edit_db_list") { &edit_db_list; }
elsif ($action eq "add_db") { &add_db; }
elsif ($action eq "del_db") { &delete_db; }
else { &show_main; }
####################################################
sub show_main {
$message = "Please select what you want to do from the menu on your left.";
# now show the message in our template...
&show_page($message);
}
####################################################
# show HTML to set cron job...
sub set_cron {
$message = qq|
Please complete the form below to set up your cron job.
THIS WILL ONLY WORK ON UNIX SERVERS!
How often do you want the script to backup?
|;
# now show the page.....
&show_page($message);
} # end the set_cron sub...
####################################################
sub set_cron_2 {
$script_path = $ENV{'SCRIPT_FILENAME'};
if ($time eq "daily") {
$command_to_add = "0 1 * * * perl $script_path\n"; # run at 1am, daily...
} # end 'if'
if ($time eq "weekly") {
$command_to_add = "0 0 * * 0 perl $script_path\n"; # run every Sunday, at midnight
} # end 'if'
if ($time eq "monthly") {
$command_to_add = "0 0 1 * * perl $script_path\n"; # set to run every month, on the 1st, at midnight
} # end 'if'
if ($time eq "annually") {
$command_to_add = "0 0 1 1 * perl $script_path\n"; # set to run at Jan 1st, at midnight
} # end 'if'
# now get the old crontab...
`crontab -l > cron_list.list`;
open(CRON_LIST, "cron_list.list") || &error("Unable to open cron_list.list...reason: $!");
@array = ;
close(CRON_LIST);
foreach $cron (@array) {
@exploded_cron = split /\:\:+/, $cron; # explode it...so we can compare values and stuff...
if ($exploded_cron[6] eq "$script_path") {
$back_in .= $command_to_add;
$found = 1;
} else {
$back_in .= $cron unless $cron =~ /^#/;
} # end if statement...
} # end foreach
# if we didn't find it in the other list (and thus edit it), we add it to $back_in here...
if (!$found) {
$back_in .= $command_to_add;
}
chmod(0666, "cron_list.list"); # incase its not CHMODed to 666...we'll add this...
open(WRITE_CRON, ">cron_list.list") || &error("Can't write to cron_list.list. Reason: $!");
print WRITE_CRON $back_in;
close(WRITE_CRON);
# update the cron list...
$location = $current_dir . "cron_list.list";
if ($debug) { print $location; }
open PIPE, "crontab $location |";
@res = ;
close PIPE or $bad = "$?,$!";
if ($bad) { $report = "ERROR: Could not update crontab. Reason: $bad"; } else { $report = "Updated cron Cron has been updated successfully to be run $time"; }
#`crontab -e < cron_list.list`;
&show_page($report);
}
####################################################
sub edit_folder_list {
# see if the file exists...and if it does, we need to make up some extra HTML stuff for the ones that exist...
#if (-e "folder.pl" && -z "folder.pl") { $exists = 1; } else { $exists = 0; }
$exists = -z "folder.pl";
if (!$exists) {
open(FOLDERS,"folder.pl") || &error("Unable to open folder.pl. Reason: $!");
@folders = ;
close(FOLDERS);
foreach (@folders) {
@divided = split /\:\:/, $_;
$folder_path = $divided[0];
$folder_name = $divided[1];
$existing_folders .= qq|
$folder_name |
$folder_path |
[ Delete ] |
|;
} # end foreach
} else { $existing_folders = "There are no existing folders..."; }# end if
}
# define the form, as well as any other folders that may exist...
$message = qq|
$existing_folders
|;
# now shoow the template...
&show_page($message);
####################################################
sub add_directory {
if (!$file_name) { &error("Can't add directory, as you didn't provide a file to back it up to!"); }
if (!$path_to_back) { &error("Can't add directory, as you didn't provide one!"); }
unless (-e $path_to_back) { &error("Can't add directory, as it doesn't appear to exist!"); }
open(FOLDER, ">>folder.pl") || &error("Unable to write to folder.pl. Reason: $!");
print FOLDER "$path_to_backup\:\:$file_name\n";
close(FOLDER);
$message = "Added $file_name ($path_to_backup)...";
&show_page($message);
} # end sub...
####################################################
sub delete_directory {
open(FOLDER_LIST,"folder.pl") || &error("Unable to open folder.pl. Reason: $!");
@list = ;
close(FOLDER_LIST);
foreach $line (@list) {
chomp $line;
if ($line eq "$folder\:\:$title") { } else { $put_back .= $line . "\n"; }
}
open(FOLDER_BACK,">folder.pl") || &error("Unable to open folder.pl. Reason: $!");
print FOLDER_BACK $put_back;
close(FOLDER_BACK);
$message = "Directory '$title', deleted successfully...";
&show_page($message);
}
####################################################
sub edit_db_list {
# see if the file exists...and if it does, we need to make up some extra HTML stuff for the ones that exist...
$exists = -z "databases.pl" ? 1 : 0;
if (!$exists) {
open(DATABASES,"databases.pl") || &error("Unable to open databases.pl. Reason: $!");
@databases = ;
close(DATABASES);
# database is in format of Database::file_name_for_saving::usewrname::password::host
foreach (@databases) {
@divided = split /\:\:/, $_;
$database_name = $divided[0];
$save_to = $divided[1];
$username = $divided[2];
$password = $divided[3];
$host = $divided[4];
$existing_databases .= qq|
$database_name |
$save_to |
$username |
$password |
$host |
[
Delete ] |
|;
} # end foreach
} else { $existing_databases = "There are no existing databases..."; }# end if
# define the form, as well as any other folders that may exist...
$show_message = qq|
$existing_databases
|;
# now shoow the template...
&show_page($show_message);
}
###################################################
sub add_db {
# check they entered it all ok :p
if (!$database_name || !$save_to || !$username || !$password || !$host) { &error("Can't add database, as you didn't provide all the required information!"); }
# add the database to the list...
open(FOLDER, ">>databases.pl") || &error("Unable to write to databases.pl. Reason: $!");
print FOLDER "$database_name\:\:$save_to\:\:$username\:\:$password\:\:$host\n";
close(FOLDER);
$message = "Added $database_name (saving to $save_to)...";
&show_page($message);
}
###################################################
sub delete_db {
open(FOLDER_LIST,"databases.pl") || &error("Unable to open databases.pl. Reason: $!");
@list = ;
close(FOLDER_LIST);
foreach $line (@list) {
chomp $line;
if ($line eq "$database_name\:\:$save_to\:\:$username\:\:$password\:\:$host") { } else { $put_back .= $line . "\n"; }
}
open(FOLDER_BACK,">databases.pl") || &error("Unable to open databases.pl. Reason: $!");
print FOLDER_BACK $put_back;
close(FOLDER_BACK);
$message = "Database '$database_name', deleted successfully...";
&show_page($message);
}
####################################################
####################################################
# do the backup...
sub do_backup {
# DO THE SQL DATABASES...
# first of all do the
$path_to_db = $current_dir . "databases.pl";
chmod(0666, "$path_to_db");
open(DATABASE_LIST,"$path_to_db") || &mail_error("Unable to open $path_to_db. Reason: $!");
@databases = ;
close(DATABASE_LIST);
foreach (@databases) {
chomp;
@divided = split /\:\:/, $_;
$database_name = $divided[0];
$save_to = $divided[1];
$username = $divided[2];
$password = $divided[3];
$host = $divided[4];
print "Backing up $database_name\n";
$results_database .= "Results for database '$database_name' (nothing is normally good) :: \n";
$bad = 0; # reset for each loop...
open PIPE, "mysqldump -u$username -p$password $database_name > $current_dir$save_to.sql.dump |";
@res = ;
close PIPE or $bad = "$?,$!";
if ($bad) { $report = "ERROR: Could not backup '$database_name' database!"; } else { $report = "Backed Up '$database_name' to '$save_to'"; }
$results_database .= "$report \n\n";
}
# NOW THE FOLDERS!
# first of all do the
$path_to_folder = $current_dir . "folder.pl";
chmod(0666, $path_to_folder);
open(FOLDER_LIST,$path_to_folder) || &mail_error("Unable to open $path_to_folder. Reason: $!");
@folders = ;
close(FOLDER_LIST);
foreach (@folders) {
chomp;
@divided = split /\:\:/, $_;
$folder_path = $divided[0];
$folder_name = $divided[1];
print "Backing up $folder_name\n";
$results_database .= "Results for folder '$folder_name'\n";
$bad = 0; # reset for each loop...
open PIPE, "tar -cf $folder_name.tar $folder_path |";
@res = ;
close PIPE or $bad = "$?,$!";
if ($bad) { $report = "ERROR: Could not backup '$folder_path' to '$folder_name'"; } else { $report = "Backed Up '$folder_path' to '$folder_name.tar.gz'"; }
`gzip -c $folder_name.tar > $current_dir$folder_name.tar.gz`;
$results_database .= "$report \n\n";
}
# if they want an email, then send it here...
if ($send_mail_to_admin) {
open(MAIL,"|$sendmail -t");
print MAIL "To: $webmaster \n";
print MAIL "From: $webmaster \n";
print MAIL "Reply-to: $webmaster \n";
print MAIL "Subject: RE Backup...\n\n";
print MAIL "Here is the resuilts of your server backup. It did both MySQL and Folder backups. \n\n$results_database\n";
print MAIL "\n \n Thanks";
print MAIL "\n";
print MAIL "A.J.Newby \n";
print MAIL "Ace Installer \n";
close(MAIL);
}
print "done backup";
exit; # stop it here, otherwise it will carry on showing HTML :)
}
####################################################
sub show_page {
my $CONTENT = shift;
print qq|
Ace Backup
Ace Backup |
MENU
|
$CONTENT |
Quick Links
Support
Our Home Page
|
© Ace Installer. All Rights Reserved 1999-2002 |
|;
exit; # shuit script off...just cos some other subs were being buggers :p
}
####################################################
####################################################
# if we get a problem, then we need this sub...
sub error {
my $error = shift;
print $error;
exit;
}
###################################################
# if we get a major problem during the cron, then we need this sub...
sub mail_error {
my $error = shift;
print $error;
open(MAIL,"|$sendmail -t");
print MAIL "To: $webmaster \n";
print MAIL "From: $webmaster \n";
print MAIL "Reply-to: $webmaster \n";
print MAIL "Subject: RE Backup...\n\n";
print MAIL "There was an error doing the backups. It was: \n\n $error\n";
print MAIL "\n \n Thanks";
print MAIL "\n";
print MAIL "A.J.Newby \n";
print MAIL "Ace Installer \n";
close(MAIL);
exit;
}