cPanel Auto Backup via FTPS (Explicit)

a nullvoid blog

cPanel Auto Backup via FTPS (Explicit)

So, you’ve got cPanel Shared Hosting and your host doesn’t provide backups, the data has some value to you, do you trust them not to lose it?

Manually logging in to cPanel and backing up every day is not viable, especially if you have several accounts, FTP or WebDAV is also no good as it only backs-up the files and not your MySQL databases, not to mention its slow if you have a lot of files.

There is a simpler solution perhaps in the form of a PHP script you can find if you search the web that can automatically generate the backup and FTP it somewhere, but from my experience with several hosts I had problems with it, on some hosts it would work but send insecurely via FTP, other hosts it would only generate the backup and never send the file. It did work for some hosts but was unreliable.

The method in this article does not rely on the cPanel Shared Hosting provider to send you the backup, in this method we pull the backup via FTPS (explicit) securely ourselves using curl which every linux machine should have. This method has several benefits including not needing to have a server running with ports exposed to the internet 24/7.

First you want to set up a cron job in your cPanel Shared Hosting to automatically generate a backup daily (or hourly whatever you want), do this by logging in to your cPanel Shared Hosting server and clicking on “Cron Jobs”.

Create a new entry using the following information:

0 4 * * * uapi Backup fullbackup_to_homedir > /dev/null 2>&1

This example will queue a backup generation in cPanel at 4AM every day which will save to your homedir (inaccessible to the public). That’s all we have to do on the shared hosting server.

Next is to set up the simple bash script on our linux machine or VPS to pull the backup.

Log in to your linux box / VPS via SSH or whatever is most convenient and create a new file for our script wherever you like, if you don’t know where I like to create a scripts folder in my user account and put it there. You can do this under root as well if you want but its not necessary.

[user@linux ~]$ mkdir scripts
[user@linux ~]$ nano scripts/pull_cpanel_backup.sh

In this example I have used nano text editor to create the new file, next you can copy and paste this code into your text editor and go through it making changes as necessary, it should be self-explanatory, you should only have to change the variables at the top.

#!/bin/bash
#If there is more than one matching file it will only process the first one it finds.
#NOTE: If the file exists locally it will be replaced!

ftpssite="sharedhost.someprovider.com" #enter your shared hosting server here.
ftpuser="someuser" #this should be your cPanel login details, you can create a separate FTP account if you  prefer.
ftppass="s0mep4ssw0rd"
filename="`curl ftp://$ftpssite --ssl-reqd -k -u $ftpuser:$ftppass --list-only -s | grep -m 1 '^backup-'`" #here you can see SSL is required but we use -k so it doesn't check the certificate if we are using self signed.
destdir="/home/user/backups" #don't add trailing /

if [ -z "$filename" ]
then
    echo "There is no backup file to download."
else
    echo "Downloading $filename..."
    curl ftp://$ftpssite/$filename --ssl-reqd -k -s -u $ftpuser:$ftppass > $destdir/$filename #remove -s if you want to see download progress when running the script.
    if [ $? -eq 0 ]; then
        echo "File successfully downloaded, now deleting the file from the source."
        curl ftp://$ftpssite/$filename --ssl-reqd -k -s -u $ftpuser:$ftppass -Q "DELE $filename"
        echo "Done!"
    else
        echo "$filename was not downloaded successfully, this script will now exit."
    <code>exit 1</code>
    <code>fi</code>
fi

This is not the best script but hey it works, you can set up a cron job to have this script run several times per day instead of just once a day, since if pulling the backup fails say due to poor internet connection, it can try again.

To set up the cron job in command line simply type the below command and add the line to your crontab:

[user@linux ~]$ crontab -e

0 */6 * * * /home/user/scripts/pull_cpanel_backup.sh

That’s it we’re done.

To re-cap we at 4AM every day the cPanel Shared Hosting server will generate a full account backup and our script will look a new backup every 6 hours, download it and delete it off the server.

You probably don’t want to keep full daily backups forever so we can set up another line in cron to automatically delete older backups:

[user@linux ~]$ crontab -e

0 1 * * *    /usr/bin/find /home/user/backups -type f -mmin +7200 -delete

This will automatically delete files in the backups folder older than 5 days (7200 minutes), you can adjust accordingly.

If this article helped you please leave a comment.

Tags: , ,

Leave a Reply

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