Bulk Change Web Hosting and Mail Settings Quickly and Easily with Plesk CLI

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.