Plesk does much more than just simplifying the maintenance of individual websites, email and databases with the graphical user interface (GUI). In addition, powerful tools are available on the command line interface (CLI) to automate almost everything that can be configured in the graphical user interface. This can also be used to apply bulk changes to all or selected user accounts, websites, mailboxes, DNS settings and more.
In this article, you will learn how to make bulk changes with Plesk on the CLI and save lots of time. All you need is:
- Loop: A program loop that applies the same command to the items of a list.
- List of business objects to be configured: E.g. a list of mailboxes or domain names.
- Plesk CLI command: The command to be applied to each list item.
In “(4) Application Examples” you will learn how the parts (1) , (2) and (3) can be assembled to unleash the full power of Plesk on the command line.
Code Sample Convention
A backslash “\” in the CLI examples below indicates that the broken lines are to be written on the same line. The backslash is not part of the command.
# <1st command> | \ <2nd command>
means
# <1st command> | <2nd command>
Test it Before Using it in Production
Before you start, make sure to test your plan on a test server. Mass changes to data are a tempting alternative to tedious manual work. But they also carry the risk of getting a lot wrong very quickly. By bulk changes it is easy to accidentally unify configurations, which can only be undone by a data recovery.
Then it gets really exciting: If you have developed and tested a useful script, you can share your gem with other users on the Plesk forum at https://talk.plesk.com/resources/categories/archives.3/add. Who knows, maybe your CLI script will become famous!
1. Loop
The principle of making bulk changes is always the same: First, a list of objects to be edited like domains is created. Then, with the help of a script loop, the same command is applied to each item in the list. Loops can be formulated in various ways. Let’s get to know three:
“while … do … done” in a Multi-Line Script
This example uses a list of objects from a previously set variable “$objectlist”:
while read -r listitem ; do
<some Plesk command> $listitem
done <<< "$objectlist"
Use this in scripts that do more, e.g. that run database queries, combine data from different sources or run other commands before or after your while loop processed a list of business objects.
“while … do … done” in a One-Liner
This example gets a list of business objects from the output of a preceding command:
# <command_that_outputs_items> | \
while read listitem; do <some Plesk command> $listitem ; done
Use this solution to quickly apply configurations without need to formulate a full script.
The technique used here to use the result of one command as input for the next is called “piping”. The character “|” is the “pipe”. In Linux, almost any number of commands can be chained together in this way.
“for … do … done” in a One-Liner
This example gets a list of business objects from the psa database:
# for listitem in $(plesk db -Ne 'SELECT field FROM some_plesk_psa_table;'); \
do <some Plesk command> $listitem; done
Use this solution if a plesk command is not available to quickly output a list of business objects that your task requires. You can select, filter and combine business objects from the “psa” database freely. All your Plesk business objects are available there.
2. List of Business Objects to be Configured
A list of business objects to be processed by your loop, e.g. domain names, can simply be written manually line by line as a text file or generated from the Plesk database by a Plesk CLI command or a database query. But the list can also be an export from other data sources such as a spreadsheet application.
The Tedious Manual Way
For example, you could use a text editor to write domain names or email addresses line by line in a text file. Either with the Windows text editor, a spreadsheet whose lines you export as a text file and upload to the server, with the Linux “vi” or “nano” editors, or in any other conceivable way. Only watch that each entry stands alone in a line of the text file, e.g.
first-domain.tld
second-domain.tld
third-domain.tld
.
.
.
nth-domain.tld
A lot of effort to write this by hand. Therefore, in most cases, you will have a list created automatically, e.g. by a database query or a Plesk CLI command.
The Simple Plesk Command Way
Maybe you need a list of all websites. For this you could use “plesk bin site –list” and save the result to a file via output redirection “>”.
# plesk bin site --list > websites.txt
Or maybe you want to make bulk changes to mailbox configurations. For this you could run the command “plesk bin mail -l” (show all mail addresses) and filter the result with a “grep ‘Mail name'” to get only the mailboxes. Then you’d only want the third column (because that is the mail box name), so you add “awk ‘{print $3}'”, and finally you save the result to a file using output redirection “>”:
# plesk bin mail -l | grep "Mail name"| awk '{print $3}' > mailboxes.txt
Working With Database Queries Like a Pro
Some tasks can be more complex. You may only want a list of mailboxes that match certain criteria. To do this, you could generate the list of items from a database query and filter it by criteria. For example, a list of all mailboxes could be generated as follows instead of using the “plesk bin mail” command:
# plesk db -Ne "SELECT CONCAT_WS('@', a.mail_name, b.name) FROM psa.mail AS a \
INNER JOIN psa.domains as b ON b.id = a.dom_id WHERE a.postbox LIKE 'true';" \
> mailboxes.txt
There you could insert arbitrary conditions, e.g. only all mailboxes whose name starts with “x”:
# plesk db -Ne "SELECT CONCAT_WS('@', a.mail_name, b.name) FROM psa.mail AS a \
INNER JOIN psa.domains as b ON b.id = a.dom_id WHERE a.postbox LIKE 'true' \
AND a.mail_name LIKE 'x%';" > mailboxes_starting_with_a.txt
There are no limits to your imagination. With Plesk, this and much more is possible, because you always have full access to all data managed by Plesk. If you plan to use database queries, the “psa” database is the place to look for all your Plesk business objects and configurations.
3. Plesk CLI Command
Now that you have all the objects as a list to which you want to apply a configuration change, you still need the appropriate Plesk command. In Linux, such commands start with “plesk bin”, but some users prefer to call the same tool “/usr/local/psa/bin” directly. In Plesk for Windows, you would use “C:\Program Files\Plesk\bin”.
Plesk provides numerous command line commands with extensive parameter collections. Almost all tasks can be performed easily and quickly with them. To avoid having to describe everything twice in this article, we will focus on the Linux command line. For this purpose, you will find a reference of the many tools in https://docs.plesk.com/current/cli-linux/getting-started-with-command-line-utilities.37894/, each of which controls different objects such as websites, user accounts, mailboxes, etc.
For Windows, you can find instructions in https://docs.plesk.com/current/cli-win/command-line-utilities-overview.25730/.
Do you want to change the configuration of websites across the board? Then the “site” tool would be the right choice. You want to change user accounts across the board? Then look out for the “user” tool. I recommend just browsing through the manual’s chapters on each tool. No one can expect you to know the commands by heart, but just browsing through them will give you an overview of what options you have.
Let’s see one example: Say you want to change the log rotation configuration of domains. The following command will update (“-u”) the domain “domain_name“. Log rotation should be enabled, logs should not exceed 10 MB in size, and only a maximum of 7 archived log files should be kept. Archived logs should be stored in compressed form. The static command to apply to a single domain would be:
# plesk bin domain -u <domain_name> -log-rotate true -log-bysize 10M \
-log-max-num-files 7 -log-compress true
Which parameters are available I looked up in the above mentioned CLI reference. However, the trick that will save you a lot of time is not to enter such commands individually for each business object to be edited, but to have the command applied to a list of all objects to be configured.
4. The Thick of the Plot: Combine Loops With Plesk CLI Commands
Let’s combine the parts “(1) Loop”, “(2) List of business objects to be configured” and “(3) Plesk CLI command” to give you a powerful tool to apply changes to your selected objects.
“Change log rotation” Example
Let’s assume that you want to set log rotation for all domains so that logs can be no larger than 10 MB and archived logs are kept for no longer than 7 days. Archived log files are to be compressed.
Because Plesk has a command to display all domains, you can output the domain list and use it as input to a while loop. You need “plesk bin domain -l” to output the domain list, a loop that goes through the list entries “while read domain_name; do … ; done” and the command to apply to all domains: “plesk bin domain -u $domain_name”:
# plesk bin domain -l | while read domain_name; do plesk bin domain -u $domain_name \ -log-rotate true -log-bysize 10M -log-max-num-files 7 -log-compress true; done
“Turn on SpamAssassin filter” Example – Using a While Loop
Let’s assume you want to turn on SpamAssassin for all domains.
You need a command that creates a list of all mailboxes on the server “plesk bin mail -l | grep “Mail name” | awk ‘{print $3}'”, a loop that runs through this list “while read item; do … ; done” and the command that should be applied to all entries in the list “plesk bin spamassassin -u $item -status true”:
# plesk bin mail -l | grep "Mail name" | awk '{print $3}' | \ while read item; do plesk bin spamassassin -u $item -status true; done
“Turn on SpamAssassin filter” Example – Using a For Loop and Mailbox List From a File
As before, you want to turn on SpamAssassin for mailboxes. This time, however, you have a list of mailboxes in a file called “mailboxes.txt” and you want to write this and perhaps other actions into a script. For example, you could write this script:
#!/bin/bash
for item in `cat mailbox.txt`
do plesk bin spamassassin -u $item -status true done
Will Your Script Become Famous?
If you have developed a useful Plesk CLI script, you can share your gem with other users on the Plesk forum at https://talk.plesk.com/resources/categories/archives.3/add. With your contribution you will offer help to other users.
Conclusion
Combining Plesk CLI commands with loops gives you a powerful, time-saving tool for everyday server maintenance tasks. Now you are set to use loops and Plesk CLI commands to make bulk changes to business objects like domains, mailboxes and more quickly and easily.
A Plesk CLI command reference for Linux can be found in https://docs.plesk.com/current/cli-linux/getting-started-with-command-line-utilities.37894/, and for Windows it can be found in https://docs.plesk.com/current/cli-win/command-line-utilities-overview.25730/.
Finally, always be careful, as bulk changes to data carry the risk of getting a lot wrong very quickly. We therefore recommend to always test scripts on a test server before applying them in your production environments.
No comment yet, add your voice below!