To create a script that can automatically log in to your website and download a cPanel backup, a couple of things need to be done on the OS X client computer. Its worth remembering that a backup on a website still really needs a copy off it.
First, if you need to, see an earlier post describing how to back up the website via cPanel.
{a} Create a “.netrc” file in your home directory and give it three properties; machine, login and password – so the format will be:
machine yourdomain.com login yourftplogin password yourftppassword
Can also be formatted as:
machine yourdomain.com login yourftplogin password yourftppassword
If you have multiple websites you can repeat them straight after the sequence. Save the file as .netrc and file it in your home directory root level. This file should only have permissions so that only you can read it – so a “chmod 600 .netrc” should do the trick. Now when you execute the ftp command in the command line to your website no password should be prompted for:
[~] ftp yourdomain.com
An interactive ftp session will occur but you shouldn’t be prompted for a password, quit the session by typing quit.
{b} Create a directory on your local machine where the backups will be copied to such as :
/Users/username/Desktop/websitebackup/
{c} Now we are able to write a script that can download the cPanel backup or any other file for that matter. If a backup download is required a simple script would be:
ftp <<** open yourdomain.com bell prompt lcd /Users/username/Desktop/websitebackup/ cd /backups/daily/ mget * quit
Notes on the script:
So the script has ftp commands, your info needs to change as marked in red, the “lcd” is the local back up directory where the back ups will be copied to, the “cd” is the remote directory where the backups will be copied from, the “mget” will copy all the files in the remote directory as denoted by the “*” this can be changed by using the literal name of the backup, but since the back up name chnages according to the date either a “*” or a “backup*” will be ok, obviously depending on what the backup name is 🙂 .
Save the file as a script; “backup.sh” and save to a location that you can run as a command or as known as ‘somewhere in your path’, for example save it in:
/usr/sbin/backup.sh
{d} The backup script will need to have certain ownership and permissions to run as a script, run the next 2 commands as root to complete this:
sudo chown root:wheel /usr/sbin/backup.sh
sudo chmod 755 /usr/sbin/backup.sh
The resulting permissions should look like:
-rwxr-xr-x@ 1 root wheel 221B Jun 26 15:13 backup.sh
Quit and relaunch the Terminal and now run the command “backup.sh” and you should get a interactive ftp session that will connect, download your files and then disconnect the session on completion. You can run this command whenever you like and to fully automate the process schedule it as a cron or launch agent job.