Supercharge Your Backups with Custom Scripts

SimpleBackups developer

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.

Prerequisites

  • A SimpleBackups account: If you don’t already have one, you can sign up for a free account.
  • A backup configured in SimpleBackups: If you haven’t already set up a backup, you can look at our documentation to create your first backup. (You can also check out our YouTube channel for video tutorials.)
  • A basic understanding of bash scripting: You’ll need to know how to write shell scripts to use Pre/Post scripts effectively.

Configuring Pre/Post Scripts in SimpleBackups

To configure Pre/Post scripts in SimpleBackups, follow these steps:

  • Go to your backup's edit page.
  • Scroll down to the "Add a custom script" section.
  • Check the appropriate box to add a Pre or Post script (or both، separately).

Custom Script screenshot

Example: Running a Pre-Backup Script

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.

Conclusion

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.



Back to blog

Stop worrying about your backups.
Focus on building amazing things!

Free 7-day trial. No credit card required.

Have a question? Need help getting started?
Get in touch via chat or at [email protected]

Customer support with experts
Security & compliance
Service that you'll love using