Results 1 to 7 of 7

Thread: Problem in Automated Script for backup

  1. #1
    Join Date
    Apr 2010
    Posts
    89

    Problem in Automated Script for backup

    I have provided the script below which i am using to automatically back up all accounts on my sellertwo plan to a remote server trough ftp. Actually it is working for backing up an account individually but an error comes when i try to do the same with the latest cpanel release.
    Code:
    /*** BEGIN CONFIG ***/
    $sellertwo = 'sellertwo.com';
    $whmUser = 'sellertwouser';
    $whmPass = 'sellertwopass';
    $bkUpsellertwo = true;
    $sellertwoCpTheme = 'x3';
    /*
    Destination for the backup...
    $destinType = '/';
    $destinType = 'ftp';
    $destinType = 'passiveftp'; 
    */
    $destinType = 'ftp';
    $destinServer = 'ftp.destin.com';//only if $destinServer = 'ftp' or 'passiveftp'
    $destinUser = 'ftpuser';//only if $destinServer = 'ftp' or 'passiveftp'
    $destinPass = 'ftppass';//only if $destinServer = 'ftp' or 'passiveftp'
    $destinEmail = 'mail@whateveremail.com';
    $logType = 'file';
    $logFileName = 'backup_full.txt';
    $useSsl = false;
    $day = strtolower(date('l'));
    $dst_dir = $day;
    $dir = $dst_dir;
    $conn_id = ftp_connect($Server) or die("Couldn't connect to $destinServer");destin
    if (@ftp_login($conn_id, $destinUser, $destinPass))
     { echo "Connected as $destinUser@$destinServer\n";
      $ar_files = ftp_nlist($conn_id, $dst_dir);
     if (is_array($ar_files))
      {  for ($i=0;$i<sizeof($ar_files);$i++)
       {   $st_file = basename($ar_files[$i]);
       if($st_file == '.' || $st_file == '..') continue;
       if (ftp_size($conn_id, $dst_dir.'/'.$st_file) == -1)
        {     ftp_rmAll($conn_id,  $dst_dir.'/'.$st_file); 
        }
       else    {    ftp_delete($conn_id,  $dst_dir.'/'.$st_file); 
        }   }  }
     $flag = ftp_rmdir($conn_id, $dst_dir); 
     }
    else { header ("location : backup_full.php");
     }
     if ( @ftp_chdir( $conn_id, $dir ) )
     { $original_directory = ftp_pwd( $conn_id );
           ftp_chdir( $conn_id, $original_directory );
     }
    else { if (ftp_mkdir($conn_id, $dir))
      {  echo '<p>successfully created '.$dir.'</p>';
      }
     else  {  echo '<p>There was a problem while creating '.$dir.'</p>';
      }
     }
    ftp_close($conn_id);
    $domains = 'example1.com,example2.net';
    $domains = '';
    set_time_limit(0);
    $time_start = getMicroTime();
    if (!extension_loaded('curl')) {
      dl('php_curl.' .PHP_SHLIB_SUFFIX) or die("Could not load curl extension");
    }
    if($useSsl) {  $protocol = 'https://';
      $cpPort = '2083';
      $whmPort = '2087';
      writeLog("Using SSL.\n");
    }
    else {
      $protocol = 'http://';
      $cpPort = '2082';
      $whmPort = '2086';
      writeLog("Not using SSL.\n");
    } 
    $domains = trim($domains);
    if(!empty($domains)){
      $chkDoms = true;
      $domains = explode(',',$domains);
      foreach($domains as $d){
        $domains[] = trim($d);
      }
    }
    else {
      $chkDoms = false;
      $domains = array();
    }
    //$postFields = urlencode("destin=$destinType&server=$destinServer&user=$destinPass&pass=$destinPass&email=$destinEmail");
    $postFields = '';
    $postArray = array();
    $postArray['destin'] = $destinType;
    $postArray['server'] = $destinServer;
    $postArray['user'] = $destinUser;
    $postArray['pass'] = $destinPass;
    $postArray['email'] = $destinEmail;
    $postArray['rdir'] = $day;
    $thingy = '';
    foreach ($postArray as $k=>$v)
    {
      $thingy.= "$k=".utf8_encode($v).'&';
    }
    $postFields = substr($thingy,0,-1);
     
    writeLog("Retrieving WHM accounts page...\n");
    $accountsPage = getByCurl("$protocol$sellertwo:$whmPort/scripts/fetchcsv",$whmUser,$whmPass);
    //var_dump($accountsPage );
    $accounts = array();
    $accounts = explode("\n", trim($accountsPage ));
    //var_dump($accounts);
    $account_records = array();
    foreach ($accounts as $row) {
     $account_records[] = explode(',', $row);
    }
    writeLog("Parsing accounts...\n");
    //var_dump($account_records);
    //die('here');
    foreach($account_records as $match) {
        $accountDom= '';
        $accountUser= '';
        $accountTheme = '';
        $r = '';
        $fullUrl = '';
        $accountDom= strip_tags(trim($match[1]));
        if($chkDoms)
          if(!in_array($accountDom,$domains)) continue;
        $accountUser= strip_tags(trim($match[3]));
        $accountTheme = strip_tags(trim($match[14]));
     
        $fullUrl = "$protocol$accountDom:$cpPort/frontend/$accountTheme/backup/dofullbackup.html";
        writeLog("Requesting backup of $accountDom...\n");
        $r =& getByCurl($fullUrl,$accountUser,$whmPass,array('CURLOPT_POST'=>$postFields));
        if($r === false){
          writeLog("Backup request of $accountDomcaused an unknown error.\n");
        }
        else {
          writeLog("Backup request of $accountDomcomplete.\n");
        }
        //writeLog("\n\n--$accountDom--\n\n$r\n\n------\n\n");
    }
    if($bkUpsellertwo){
      $fullUrl = "$protocol$sellertwo:$cpPort/frontend/$sellertwoCpTheme/backup/dofullbackup.html";
      writeLog("Requesting backup of $sellertwo...\n");
      $r =& getByCurl($fullUrl,$accountUser,$whmPass,array('CURLOPT_POST'=>$postFields));
    }
    $time_end = getMicroTime();
    $time = $time_end - $time_start;
    writeLog("Elapsed time: ".round($time,2)." seconds.\n");
    
    function getByCurl($url, $user = '', $pass = '',$extra = '') {
      global $useSsl;
      $ch = curl_init();
      //tells curl to save result in a variable instead of outputing to page
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_URL, $url);  
      curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
      curl_setopt ($ch, CURLOPT_COOKIEJAR, './cookie.txt');
      curl_setopt ($ch, CURLOPT_FOLLOWLOCATION,1);
      if(!empty($extra) && is_array($extra)){
        foreach($extra as $opt=>$val){
          switch($opt){
            case 'CURLOPT_REFERER':
              curl_setopt($ch,CURLOPT_REFERER,$val);
            break;
            case 'CURLOPT_POST':
            case 'CURLOPT_POSTFIELDS':
              curl_setopt($ch,CURLOPT_POST,1);
              curl_setopt($ch,CURLOPT_POSTFIELDS,$val);
            break;
          }
        }
      }
      if($useSsl){
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
      }
      $result = curl_exec($ch);
      curl_close($ch);
      return $result;
    }
    function writeLog($entry) {
      global $logType,$logFileName;
      $method = strtolower($logType);
      $entry = date('r').' - '.$entry;
     
      if($method == 'file') {
        $fp = fopen($logFileName,'ab');
     fwrite($fp, $entry);
     fclose($fp);
      } elseif($method == 'echo'){
          echo nl2br($entry);//browser
     
          flush();
      }
      return;
    }
    function getMicroTime(){
      list($usec, $sec) = explode(" ",microtime());
      return ((float)$usec + (float)$sec);
    }
    
    $text = file_get_contents('backup_full.txt.txt');
    echo $text;
    $where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
    $message = stripslashes($text);
    mail("email_address","Full Backup Initiated",$message,"from_email_address");
    unlink('backup_full.txt.txt');
    ?>
    Last edited by Knowle; 16-06-2010 at 12:59 AM.

  2. #2
    Join Date
    Apr 2008
    Posts
    3,267

    Re: Problem in Automated Script for backup

    I have done some changes in the script and you can check whether it works for you or not. Actually i do not have much idea about it but still i have tried it and i guess it must be working for you or i hope so that it will at least give you some kind of hint.
    Code:
    $accountsPage = getByCurl("$protocol$sellertwo:$whmPort/scripts/fetchcsv",$whmUser,$whmPass)
    Last edited by MrChris-; 16-06-2010 at 12:45 AM.

  3. #3
    Join Date
    Nov 2005
    Posts
    3,026

    Re: Problem in Automated Script for backup

    Actually an array variables are in a different order in the latest release of CPanel and due to which variables are assigned incorrectly when the script continues

    $accountDom = strip_tags(trim($match[1])); to> $accountDom= strip_tags(trim($match[0]));

    $accountUser = strip_tags(trim($match[3])); to $accountUser = strip_tags(trim($match[2]));//username

    $accountTheme = strip_tags(trim($match[14]));
    $accountTheme = strip_tags(trim($match[9]));

    Additionally, I had to define $destinPort = '21'; and add it to the array for the remote connection.

  4. #4
    Join Date
    Apr 2008
    Posts
    3,522

    Re: Problem in Automated Script for backup

    I think you should not use any automated script in order to backup any data as it might happen that those script could make your backup visible only foe some time then that backup might become of no use or there could come any kind of problem. According to me you should always some software which can backup for you and you just need to do some clicks, That's all!!!

  5. #5
    Join Date
    Apr 2008
    Posts
    3,295

    Re: Problem in Automated Script for backup

    Full backups of your dedicated servers easier to manage and easy to use thanks to the web interface. Ask for their activation as an option for your dedicated server. The packages offer up to 500 GB of backup space. Powerful dedicated backup server, up to 4TB backup space and 25 client-server licenses. Provides an all-in-one, to ensure backups of multiple servers hosted at iWeb or elsewhere. Adopt dedicated servers for total control of your backup solutions.

  6. #6
    Dr. V Guest

    Re: Problem in Automated Script for backup

    The product lines dedicated servers cheap and pro iWeb can be extended to use multiple disks and a RAID controller to build a custom server archiving terabytes. Server Solutions Database Mirroring iWeb resolve your problems of MySQL backup and allow you to access a backup of your database replicated in real time. Storage solutions iWeb provide connections from multiple servers by allowing them to share data among all your servers. These servers are essential for advanced solutions for high availability and hosting solutions for the enterprise.

  7. #7
    Join Date
    May 2008
    Posts
    2,680

    Re: Problem in Automated Script for backup

    mysql and mysqldump Both commands are available in mysql that will allow us to perform a backup or restore! To make a dump of the database mydb just run the mysqldump command
    1.mysqldump-h192.168.1.10-uroot pp455wd-> backupmybase.sql
    where backupmybase.sql and the file that contains the backup and will be created after execution of mysqldump.
    Restoring a database from a backup via the mysql command itself.
    1.mysql-h192.168.1.10-uroot-pp455wd <mybackupbase.sql
    Back up all databases in a single command
    1.mysqldump - user = root - password = p455wd - all-databases | gzip> all_mysql_databases.sql.gz
    Create a backup script to make a backup of all databases
    Sometimes you need to make regular backups without installing special tools on our servers as cpanel or other, I created this little script that you can do that with the ability to delete old backup dates X days (10 in our case). Just create a file containing the following code mysqlbackup.sh
    view sourceprint?
    Code:
    01.# Address of server
    02.mysql_host ="192.168.1.10"
    03.# User mysql
    04.mysql_user ="Mcherifi"
    05.# Mysql password
    06.MYSQL_PWD ="P455wd"
    07.# Directory backup
    08.BACKUP_DIR ="/ Home / simo /"
    09.# Number of days for removal of old backups
    10.delete_after = 10
    11. 
    12.# START OF SCRIPT
    13.date_debut_sauvegarde ="$ (Date +"% D-% m-% Y")"
    14.current_backupdir =$ BACKUP_DIR"/"$ Date_debut_sauvegarde
    15. 
    16.[! Ie $ BACKUP_DIR  ] && mkdir -P $ BACKUP_DIR  || :
    17.[! Ie $ Current_backupdir  ] && mkdir -P $ Current_backupdir  || :
    18. 
    19.find $ () * BACKUP_DIR-mtime +$ Delete_after -exec rm-f () \;
    20.for i in `mysqlshow-h $ Mysql_host - User =$ Mysql_user - Password =$ MYSQL_PWD | Awk-F "^| " '(Print $ 2)' | Grep-v '^$'`
    21.do
    22.mysqldump-h $ Mysql_host - User =$ Mysql_user - Password =$ MYSQL_PWD - Add-locks - add-drop-table $ I | Gzip> $ Current_backupdir"/"$ I. Sql.gz | grep '^$'
    23.done;

Similar Threads

  1. How to backup WLC configuration script?
    By ChittiBabu in forum Networking & Security
    Replies: 4
    Last Post: 31-12-2011, 04:11 PM
  2. Replies: 1
    Last Post: 20-04-2011, 06:15 PM
  3. Replies: 2
    Last Post: 14-01-2009, 01:25 PM
  4. Clickfree Transformer For Automated Data Backup
    By admil in forum Hardware Peripherals
    Replies: 2
    Last Post: 06-01-2009, 11:56 AM
  5. Security Event Log Backup via script
    By Omar in forum Windows Security
    Replies: 2
    Last Post: 05-03-2008, 10:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,309,102.74670 seconds with 17 queries