PHP script for generating cPanel backup and send as email!

Discussion in 'PHP' started by qualityhostings, Oct 19, 2007.

  1. #1
    Hello

    I would like to get some php scripts for the following need.

    The script should check the username/password of that cPanel acccount.
    Then it should take a backup of the whole account including Mysql db as zip or tar.gz

    Then it should email me the file, may be as small size files coz gmail and yahoo is not supporting attachments above 10 MB I think.

    Anybody have any such scripts, please paste the link here.

    Thanks in advance
     
    qualityhostings, Oct 19, 2007 IP
  2. Edynas

    Edynas Peon

    Messages:
    796
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #2
    isn't it a standard cpanel function? if so it would be a matter of writing a curl function and execute that.

    Also at hotscrips there is a readymade script that does it but instead of emailing it ftp's it to an other erver. Maybe you can modify that http://cpsafe.smccandl.net/
     
    Edynas, Oct 19, 2007 IP
  3. qualityhostings

    qualityhostings Well-Known Member

    Messages:
    1,764
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    105
    #3
    No.Its not the standard cpanel function. The std cpanel fn is to generate backup and store it locally or send to another server using ftp.
    I need a script to send the generated file as email. Is it possible?

    I have 2 scripts in my hand. One can take backup and send via ftp using the cpanel function as backend. The other is, it can zip the foldr public_html and can send as email by cutting into as many part as we want.
    But it can not take email backup and mysql backup.

    Any idea?

     
    qualityhostings, Oct 20, 2007 IP
  4. Edynas

    Edynas Peon

    Messages:
    796
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I suggest you do it in 2 steps...1 make an automated backup and store it local and 2 make a script that takes the backup and sends an email to you every day or whenever you want it to happen.

    You will need 2 scripts and a crontab for those two. The one that takes the file and sends an email shouldn't be a problem. The first that makes the backup...I must admit i never used it in this way but ...... here is a piece of code to login cpanel using php and create subdomains

    
    <?php
    
    ###############################################################
    # cPanel Subdomains Creator 1.1
    ###############################################################
    # Visit http://www.zubrag.com/scripts/ for updates
    ###############################################################
    #
    # Can be used in 3 ways:
    # 1. just open script in browser and fill the form
    # 2. pass all info via url and form will not appear
    # Sample: cpanel_subdomains.php?cpaneluser=USER&cpanelpass=PASSWORD&domain=DOMAIN&subdomain=SUBDOMAIN
    # 3. list subdomains in file. In this case you must provide all the defaults below
    #
    # Note: you can omit any parameter, except "subdomain".
    # When omitted, default value specified below will be taken
    ###############################################################
    
    // cpanel user
    define('CPANELUSER','user');
    
    // cpanel password
    define('CPANELPASS','pass');
    
    // name of the subdomains list file.
    // file format may be 1 column or 2 columns divided with semicilon (;)
    // Example for two columns:
    //   rootdomain1;subdomain1
    //   rootdomain1;subdomain2
    // Example for one columns:
    //   subdomain1
    //   subdomain2
    define('INPUT_FILE','domains.txt');
    
    // cPanel skin (mainly "x")
    // Check http://www.zubrag.com/articles/determine-cpanel-skin.php
    // to know it for sure
    define('CPANEL_SKIN','x');
    
    // Default domain (subdomains will be created for this domain)
    // Will be used if not passed via parameter and not set in subdomains file
    define('DOMAIN','');
    
    
    /////////////// END OF INITIAL SETTINGS ////////////////////////
    ////////////////////////////////////////////////////////////////
    
    function getVar($name, $def = '') {
      if (isset($_REQUEST[$name]) && ($_REQUEST[$name] != ''))
        return $_REQUEST[$name];
      else 
        return $def;
    }
    
    $cpaneluser=getVar('cpaneluser', CPANELUSER);
    $cpanelpass=getVar('cpanelpass', CPANELPASS);
    $cpanel_skin = getVar('cpanelskin', CPANEL_SKIN);
    
    if (isset($_REQUEST["subdomain"])) {
      // get parameters passed via URL or form, emulate string from file 
      $doms = array( getVar('domain', DOMAIN) . ";" . $_REQUEST["subdomain"]);
      if (getVar('domain', DOMAIN) == '') die("You must specify domain name");
    }
    else {
      // open file with domains list
      $doms = @file(INPUT_FILE);
      if (!$doms) {
        // file does not exist, show input form
        echo "
    Cannot find input file with subdomains information. It is ok if you are not creating subdomains from file.<br>
    Tip: leave field empty to use default value you have specified in the script's code.<br>
    <form method='post'>
      Subdomain:<input name='subdomain'><br>
      Domain:<input name='domain'><br>
      cPanel User:<input name='cpaneluser'><br>
      cPanel Password:<input name='cpanelpass'><br>
      cPanel Skin:<input name='cpanelskin'><br>
      <input type='submit' value='Create Subdomain' style='border:1px solid black'>
    </form>";
        die();
      }
    }
    
    // create subdomain
    function subd($host,$port,$ownername,$passw,$request) {
    
      $sock = fsockopen('localhost',2082);
      if(!$sock) {
        print('Socket error');
        exit();
      }
    
      $authstr = "$ownername:$passw";
      $pass = base64_encode($authstr);
      $in = "GET $request\r\n";
      $in .= "HTTP/1.0\r\n";
      $in .= "Host:$host\r\n";
      $in .= "Authorization: Basic $pass\r\n";
      $in .= "\r\n";
     
      fputs($sock, $in);
      while (!feof($sock)) {
        $result .= fgets ($sock,128);
      }
      fclose( $sock );
    
      return $result;
    }
    
    foreach($doms as $dom) {
      $lines = explode(';',$dom);
      if (count($lines) == 2) {
        // domain and subdomain passed
        $domain = trim($lines[0]);
        $subd = trim($lines[1]);
      }
      else {
        // only subdomain passed
        $domain = getVar('domain', DOMAIN);
        $subd = trim($lines[0]);
      }
      // http://[domainhere]:2082/frontend/x/subdomain/doadddomain.html?domain=[subdomain here]&rootdomain=[domain here]
      $request = "/frontend/$cpanel_skin/subdomain/doadddomain.html?rootdomain=$domain&domain=$subd";
      $result = subd('localhost',2082,$cpaneluser,$cpanelpass,$request);
      $show = strip_tags($result);
      echo $show;
    }
    
    ?>
    
    PHP:
    Now the basics are the same...here you login and goto the addon domains and post the request ...you should alter it

    the page that does the fullbackup is at http://www.yourdomain.com:2082/frontend/x/backup/fullbackup.html and if you look at the source you will see the form looks like this

    
    <form action="dofullbackup.html" method=POST ENCTYPE="multipart/form-data">
    <font class="med">Backup Destination:</font> <select name=dest>
    <option value="homedir">Home Directory</option>
    
    <option value="ftp">Remote FTP Server</option>
    <option value="passiveftp">Remove FTP Server (Passive mode transfer)</option>
    <option value="scp">Secure Copy (scp)</option>
    </select><br>
    <font class="med">Email Address:</font> <input type=text name="email" value="hosting@printcoach.nl"><br>
    <font class="med"><b>Remote Server (FTP/SCP only):</font> <input type=text name=server></b><br>
    <font class="med"><b>Remote User (FTP/SCP only):</font> <input type=text name=user></b><br>
    <font class="med"><b>Remote Password (FTP/SCP only):</font> <input type=password name=pass></b><br>
    
    <font class="med">Port (FTP/SCP only)</font> <input type=text name=port><br>
    <font class="med">Remote Dir (FTP/SCP only)</font> <input type=text name=rdir><br>
    <br>
    <input type="submit" value="Generate Backup">
    </form>
    
    PHP:
    so it will be something like this...and please look at it as i write this without testing

    
    <?php
    
    // cpanel user
    define('CPANELUSER','user');
    
    // cpanel password
    define('CPANELPASS','pass');
    
    // cPanel skin (mainly "x")
    define('CPANEL_SKIN','x');
    
    $cpaneluser=getVar('cpaneluser', CPANELUSER);
    $cpanelpass=getVar('cpanelpass', CPANELPASS);
    $cpanel_skin = getVar('cpanelskin', CPANEL_SKIN);
    $email = "your@mail.com";
      
    
    
    function backup($host,$port,$ownername,$passw,$request) {
    
      $sock = fsockopen('localhost',2082);
      if(!$sock) {
        print('Socket error');
        exit();
      }
    
      $authstr = "$ownername:$passw";
      $pass = base64_encode($authstr);
      $in = "GET $request\r\n";
      $in .= "HTTP/1.0\r\n";
      $in .= "Host:$host\r\n";
      $in .= "Authorization: Basic $pass\r\n";
      $in .= "\r\n";
     
      fputs($sock, $in);
      while (!feof($sock)) {
        $result .= fgets ($sock,128);
      }
      fclose( $sock );
    
      return $result;
    }
    
    
      $request = "/frontend/$cpanel_skin/subdomain/dofullbackup.html?dest=homedir&email=$email";
      $result = backup('localhost',2082,$cpaneluser,$cpanelpass,$request);
      $show = strip_tags($result);
      echo $show;
    
    ?>
    
    PHP:
    My logic says this should work but please do test it and I am not responsible for anything so make a backup before you use it :)
     
    Edynas, Oct 20, 2007 IP
  5. qualityhostings

    qualityhostings Well-Known Member

    Messages:
    1,764
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    105
    #5
    Hello

    Thank you for your effort. But I had already thought about this. But the problem is like this.


    If we generate a backup file using the cPanel Backup function then the file name should be something like this.

    backup-9.9.2007_06-16-40_username.tar.gz

    The file name includes date and time.

    You said that, First generate a backup, and then attach it ,send mail.
    But how to know that file name ? It varies by time to time.

    Any idea ?
     
    qualityhostings, Oct 20, 2007 IP
  6. qualityhostings

    qualityhostings Well-Known Member

    Messages:
    1,764
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    105
    #6
    Update :

    I asked for a script that can send the generated backup file as email.

    There is 2 problem as far as I know.

    1. How to get the generated backup file name.?
    2. It will take time to generate a backup if the total size is big.Then the php file has to wait for a long time for attaching the file.

    Not sure, it is possible or not.
     
    qualityhostings, Oct 20, 2007 IP
  7. qualityhostings

    qualityhostings Well-Known Member

    Messages:
    1,764
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    105
    #7
    Ok, I now have a new idea.

    I want to backup the files and the databases seperately

    I think this will solve my problem

    Because, I already have a script for backing up public_html

    Now I need a script for backing up ALL THE DATABASES with the cPanel password(please note, with the cpanel password, not with the each database password

    Can anyone help me ?
     
    qualityhostings, Oct 20, 2007 IP
  8. Edynas

    Edynas Peon

    Messages:
    796
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #8
    the two problems are easily tackeled (time delay and datebased) but i get the feeling you rather have everything readymade ... :(
     
    Edynas, Oct 20, 2007 IP