Guest User

Untitled

a guest
Nov 2nd, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.81 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. # ampbackup.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 to pick up the backup sets name and options
  6. # Then it creates the tar files and places them in the /var/lib/asterisk/backups folder
  7. #
  8. # The program if run from asterisk users crontab it is run as ampbackup.pl <Backup Job Record Number in Mysql>
  9. # OR
  10. # The program is called from the backup.php script and implemented immediately as such:
  11. # ampbackup.pl <Backup_Name> <Backup_Voicemail_(yes/no)> <Backup_Recordings_(yes/no)> <Backup_Configuration_files(yes/no)>
  12. # <Backup_CDR_(yes/no)> <Backup_FOP_(yes/no)
  13. #
  14. # example ampbackup.pl "My_Nightly_Backup" yes yes no no yes
  15. #
  16.  
  17. # This program is free software; you can redistribute it and/or
  18. # modify it under the terms of the GNU General Public License
  19. # as published by the Free Software Foundation; either version 2
  20. # of the License, or (at your option) any later version.
  21.  
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. # GNU General Public License for more details.
  26.  
  27.  
  28. use DBI;
  29.  
  30. if (scalar @ARGV < 1)
  31. {
  32.          print "Usage: $0 Backup-set-ID \n";
  33.          print "  This script Reads the backup options from the BackupTable.\n";
  34.          print "    then runs the backup picking up the items that were turned\n";
  35.          print "    OR\n";
  36.          print "    \n";
  37.          print "    The program is called from the backup.php script and implemented immediately as such:\n";
  38.          print "    ampbackup.pl <Backup_Name> <Backup_Voicemail_(yes/no)> <Backup_Recordings_(yes/no)> <Backup_Configuration_files(yes/no)>\n";
  39.          print "    <Backup_CDR_(yes/no)> <Backup_FOP_(yes/no)\n";
  40.          print "    \n";
  41.          print "    example ampbackup.pl \"My_Nightly_Backup\" yes yes no no yes\n";
  42.          exit(1);
  43. }
  44.  
  45. ################### BEGIN OF CONFIGURATION ####################
  46.  
  47. # the name of the extensions table
  48. $table_name = "Backup";
  49. # WARNING: this file will be overwritten by the output of this program
  50. # the name of the box the MySQL database is running on
  51. $hostname = $amp_conf['AMPDBHOST'];
  52. # the name of the database our tables are kept
  53. # Now taken from $User_Preferences
  54. # $database = "asterisk";
  55. # scratch file to ftp results with
  56. $ftpfile = "/tmp/freepbx-backup.ftp";
  57.  
  58. # Set Default if not defined below
  59. $User_Preferences{"AMPDBUSER"}  = "asteriskuser";
  60. $User_Preferences{"AMPDBPASS"}  = "amp109";
  61. $User_Preferences{"AMPDBNAME"}  = "asterisk";
  62. $User_Preferences{"AMPWEBROOT"} = "/var/www/html";
  63. $User_Preferences{"ASTETCDIR"} = "/var/opt/ippbx/etc/asterisk";
  64.  
  65. $User_Preferences{"AMPPROVROOT"} = "";
  66. $User_Preferences{"AMPPROVEXCLUDE"} = "";
  67.  
  68. $User_Preferences{"FTPBACKUP"} = "";
  69. $User_Preferences{"FTPUSER"} = "";
  70. $User_Preferences{"FTPPASSWORD"} = "";
  71. $User_Preferences{"FTPSUBDIR"} = "";
  72. $User_Preferences{"FTPSERVER"} = "";
  73.  
  74. $User_Preferences{"SSHBACKUP"} = "";
  75. $User_Preferences{"SSHUSER"} = "";
  76. $User_Preferences{"SSHRSAKEY"} = "";
  77. $User_Preferences{"SSHSUBDIR"} = "";
  78. $User_Preferences{"SSHSERVER"} = "";
  79.  
  80. open(FILE, "/etc/amportal.conf") || die "Failed to open amportal.conf\n";
  81. while (<FILE>) {
  82.     chomp;                  # no newline
  83.     s/#.*//;                # no comments
  84.     s/^\s+//;               # no leading white
  85.     s/\s+$//;               # no trailing white
  86.     next unless length;     # anything left?
  87.     my ($var, $value) = split(/\s*=\s*/, $_, 2);
  88.     $User_Preferences{$var} = $value;
  89. }
  90. close(FILE);
  91.  
  92. open(FILE, $User_Preferences{"ASTETCDIR"}."/asterisk.conf") || die "Failed to open asterisk.conf\n";
  93. while (<FILE>) {
  94.     chomp;
  95.     s/\s//g;    # No spaces, anywhere.
  96.     s/#.*//;    # no comments
  97.     s/^\s+//;   # no leading white
  98.     s/\s+$//;   # no trailing white
  99.     if (/(.+)=>(.+)/) {
  100.         $ast{$1} = $2;
  101.     }
  102. }
  103.  
  104. $backupdir = $ast{'astvarlibdir'}."/backups";
  105.  
  106. # username to connect to the database
  107. $username = $User_Preferences{"AMPDBUSER"} ;
  108. # password to connect to the database
  109. $password = $User_Preferences{"AMPDBPASS"};
  110. # Database name
  111. $database = $User_Preferences{"AMPDBNAME"};
  112. $databasecdr = $User_Preferences{"AMPDBNAMECDR"};
  113. $databaseqstat = $User_Preferences{"AMPDBNAMEQSTAT"};
  114. # Database host
  115. $hostname = $User_Preferences{"AMPDBHOST"};
  116. # the WEB ROOT directory
  117. $webroot = $User_Preferences{"AMPWEBROOT"};
  118.  
  119. # Provisioning root(s) and exclude list, if phone configuratoins should be backed up
  120. #
  121. $provroot = $User_Preferences{"AMPPROVROOT"};
  122. $excludefile = $User_Preferences{"AMPPROVEXCLUDE"};
  123.  
  124. # If and where to send the backup file once created (still left on local machine as well)
  125. #
  126. $ftpbackup = uc $User_Preferences{"FTPBACKUP"};
  127. $ftpuser = $User_Preferences{"FTPUSER"};
  128. $ftppassword = $User_Preferences{"FTPPASSWORD"};
  129. $ftpsubdir = $User_Preferences{"FTPSUBDIR"};
  130. $ftpserver = $User_Preferences{"FTPSERVER"};
  131.  
  132. $sshbackup = uc $User_Preferences{"SSHBACKUP"};
  133. $sshuser = $User_Preferences{"SSHUSER"};
  134. $sshrsakey = $User_Preferences{"SSHRSAKEY"};
  135. $sshsubdir = $User_Preferences{"SSHSUBDIR"};
  136. $sshserver = $User_Preferences{"SSHSERVER"};
  137.  
  138. ################### END OF CONFIGURATION #######################
  139. my $now = localtime time;
  140. my ($sec,$min,$hour,$mday,$mon,$year, $wday,$yday,$isdst) = localtime time;
  141. $year += 1900;
  142. $mon +=1;
  143. #my $Stamp="$year$mon$mday.$hour.$min.$sec";
  144. my $Stamp=sprintf "%04d%02d%02d.%02d.%02d.%02d",$year,$mon,$mday,$hour,$min,$sec;
  145.  
  146. if (scalar @ARGV > 1)
  147. {
  148.     $Backup_Name = $ARGV[0];
  149.     $Backup_Voicemail = $ARGV[1];
  150.     $Backup_Recordings = $ARGV[2];
  151.     $Backup_Configurations = $ARGV[3];
  152.     $Backup_CDR = $ARGV[4];
  153.     $Backup_FOP = $ARGV[5];
  154.    
  155.     #fixme rahman catch parameter FTP/SSH from web
  156.     if(defined($ARGV[6])){
  157.        
  158.         if($ARGV[10] eq "1"){
  159.             $ftpbackup = "YES";
  160.             $ftpuser = $ARGV[7];
  161.             $ftpserver = $ARGV[6];
  162.             $ftppassword = $ARGV[8];
  163.             $ftpsubdir = $ARGV[9];
  164.             if($ftpsubdir eq "0"){
  165.                 $ftpsubdir = "";
  166.             }
  167.         }else{
  168.             $sshbackup = "YES";        
  169.             $sshuser = $ARGV[7];
  170.             $sshserver = $ARGV[6];
  171.             $sshrsakey = $ARGV[8];
  172.             $sshsubdir = $ARGV[9];
  173.             if($sshsubdir eq "0"){
  174.                 $sshsubdir = "";
  175.             }
  176.             $sshport = $ARGV[11];
  177.         }
  178.     }
  179. }
  180. else
  181. {
  182.     $dbh = DBI->connect("dbi:mysql:dbname=$database;host=$hostname", "$username", "$password");
  183.  
  184.     # $statement = "SELECT Name, Voicemail, Recordings, Configurations, CDR, FOP from $table_name where ID= $ARGV[0]";
  185.     $statement = "SELECT Name, Voicemail, Recordings, Configurations, CDR, FOP, server, transfer_method, port, username, password, folder from $table_name where ID= $ARGV[0]";
  186.  
  187.     $result = $dbh->selectall_arrayref($statement);
  188.     unless ($result) {
  189.       # check for errors after every single database call
  190.       print "dbh->selectall_arrayref($statement) failed!\n";
  191.       print "DBI::err=[$DBI::err]\n";
  192.       print "DBI::errstr=[$DBI::errstr]\n";
  193.     }  
  194.  
  195.     @resultSet = @{$result};
  196.     if ( $#resultSet == -1 ) {
  197.       print "No Backup Schedules defined in $table_name\n";
  198.       exit;
  199.     }
  200.  
  201.     foreach my $row ( @{ $result } ) {
  202.         $Backup_Name = @{ $row }[0];
  203.         $Backup_Voicemail = @{ $row }[1];
  204.         $Backup_Recordings = @{ $row }[2];
  205.         $Backup_Configurations = @{ $row }[3];
  206.         $Backup_CDR = @{ $row }[4];
  207.         $Backup_FOP = @{ $row }[5];
  208.        
  209.         #fixme rahman
  210.         $ftpserver = @{ $row }[6];
  211.         $transfer_method = @{ $row }[7];
  212.         $port = @{ $row }[8];
  213.         $ftpuser = @{ $row }[9];
  214.         $ftppassword = @{ $row }[10];
  215.         $ftpsubdir = @{ $row }[11];
  216.         if($ftpsubdir eq "0"){
  217.             $ftpsubdir = "";
  218.         }
  219.                    
  220.         $sshserver = @{ $row }[6];
  221.         $sshuser = @{ $row }[9];
  222.         $sshrsakey = @{ $row }[10];
  223.         $sshsubdir = @{ $row }[11];
  224.         if($sshsubdir eq "0"){
  225.             $sshsubdir = "";
  226.         }
  227.         $sshport = $port;
  228.        
  229.         #print "$Backup_Name $Backup_Voicemail $Backup_Recordings $Backup_Configurations $Backup_CDR $Backup_FOP\n";
  230.     }
  231.     #fixme rahman
  232.     if($ftpserver ne "" || $ftpserver ne "backup"){
  233.         if($transfer_method eq "1"){
  234.             $ftpbackup = "YES";
  235.         }
  236.         if($transfer_method eq "2"){
  237.             $sshbackup = "YES";
  238.         }
  239.     }
  240. }
  241.  
  242.     system ("/bin/rm -rf /tmp/ampbackups.$Stamp > /dev/null  2>&1");
  243.     system ("/bin/mkdir /tmp/ampbackups.$Stamp > /dev/null  2>&1");
  244.     if ( $Backup_Voicemail eq "yes" ){
  245.         system ("/bin/tar -Pcz -f /tmp/ampbackups.$Stamp/voicemail.tar.gz ".$ast{'astspooldir'}."/voicemail");
  246.     }
  247.     if ( $Backup_Recordings eq "yes" ){
  248.         system ("/bin/tar -Pcz -f /tmp/ampbackups.$Stamp/recordings.tar.gz ".$ast{'astvarlibdir'}."/sounds/custom");
  249.     }
  250.     if ( $Backup_Configurations eq "yes" ){
  251.         system ($ast{'astvarlibdir'}."/bin/dumpastdb.php $Stamp > /dev/null");
  252.         system ("/bin/cp -rR /var/opt/ippbx/etc/asterisk /tmp > /dev/null");
  253.         system ("/bin/rm /var/opt/ippbx/etc/asterisk/asterisk.conf > /dev/null");
  254.        
  255.         # fixme anton
  256.         #system ("/bin/tar -Pcz -f /tmp/ampbackups.$Stamp/configurations.tar.gz ".$ast{'astvarlibdir'}."/agi-bin/ ".$ast{'astvarlibdir'}."/bin/ /etc/asterisk $webroot/admin /etc/amportal.conf /tmp/ampbackups.$Stamp/astdb.dump ");
  257.         system ("/bin/tar -Pcz -f /tmp/ampbackups.$Stamp/configurations.tar.gz ".$ast{'astvarlibdir'}."/agi-bin/ ".$ast{'astvarlibdir'}."/bin/ /var/opt/ippbx/etc/asterisk $webroot/admin /etc/amportal.conf /tmp/ampbackups.$Stamp/astdb.dump ".$ast{'astlogdir'}."/queue_log ");
  258.  
  259.         if ($provroot ne "") {
  260.             $excludearg = "";
  261.             if (-r $excludefile) {
  262.                 $excludearg = "--exclude-from $excludefile ";
  263.             }
  264.             system ("/bin/tar -Pcz $excludearg -f /tmp/ampbackups.$Stamp/phoneconfig.tar.gz $provroot ");
  265.         }
  266.         #fixme anton -- database asterisk
  267.         system ("mysqldump --add-drop-table -h $hostname -u $username -p$password --database $database > /tmp/ampbackups.$Stamp/$database.sql");
  268.        
  269.         # fixme anton -- database qstat
  270.         system ("mysqldump --add-drop-table -h $hostname -u $username -p$password --database $databaseqstat > /tmp/ampbackups.$Stamp/$databaseqstat.sql");
  271.  
  272.         # fixme anton -- database wallboard,acdreport & outstats
  273.         system ("mysqldump --add-drop-table -h $hostname -u $username -p$password --database brikerbox_acdreport > /tmp/ampbackups.$Stamp/brikerbox_acdreport.sql");
  274.         system ("mysqldump --add-drop-table -h $hostname -u $username -p$password --database brikerbox_wallboard > /tmp/ampbackups.$Stamp/brikerbox_wallboard.sql");
  275.         system ("mysqldump --add-drop-table -h $hostname -u $username -p$password --database brikerbox_outstats > /tmp/ampbackups.$Stamp/brikerbox_outstats.sql");
  276.         system ("mysqldump --add-drop-table -h $hostname -u $username -p$password --database brikerbox_integration > /tmp/ampbackups.$Stamp/brikerbox_integration.sql");
  277.     }
  278.     if ( $Backup_CDR eq "yes" ){
  279.         system ("/bin/tar -Pcz -f /tmp/ampbackups.$Stamp/cdr.tar.gz $webroot/admin/cdr");
  280.         # fixme anton -- database cdr
  281.         #system ("mysqldump --add-drop-table -h $hostname -u $username -p$password --database brikerbox_cdr > /tmp/ampbackups.$Stamp/asteriskcdr.sql");
  282.         system ("mysqldump --add-drop-table -h $hostname -u $username -p$password --database $databasecdr > /tmp/ampbackups.$Stamp/$databasecdr.sql");
  283.     }
  284.     if ( $Backup_FOP eq "yes" ){
  285.         system ("/bin/tar -Pcz -f /tmp/ampbackups.$Stamp/fop.tar.gz $webroot/panel");
  286.     }
  287.     system ("/bin/mkdir -p '$backupdir/$Backup_Name' > /dev/null  2>&1");
  288.     system ("/bin/tar -Pcz -f '$backupdir/$Backup_Name/$Stamp.tar.gz' /tmp/ampbackups.$Stamp");
  289.     system ("/bin/rm -rf /tmp/ampbackups.$Stamp > /dev/null  2>&1");
  290.     system ("/bin/cp -rR /tmp/asterisk /var/opt/ippbx/etc > /dev/null  2>&1");
  291.     system ("/bin/rm -rf /tmp/asterisk> /dev/null  2>&1");
  292. #
  293. #
  294. # FTP Sucessfull Backup's to FTPSERVER
  295. #
  296. # leave $ftpbackup which gets overwritten next time but can be checked to see if there were errors.
  297. # IMPORTANT - if testing as root, delete files since backup runs as asterisk and will fail here since
  298. #             root leave the file around and asterisk can't overwrite it.
  299. #         Note - the hardcoded full backup that cron does will overwrite each day at destination.
  300. #
  301. if ( $ftpbackup eq "YES" ) {
  302.     open(FILE, ">$ftpfile") || die "Failed to open $ftpfile\n";
  303.  
  304.         printf FILE "user $ftpuser $ftppassword \n";
  305.         printf FILE "binary\n";
  306.             if ( $ftpsubdir ne "" ) {
  307.                 printf FILE "cd $ftpsubdir \n";
  308.             }
  309.             printf FILE "lcd $backupdir/$Backup_Name/\n";
  310.             printf FILE "put $Stamp.tar.gz\n";
  311.             printf FILE "bye\n";
  312.             close(FILE);
  313.  
  314.             system ("ftp -n $ftpserver < $ftpfile > /dev/null  2>&1");
  315.  
  316.             #system ("/bin/rm -rf /tmp/ftp2cabana > /dev/null  2>&1");
  317. }
  318.  
  319. if ( ($sshbackup eq "YES") && ($sshrsakey ne "") && ($sshserver ne "") ) {
  320.  
  321.     if ($sshuser eq "") {
  322.         $sshuser = system("whoami");
  323.     }
  324.     if ($sshsubdir ne "") {
  325.         system("/usr/bin/ssh -o StrictHostKeyChecking=no -p $sshport -i $sshrsakey $sshuser\@$sshserver mkdir -p $sshsubdir");
  326.     }
  327.     system("/usr/bin/scp -o StrictHostKeyChecking=no -P $sshport -i $sshrsakey $backupdir/$Backup_Name/$Stamp.tar.gz $sshuser\@$sshserver:$sshsubdir");
  328. }
  329.  
  330. exit 0;
Add Comment
Please, Sign In to add comment