DirectAdmin hook script is a very powerful feature that is used to extend DirectAdmin functionalities. Creating a custom hook script in DirectAdmin is really easy. First, let us assume that you want to customize the user backup tool to check system load and then put your website into maintenance mode before executing a real backup. In order to achieve this, you can create the custom hook script as follows:
Step 1: Identify which hook script is needed.
You can see the complete hook script list here: List of DirectAdmin custom hook scripts. In our use case above, we would use the user_backup_pre and user_backup_post hook script.
Step 2: Create user_backup_pre hook script
mkdir /usr/local/directadmin/scripts/custom/user_backup_pre
Step 3: Create a script called aa_main.sh that will control the script order and it has the following content:
nano /usr/local/directadmin/scripts/custom/user_backup_pre/aa_main.sh
#!/bin/bash
SCRIPT_PATH="$(dirname "$(readlink -f "$0")")"
# This is the main script that will manage which script to execute in order
echo "Executing script ${SCRIPT_PATH}/load_check.sh ..."
chmod +x "${SCRIPT_PATH}/load_check.sh"
sh "${SCRIPT_PATH}/load_check.sh"
echo ""
echo "Executing script ${SCRIPT_PATH}/maintenance_mode.sh ..."
chmod +x "${SCRIPT_PATH}/maintenance_mode.sh"
sh "${SCRIPT_PATH}/maintenance_mode.sh"
# You must put this exit 0, so that Directadmin will not re-execute the above scripts
exit 0
Now make sure the script aa_main.sh is executable by running the following command:
chmod +x aa_main.sh
The reason why the script is named with the prefix aa_ is that all the DirectAdmin hooks scripts are actually executed in alphabetical order. Hence, you can rename it into anything you want but make sure it is in the first alphabetical order to allow it to be executed first before other scripts.
What the code in aa_main.sh does is, first it will call the script called load_check.sh to check system load and after it has done that it will then execute the last script called maintenance_mode.sh to put the user website into maintenance mode. Notice that at the end of the script, I put exit 0 to prevent DirecAdmin to execute other scripts that are not specified in aa_main.sh
Step 3: create the load_check.sh and maintenance_mode.sh. The script explanation is as follows:
/usr/local/directadmin/scripts/custom/user_backup_pre/load_check.sh : This script checks the system load. You can see an example of the load_check script here: Dealing with large users and backup timings | Directadmin Docs. You can copy paste the script from the link and put it inside load_check.sh
/usr/local/directadmin/scripts/custom/user_backup_pre/maintenance_mode.sh: This script puts the website domain into maintenance mode. I have implemented maintenance mode using dacli script to put the user domain in maintenance mode with one line command as mentioned here but the dacli needs more functionalities before I can publish it on GitHub.
At this stage, you have already created the pre-hook script for user backup. If you run user backup now, the system will check for system load for a few seconds and if the system load is high, it will abort the backup. If the system load is OK, it will then put the website into maintenance mode and do the real backup.
Unfortunately, we have missed another step here. After the script puts our website in maintenance mode, it should turn off the maintenance mode after the backup process is done, so our website will go to live mode automatically. If you ignore the next step, our website will be stuck into maintenance mode forever until you manually turn it off. So what we should do to overcome this is, we need to create another hook script for user_backup_post as follows:
Step 4: Create user_backup_post hook script
mkdir /usr/local/directadmin/scripts/custom/user_backup_pre
Step 5: Similar to the user_backup_pre hook script, create the following script called aa_main.sh for user_backup_post that will control the script order and it has the following content:
nano /usr/local/directadmin/scripts/custom/user_backup_post/aa_main.sh
#!/bin/bash
SCRIPT_PATH="$(dirname "$(readlink -f "$0")")"
# This is the main script that will manage which script to execute in order
echo "Executing script ${SCRIPT_PATH}/put_web_to_online.sh ..."
chmod +x "${SCRIPT_PATH}/put_web_to_online.sh"
sh "${SCRIPT_PATH}/put_web_to_online.sh"
# You must put this exit 0, so that Directadmin will not re-execute the above scripts
exit 0
Make sure the script aa_main.sh is executable by running the following command:
chmod +x aa_main.sh
Step 6: Write a script to disable website maintenance mode inside put_web_to_online.sh
Finally, we have completed our hook script and to summarize this implementation, when you execute the admin/user backup tool in DirectAdmin, it will first check system load. If the system load is high, it will try to check again within a few attempts (based on the DA script). If the system load is OK, it will put each website into maintenance mode before doing a real backup. If the user backup has finished its backup operation, it will turn off the maintenance mode for the domain.com. The backup will continue for another website.
The most important part of this article is to show you how to control the hook scripts execution order in DirectAdmin which is not documented elsewhere.