Nour Sofanati
Developer, SimpleBackups
February 15, 2024
With SimpleBackups, you can easily automate your backups and customize them to fit your needs. One of the ways you can do this is by using Pre/Post scripts. These scripts allow you to run custom commands before and after your backups, giving you the flexibility to customize your backups to fit your specific needs.
In this article, we’ll show you how to use Pre/Post scripts to supercharge your backups and automate your backup process with SimpleBackups.
bash
scripting: You’ll need to know how to write shell scripts to use Pre/Post scripts effectively.To configure Pre/Post scripts in SimpleBackups, follow these steps:
In this example, we're going to run a few status checks, and send a slack notification before the backup starts. Here's a simple Pre-Backup script that does just that:
#!/bin/bash
# Customizable Parameters
MYSQL_HOST="your_mysql_host"
MYSQL_USER="your_mysql_user"
MYSQL_PASSWORD="your_mysql_password"
MYSQL_DATABASE="your_database_name"
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
# Check CPU Utilization (get current percentage)
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
# Check RAM Utilization
total_ram=$(free -m | awk '/^Mem:/{print $2}')
used_ram=$(free -m | awk '/^Mem:/{print $3}')
ram_usage=$(echo "scale=2; $used_ram/$total_ram*100" | bc -l)
# Check Disk Usage (replace '/path/to/check' with your target directory)
disk_usage=$(df -h /path/to/check | awk '/\// {print $(NF-1)}')
# Check Nginx Status
nginx_status=$(systemctl status nginx | grep -o "active (running)")
if [[ "$nginx_status" != "active (running)" ]]; then
echo "Nginx is not running!"
# If you want to attempt restarting Nginx, add logic here
# Example: sudo systemctl restart nginx
fi
# Check Node.js Status (assuming PM2 process manager)
node_status=$(pm2 list | grep -q "your_app_name")
if [[ $? -ne 0 ]]; then
echo "Node.js app is not running!"
# If you want to attempt restarting Node, add logic here
# Example: pm2 restart your_app_name
fi
# Check MySQL Connectivity
mysql -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "USE $MYSQL_DATABASE" >/dev/null 2>&1
mysql_status=$? # Capture the exit code
if [[ $? -ne 0 ]]; then
echo "Cannot connect to MySQL database!"
else
echo "MySQL connection successful."
fi
# Send report to Slack (requires 'curl')
function send_slack_message() {
local message="$1"
curl -X POST -H 'Content-type: application/json' --data '{"text": "'"${message}"'"}' $SLACK_WEBHOOK_URL
}
# Build report based on statuses
report="Pre-backup Status:\n"
report+="* Nginx: ${nginx_status:-unknown}\n"
report+="* Node.js: ${node_status:-unknown}\n"
report+="* MySQL: ${mysql_status:-unknown}\n"
report+="* CPU Usage: ${cpu_usage}\n"
report+="* RAM Usage: ${ram_usage}%\n"
report+="* Disk Usage: ${disk_usage}\n"
# Send report to Slack
send_slack_message "$report"
This script checks the CPU, RAM, and disk usage, as well as the status of Nginx, Node.js, and MySQL. It then sends a report to a Slack channel using a webhook URL.
Pre/Post scripts are a powerful feature that allows you to customize your backups and automate your backup process. By using these scripts, you can ensure that your backups are tailored to your specific needs and that your environment is properly prepared for a backup. This can help you save time and resources, and ensure that your backups are reliable and consistent.
Free 7-day trial. No credit card required.
Have a question? Need help getting started?
Get in touch via chat or at [email protected]