Overview
Only advanced users should create custom transport scripts. Other users can try one of WHM’s other options:
- A local directory
- Amazon S3™
- Backblaze B2
- FTP
- Google Drive™
- Rsync
- S3 Compatible
- SFTP
- WebDAV
The Backup Configuration feature allows users to create a custom destination for their backups.
Create a custom transport script
The custom transport script is a script that you must provide for each custom backup destination that you set up in WHM’s Backup Configuration interface (WHM » Home » Backups » Backup Configuration). You can enter the transport script’s absolute path via the Script setting for the Custom destination type in the Additional Destinations section.
Script operation
The following rules affect how the script interacts with the system:
- The script runs once per command.
- The script cannot save state information between commands.
- The system does not reuse the connection between commands. Instead, each time that the script runs, the system creates the connection to the remote custom destination, and then drops it after the script runs.
The system passes information to the script, through the command line, in the following order:
- Command name.
- Current directory.
- Command specific parameters.
- Host.
- Username.
The system passes the password information to the script through the environment.
Script commands
The script must implement the following commands:
Command | Description | Parameters |
---|---|---|
chdir |
This command changes directories on the remote destination. It is equivalent to the cd command on the command line. |
$path — A file path. |
delete |
This command deletes an individual file on the remote destination. | $path — A file path. |
get |
This command copies a remote file to a local destination. |
|
ls |
This command prints identical output to the ls -l command. |
$path — A file path. |
mkdir |
This command creates a directory on the remote destination. | $path — A file path. |
put |
This command copies a local file to a remote destination. |
|
rmdir |
This command deletes a directory on the remote destination. We strongly recommend that you verify the path that you plan to delete. If you pass the root directory (/ ) as the path to delete, your system will experience serious problems. |
$path — A file path. |
Backups run each of these commands individually while the system transports the backup file and validates the destination.
Your script should return any output to STDOUT
to return data to the user.
If the script fails, it prints the output to STDERR
. The system logs any data that the script returns to STDERR
as part of the failure.
Templates
You can use the /usr/local/cpanel/scripts/custom_backup_destination.pl.skeleton
script in cPanel & WHM as a template to create your own custom_backup_destination.pl
script. For a sample backup transport script, see the /usr/local/cpanel/scripts/custom_backup_destination.pl.sample
script.
The system passes most variables as arguments to the command line. If your script does not pass one of the hardcoded arguments to the core functions, the system will display all valid arguments in the global %commands
hash.
Code examples
Use statements
Begin the script with the standard use
statements. Include any modules that you may need for your transport:
|
|
The %commands list
The script can only process the following commands.
|
|
The %command subroutines
Every call to the script begins with a command and a local directory. You must pass the command line arguments in the following order:
$cmd
— The command.$local_dir
— The local directory.@args
— The command’s arguments.$host
— Optional. The remote destination’s hostname or IP address.$user
— Optional. The remote destination’s account username.$password
— Optional. The remote destination’s password.
Use the arguments that are specific to each of the commands and variables.
- You should only include the optional
$host
,$user
, and$password
values if you configured them in the transport. - If you include the
$password
value, you must use theENV
hash. - You must include the
$local_dir
variable in every command subroutine that you create because the script calls each command individually.
|
|
The put functions
The put
function directs the script to upload or copy a local file to a remote destination. This function works similarly to the FTP put
command.
For more robust transports, we strongly recommend that you perform several error checks for each step to ensure that the system reports all errors back properly.
|
|
The get function
The get
function directs the script to download or retrieve a local file from a remote destination. This function works similarly to the FTP get
command.
|
|
The ls function
The ls
function pulls the listing of a remote file or directory, similar to FTP or a local ls
argument on the command line.
|
|
The mkdir function
The mkdir
function ensures that a directory exists on the remote machine and that the system uploads the backup to a real path.
Not all transports use a feature like the mkdir
function, however, you must include this function in the script.
|
|
The chdir function
The chdir
function allows you to store the working directory and keep the session information between operations.
Because this is a custom transport script, the system will not keep session information between operations by a single active process.
|
|
The rmdir function
The rmdir
function removes a directory and recursively deletes everything below the given directory. Based on which transport you use, you may need to remove all the files and directories below the given directory before the system can execute this function.
We strongly recommend that you verify the path that you plan to recursively delete. If you pass the root (/
) directory as the path to delete, your system will experience serious issues.
|
|
The delete function
The delete
function deletes a single file.
We strongly recommend that you ensure the path that you use, relative or full, is appropriate for the transport. If your transport does not provide an error status check, use the ls
function on the file to ensure the system deleted it.
|
|
Basic error check
We strongly recommend that you:
- Perform a basic error check to ensure that the script receives the proper arguments when the system calls it. At a minimum, you must pass the
usage() if ( @ARGV < 2 )
command. - Construct a built-in description of what the script is, what it does, and its purpose so that you can identify the script.
|
|
|
|
The $cmd command
Use the %command
hash to call each command’s specific code block.
$commands{$cmd}->(@args);