cPanel, Automatically copying backups to Box

If you want to automatically copy your cPanel website backups to Box, these instructions will show you how.

There are five steps to the process:

Step 1: Prepare your backup and remote location

Step 1.1: Create and/or locate the backups you want to copy to Box

cPanel's Softaculous system allows automated backups of common content management systems such as WordPress and Drupal, and cPanel itself can create full account and database backups of its own. cPanel, Backup and restore of your website explains more about the types of backups available and where their controls are located. We have also included patterns below to automatically create backups from your script.

Your softaculous backups are not available through the cPanel File Manager. To find your softaculous backups:

  1. Under the Advanced section of your cPanel dashboard, click Terminal to launch a command line.
  2. type "cd softaculous_backups"
  3. type the letters "ls"
  4. A listing of your backup files are shown. Filenames include the software package name (e.g. wordpress), a number string meaningful to softaculous, the datetime the backup was made, and a tar.gz extension.

Step 1.2: Create and/or locate a box folder to copy your backups to

To make your backups easier to manage, log in to UofI Box and create a folder to store your backups. For the purposes of these instructions we will call this folder cPanelBackups.

Step 2: Configure rclone with the correct credentials

Rclone is pre-installed and available to your user account in cPanel. To complete configuration, you must also install rclone on your local machine. Your local rclone installation is only needed for the configuration step.

Under the Advanced section of your cPanel dashboard, click Terminal to launch a command line.

Typing which rclone and pressing Enter will return /bin/rclone if rclone is already installed. (It should be.)

View a screen cast of the configuration process in a new window

Once you've established rclone is available: 

  1. type rclone config
  2. enter "n" to create a new remote location
  3. enter a name for your remote (e.g. <boxaccountname>_uofibox)
  4. enter "box" for a UofI Box Account storage type (after a very long list of options is displayed)
  5. Just hit "enter" to skip the next four options (client_id, client_secret, box_config_file, access_token)
  6. enter "1" for the box_sub_type to authenticate as a user account or "2" to authenticate as a service account (usually 1)
  7. enter "n" to skip the advanced config
  8. enter "n" to Use web browser to automatically authenticate rclone with remote?

On your local machine, open a command line (e.g. windows: cmd, mac: terminal) type "rclone authorize box." Your web browser will open and lead you through a login process. When it completes, you will return to the command line and copy the token that is displayed.

Paste the token into your terminal window on cPanel. Review the settings and then confirm.

Step 3: Write a bash script that uses the rclone command

3.1: Creating a file to hold your bash script

If you are comfortable with command line editors, you can use your favorite editor in the Terminal window.

  1. Create your file in your site root, not in public_html.
  2. For purposes of these instructions, we'll be using the file name boxbackup.sh.

If you are not comfortable with command line editors:

  1. Return to your cPanel dashboard and choose File Manager.
  2. Click on the home/youraccountname folder at the very top of the folder list.
  3. In the top left corner, click the +File button. 
  4. In the pop-up that appears, enter boxbackup.sh in the "New File Name" line.
  5. If there is anything in the "New File will be created in" line, delete it.
  6. Click the Create New File button.
  7. Scroll through the file list until you find your new boxbackup.sh file.
  8. Select it (it will turn blue).
  9. Click the Edit button in the top menu bar.
  10. In the pop-up that appears, click Edit again.

At this point, you should be working in a file that will hold your shell script.

3.2: The basic script contents - copy existing backup to Box

#!/bin/bash

rclone copyto <path/to/filename> <boxaccountname>_uofibox:/cPanelBackups/<remote_filename>

3.3: Saving your edits

If editing through the File Manager, click the "Save Changes" button then click "Close"

If editing through terminal, use the save command of your text editing program (e.g. ":q" for vim, "ctrl-O ctrl-X for nano)

3.4: Running your script

Before setting your script to run automatically, verify that it correctly copies your backup to the expected location in Box.

  1. Make your script executable by typing "chmod u+x boxbackup.sh" in your terminal window
  2. From your terminal window, type "./boxbackup.sh" to run your script
  3. If the script reports an error, read the error text carefully and make changes to your script to resolve the error
  4. If no error is reported, log in to UofI Box and check the remote location (cPanelBackups). The file you referenced in your script should be present

3.5: Script variations

The script above will copy a single existing backup file to box. To fully automate your remote backup solution, you can use one of the following patterns to create a script that will create a backup of your database and document root and optionally any other important files in your account, copy it to box and manage backup rotation.

You will notice that we are writing our backups to the softaculous_backups location. This location is not included in your account quota, and so is a good place to store your backups. Even so, you should not store more than a few backups at a time on cPanel (that's one reason we're copying them to Box afterall!). While you are testing, you might create extra backups that you don't need. Please be sure to delete these manually once your testing is complete.

Scenario A: Softaculous creates your backups and boxbackup.sh copies them to a remote location

Pros and Cons of Scenario A
Pros Cons
Can manage backup frequency and number of files kept locally through Softaculous GUI If softaculous fails to create your backup, you may be unexpectedly left without a restore point
Current backups are available to Softaculous GUI for restoration Only allows for backups of content in the softaculous_backups folder*
Older backups can be copied back to their original location for easy restoration with Softaculous
*You can manually copy additional files into the softaculous_backups folder to copy them to you cPanelBackups box folder as well

#!/bin/bash

rclone copy /home/<accountname>/softaculous_backups <boxaccountname>_uofibox:cPanelBackups

Scenario B: boxbackup.sh creates the backup on the fly and moves the files to Box

Pros and Cons of Scenario B
Pros Cons
complete control of backup schedule within your crontab All backups will only be available on the remote location and must be copied back to cPanel if restoration is required
higher success rate for backups Backups not suitable for Softaculous restoration (require manual restoration)
ability to backup any content within your account

#!/bin/bash

currenttime=`date '+%Y%m%d_%H_%M_%S'`;

mysqldump -u <db_username> -p"<db_password>" <db_name> > /home/<accountname>/public_html/$currenttime.dbbackup.sql

cd /home/<accountname>

tar -zcf /home/<accountname>/softaculous_backups/$currenttime.backup.tgz public_html

rm /home/<accountname>/public_html/$currenttime.dbbackup.sql

rclone moveto /home/<accountname>/softaculous_backups/$currenttime.backup.tgz <boxaccountname>_uofibox:/cPanelBackups/$currenttime.backup.tgz

 

Scenario C: boxbackup.sh creates the backup on the fly, copies it to Box, and deletes files older than 14 days in cPanel

Pros and Cons of Scenario C
Pros Cons
complete control of backup schedule within your crontab slightly more complicated script
Current backups are available in your cPanel directory for smoother restore process
higher success rate for backups
ability to backup any content within your account

#!/bin/bash

currenttime=`date '+%Y%m%d_%H_%M_%S'`;

mysqldump -u <db_username> -p"<db_password>" <db_name> > /home/<accountname>/public_html/$currenttime.dbbackup.sql

cd /home/<accountname>

tar -zcf /home/<accountname>/softaculous_backups/$currenttime.backup.tgz public_html

rm /home/<accountname>/public_html/$currenttime.dbbackup.sql

rclone copyto /home/<accountname>/softaculous_backups/$currenttime.backup.tgz <boxaccountname>_uofibox:/cPanelBackups/$currenttime.backup.tgz

find /home/<accountname>/softaculous_backups/ -type f -mtime +14 -delete

Additional Scenarios:

Review the documentation for rclone, mysqldump, pg_dump, tar, and other commands to adjust your script as needed. In your terminal window, you can also type "man <commandname>" for help with linux commands.

Some additional ideas are:

  • Automatically delete files from your box folder (ideally you will keep more files in box than on cPanel, but you will still want to clean up at some point!)
  • create db backups of additional databases
  • Selectively back up database tables, back up content only (not structure), backup structure only (no content), etc.
  • Adjust what paths are preserved/extracted from your archive
  • change the number of days your backups are kept on cPanel
  • change the naming scheme for your backups
  • add, remove, or change files and directories that are copied to box

Step 4: Edit crontab to run the script on a schedule

Your crontab is a special text file you can use to schedule events to occur on a periodic basis. cPanel provides a simple graphical interface for setting up Cron Jobs for your account. You can find it in the "Advanced" section of the cPanel Dashboard, or by searching for "cron" in the "Search Tools" search box.

Cron Jobs feature on cPanel Dashboard

You can select your backup timing from a list of common presets, or set the timing manually using the guided text fields.

Cron Job GUI Interface

Receiving email output from your cron job can help you monitor the success or failure of your backups.

If you would like to receive email output every time the command is run, in the "Command" text box, enter

/home/<accountname>/boxbackup.sh

If you would not like to receive email output when the command is run, enter

/home/<accountname>/boxbackup.sh > /dev/null 2>&1

Alternatively, you can manually edit your crontab from a terminal or SSH window by typing "crontab -e". This will open your configured plain text editor for editing. Adding the following line to your crontab will execute your boxbackup.sh shell script at 3:17am every Saturday, and will not send you email.

17 3 * * 6 /home/<accountname>/boxbackup.sh > /dev/null 2>&1

You can use Crontab.guru to help you configure the timing of your cron command. 



Keywords:
cpanel, backup, box, uofibox, rclone, cron 
Doc ID:
130310
Owned by:
Web H. in University of Illinois Technology Services
Created:
2023-08-14
Updated:
2023-08-16
Sites:
University of Illinois Technology Services