| 1 |
#!/usr/bin/perl -w |
|---|
| 2 |
# retrieve_backup_cron_from_mysql.pl Copyright (C) 2005 VerCom Systems, Inc. & Ron Hartmann (rhartmann@vercomsystems.com) |
|---|
| 3 |
# Asterisk Management Portal Copyright (C) 2004 Coalescent Systems Inc. (info@coalescentsystems.ca) |
|---|
| 4 |
|
|---|
| 5 |
# this program is in charge of looking into the database and creating crontab jobs for each of the Backup Sets |
|---|
| 6 |
# The crontab file is for user asterisk. |
|---|
| 7 |
# |
|---|
| 8 |
# The program preserves any other cron jobs (Not part of the backup) that are installed for the user asterisk |
|---|
| 9 |
# |
|---|
| 10 |
|
|---|
| 11 |
# This program is free software; you can redistribute it and/or |
|---|
| 12 |
# modify it under the terms of the GNU General Public License |
|---|
| 13 |
# as published by the Free Software Foundation; either version 2 |
|---|
| 14 |
# of the License, or (at your option) any later version. |
|---|
| 15 |
|
|---|
| 16 |
# This program is distributed in the hope that it will be useful, |
|---|
| 17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 19 |
# GNU General Public License for more details. |
|---|
| 20 |
|
|---|
| 21 |
use FindBin; |
|---|
| 22 |
push @INC, "$FindBin::Bin"; |
|---|
| 23 |
|
|---|
| 24 |
use DBI; |
|---|
| 25 |
require "retrieve_parse_amportal_conf.pl"; |
|---|
| 26 |
|
|---|
| 27 |
################### BEGIN OF CONFIGURATION #################### |
|---|
| 28 |
|
|---|
| 29 |
# the name of the extensions table |
|---|
| 30 |
$table_name = "Backup"; |
|---|
| 31 |
# the path to the extensions.conf file |
|---|
| 32 |
# WARNING: this file will be substituted by the output of this program |
|---|
| 33 |
$Backup_cron = "/etc/asterisk/backup.conf"; |
|---|
| 34 |
# the name of the database our tables are kept |
|---|
| 35 |
$database = "asterisk"; |
|---|
| 36 |
|
|---|
| 37 |
# cool hack by Julien BLACHE <jblache@debian.org> |
|---|
| 38 |
$ampconf = parse_amportal_conf( "/etc/amportal.conf" ); |
|---|
| 39 |
# the name of the box the MySQL database is running on |
|---|
| 40 |
$hostname = $ampconf->{"AMPDBHOST"}; |
|---|
| 41 |
|
|---|
| 42 |
################### END OF CONFIGURATION ####################### |
|---|
| 43 |
open(FILE, "/etc/amportal.conf") || die "Failed to open amportal.conf\n"; |
|---|
| 44 |
while (<FILE>) { |
|---|
| 45 |
chomp; # no newline |
|---|
| 46 |
s/#.*//; # no comments |
|---|
| 47 |
s/^\s+//; # no leading white |
|---|
| 48 |
s/\s+$//; # no trailing white |
|---|
| 49 |
next unless length; # anything left? |
|---|
| 50 |
my ($var, $value) = split(/\s*=\s*/, $_, 2); |
|---|
| 51 |
$User_Preferences{$var} = $value; |
|---|
| 52 |
} |
|---|
| 53 |
close(FILE); |
|---|
| 54 |
|
|---|
| 55 |
# username to connect to the database |
|---|
| 56 |
$username = $User_Preferences{"AMPDBUSER"} ; |
|---|
| 57 |
# password to connect to the database |
|---|
| 58 |
$password = $User_Preferences{"AMPDBPASS"}; |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
open EXTEN, ">$Backup_cron" or die "Cannot create\/overwrite cron file: $Backup_cron\n"; |
|---|
| 62 |
|
|---|
| 63 |
$dbh = DBI->connect("dbi:mysql:dbname=$database;host=$hostname", "$username", "$password"); |
|---|
| 64 |
|
|---|
| 65 |
$statement = "SELECT Command, ID from $table_name"; |
|---|
| 66 |
|
|---|
| 67 |
$result = $dbh->selectall_arrayref($statement); |
|---|
| 68 |
unless ($result) { |
|---|
| 69 |
# check for errors after every single database call |
|---|
| 70 |
print "dbh->selectall_arrayref($statement) failed!\n"; |
|---|
| 71 |
print "DBI::err=[$DBI::err]\n"; |
|---|
| 72 |
print "DBI::errstr=[$DBI::errstr]\n"; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
@resultSet = @{$result}; |
|---|
| 76 |
if ( $#resultSet == -1 ) { |
|---|
| 77 |
print "No Backup Schedules defined in $table_name\n"; |
|---|
| 78 |
#grab any other cronjobs that are running as asterisk and NOT associated with backups |
|---|
| 79 |
system ("/usr/bin/crontab -l | grep -v ampbackup.pl >> $Backup_cron "); |
|---|
| 80 |
#issue the schedule to the cron scheduler |
|---|
| 81 |
system ("/usr/bin/crontab $Backup_cron"); |
|---|
| 82 |
exit; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
foreach my $row ( @{ $result } ) { |
|---|
| 86 |
my $Backup_Command = @{ $row }[0]; |
|---|
| 87 |
my $Backup_ID = @{ $row }[1]; |
|---|
| 88 |
print EXTEN "$Backup_Command $Backup_ID\n"; |
|---|
| 89 |
} |
|---|
| 90 |
#grab any other cronjobs that are running as asterisk and NOT associated with backups |
|---|
| 91 |
system ("/usr/bin/crontab -l | grep -v ampbackup.pl >> $Backup_cron "); |
|---|
| 92 |
#issue the schedule to the cron scheduler |
|---|
| 93 |
system ("/usr/bin/crontab $Backup_cron"); |
|---|
| 94 |
|
|---|
| 95 |
exit 0; |
|---|