Automate Server Tasks with Virtualmin API Scripts
Learn how to efficiently automate recurring server tasks with Virtualmin API scripts. Save time and avoid errors with clever scripts.
Why Automate with Virtualmin API?
As a server administrator, many recurring tasks arise daily: creating new users, setting up databases, or monitoring resources. With the Virtualmin API, you can automate these processes, saving time and avoiding human errors. The API allows you to control almost all Virtualmin functions via scripts – whether with Bash, Python, or PHP.
Getting Started with the Virtualmin API
Before you start, you need to enable the API. Go to Virtualmin under Settings → API and enable access. Note the API key you need for authentication. Each API call is made via a URL like:
https://your-server:10000/virtual-server/remote.cgi?program=api&json=1&key=YOUR_KEYThe parameter json=1 ensures the response comes in JSON format – ideal for further processing in scripts.
Practical Examples for Automation
1. Create a New User
With a simple Bash script, you can automatically create new domains and users. Here's what an API call looks like:
curl -k "https://localhost:10000/virtual-server/remote.cgi?program=create-domain&json=1&key=YOUR_KEY&domain=example.com&pass=secret"The script can then create multiple domains at once in a loop – perfect for web hosting environments.
2. Automate Database Backups
Regular backups are essential. With the API, you can trigger a backup for each domain:
curl -k "https://localhost:10000/virtual-server/remote.cgi?program=backup-domain&json=1&key=YOUR_KEY&domain=example.com&dest=file&file=/backup/example.tar.gz"Combine this with a cron job to get daily backups without manual intervention.
3. Resource Monitoring
The API also provides status information. The list-domains command gives you a list of all domains and their resource usage. A script can then automatically take action when limits are exceeded, e.g., notify the user.
Tips for Effective Scripts
- Error handling: Always check the HTTP status code and JSON response for errors.
- Logging: Write success and error messages to a log file to trace what happened.
- Security: Use HTTPS and protect your API key like a password. Never expose it in public scripts.
Further Possibilities
In addition to the examples mentioned, you can also manage SSL certificates, set up email accounts, or change DNS entries with the Virtualmin API. The possibilities are almost limitless. If you are looking for a reliable server for your automations, check out our Virtual Servers – ideal for Virtualmin installations.