cPanel backup script to Amazon S3

For those using cPanel, here’s a basic backup script nzgeoff posted at the VPS.NET clients-only forums.
This script will create individual tar.gz backups of all your cPanel user accounts and upload them to your S3 bucket. It won’t delete the local backups until the next time the scripts runs.

Prerequisites

This script requires php-cli and the aws perl script from http://timkay.com/aws/
Here are the steps:

yum install curl
curl github.com/timkay/aws/raw/master/aws -o aws
perl aws --install

Now put your AWS credentials in ~/.awssecret: the Access Key ID on the first line and the Secret Access Key on the second line like this:

1B5JYHPQCXW13GWKHAG2
2GAHKWG3+1wxcqyhpj5b1Ggqc0TIxj21DKkidjfz

The script

Open up nano on your server and create a file eg /root/backups/backup.php and copy the following code.

Just change your email address ($emailaddy) and bucket name ($s3bucket) into your own and you're done.

Cron job

Open up your crontab by running crontab -e and paste the following code. In this example the backup script will run daily at 3am.

0 3 * * * root php /root/backups/backup.php

A nice, clean, easy and efficient way to backup your cPanel userdata to Amazon S3.

14 thoughts on “cPanel backup script to Amazon S3

  1. Yheng

    There is an error when executing curl:

    Number found where operator expected at aws line 2, near “301”
    (Missing operator before 301?)
    Bareword found where operator expected at aws line 2, near “301 Moved”
    (Missing operator before Moved?)
    Bareword found where operator expected at aws line 4, near “301 Moved Permanently</h1"
    (Might be a runaway multi-line // string starting on line 2)
    (Missing operator before h1?)
    Bareword found where operator expected at aws line 5, near "nginx”
    (Missing operator before nginx?)
    Bareword found where operator expected at aws line 6, near “”
    Execution of aws aborted due to compilation errors.

    Any help?

    Reply
    1. George Tasioulis Post author

      Could you please try again, by copy-pasting the following line? Your error is just because of a misspelled URL (301 is a HTTP Status code).

      curl github.com/timkay/aws/raw/master/aws -o aws

      Thanks

      Reply
  2. Snick

    I tried just to execute the script ./backup.php after chmod +x
    but it would not run is there a way to test this without crontab?

    ./backup.php: line 1: ?php: No such file or directory
    ./backup.php: line 3: /aquota.user: Text file busy
    ./backup.php: line 4: backup.php: command not found
    ./backup.php: line 5: backup.php: command not found
    ./backup.php: line 6: backup.php: command not found
    ./backup.php: line 7: backup.php: command not found
    ./backup.php: line 8: backup.php: command not found
    ./backup.php: line 9: backup.php: command not found
    ./backup.php: line 10: backup.php: command not found
    ./backup.php: line 11: backup.php: command not found
    ./backup.php: line 12: backup.php: command not found
    ./backup.php: line 13: */: No such file or directory
    ./backup.php: line 15: syntax error near unexpected token `0′
    ./backup.php: line 15: `set_time_limit(0);’

    Reply
  3. S3 Browser Team

    Nice script.

    We are using similar script for cPanel backups.

    It seems to be a bit simpler. s3cmd tool is required only.

    #!/bin/sh

    NOWDATE=`date +%Y-%m-%d`

    # backup everything
    tar -czvf $NOWDATE.tar.gz ../*

    # upload backed up files
    s3cmd/s3cmd-0.9.9/s3cmd put ./$NOWDATE.tar.gz s3://your_bucket_name/$NOWDATE/

    # dump all databases
    mysqldump –user=user –password=password –all-databases > all-databases.sql

    # upload all databases
    s3cmd/s3cmd-0.9.9/s3cmd put ./all-databases.sql s3://your_bucket_name/$NOWDATE/

    # remove archive
    rm $NOWDATE.tar.gz

    # remove databases dump
    rm all-databases.sql

    Reply
  4. linkway hosting

    Fantastic, we are building a script which will automatically transfer cpanel cpmove file to amazon s3, prior authentication will take place with amazon s3 and bucket selection.
    I will inform here once it is done, BTW, thanks for your script

    Reply
  5. Ronnie

    Hello,
    Thanks for the information But does this script work for us too who are using share hosting servers on CPanel? your response will be highly appreciated

    Thank you
    Ronnie

    Reply
  6. Rick

    Any idea how you’d set this to use the Reduced Redundancy Storage? Cant seem to find any info about it on Tom Kay’s site.

    Reply
  7. Ronnie

    Hello,
    Iam just wondering if you already found a script that can help backup cpanel websites on shared hosting

    I will be happy to hear from you

    Regards,
    Ronnie

    Reply
    1. George Tasioulis Post author

      Hello Ronnie,

      I’m afraid I haven’t – as I run my own cPanel servers I have root access on every one. However, I have moved to R1Soft Backups for a long time now (and never looked back).
      Root access to the cPanel server is required for R1Soft Backups as well however.

      – G.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.