how to automatically backup MySQL databases
Author: admin admin Reference Number: AA-00470 Views: 3015 Created: 2025-08-30 09:06 Last Updated: 2025-08-30 09:06 0 Rating/ Voters

How to automatically Backup All Your Databases on iFastNet cPanel Hosting

This guide will help you create an automated backup of all your MySQL databases using a simple script on your iFastNet CloudLinux cPanel server.

What This Script Does

  • Automatically finds all databases in your cPanel account
  • Creates individual SQL backup files for each database
  • Compresses everything into a single ZIP file
  • Saves the backup directly in your home directory (no folders to create)
  • Names the backup file with date and time for easy identification

Before You Start

You'll need:

  • Your cPanel username
  • Your cPanel password (which is also your MySQL password on iFastNet)
  • Access to File Manager or SSH

Getting Your Login Details

If you don't remember your cPanel credentials:

Method 1: Check Your Welcome Email

Look for the welcome email from iFastNet when you first signed up. It contains your login details.

Method 2: Client Area Login

  1. Go to your iFastNet client area
  2. Log in with your account credentials
  3. Find your hosting service
  4. View the cPanel login details

Method 3: Password Reset

If you've forgotten your password:

  1. Use the "Forgot Password" option on the cPanel login page
  2. Or log into your iFastNet client area to reset it

Method 4: Contact Support

If you're still having trouble:

  1. Visit: https://support.ifastnet.com/new_ticket.php
  2. First-time users: You'll need to register for a support account before creating your first ticket
  3. Create a ticket requesting your cPanel login details

Step-by-Step Instructions

Step 1: Access File Manager

  1. Log into your cPanel account
  2. Find and click on File Manager in the Files section
  3. Make sure you're in your home directory (you should see folders like public_html, mail, etc.)

Step 2: Create the Backup Script

  1. In File Manager, click + File at the top
  2. Name the file: backup_databases.sh
  3. Click Create New File
  4. Right-click on the new file and select Edit
  5. Copy and paste the entire script below into the file:
#!/bin/bash

# iFastNet cPanel Database Backup Script
# This script backs up all databases in your cPanel account

echo "================================================"
echo "iFastNet Database Backup Script"
echo "================================================"

# You only need to change these two lines:
CPANEL_USER="your_username_here"
MYSQL_PASSWORD="your_password_here"

# The rest is automatic - do not change
BACKUP_DIR="$HOME"
DATE=$(date +"%Y%m%d_%H%M%S")
BACKUP_FILE="database_backup_$DATE.zip"
TEMP_DIR="$HOME/temp_backup_$DATE"

# Create temporary directory
mkdir -p "$TEMP_DIR"

echo "Starting backup process..."
echo "Date/Time: $(date)"
echo "Backup will be saved as: $BACKUP_FILE"
echo ""

# Test MySQL connection first
echo "Testing database connection..."
mysql -u "$CPANEL_USER" -p"$MYSQL_PASSWORD" -e "SELECT 1;" > /dev/null 2>&1

if [ $? -ne 0 ]; then
    echo "ERROR: Cannot connect to database!"
    echo "Please check your username and password in the script."
    echo ""
    echo "Your username should be your cPanel username"
    echo "Your password should be your cPanel password"
    rm -rf "$TEMP_DIR"
    exit 1
fi

echo "? Database connection successful!"
echo ""

# Get list of databases
echo "Finding your databases..."
DATABASES=$(mysql -u "$CPANEL_USER" -p"$MYSQL_PASSWORD" -e "SHOW DATABASES;" -s | grep -v "^information_schema$\|^performance_schema$\|^mysql$\|^sys$")

if [ -z "$DATABASES" ]; then
    echo "No databases found in your account."
    rm -rf "$TEMP_DIR"
    exit 1
fi

echo "Found the following databases:"
for DB in $DATABASES; do
    echo "  • $DB"
done
echo ""

# Backup each database
SUCCESS_COUNT=0
TOTAL_COUNT=0

for DB in $DATABASES; do
    TOTAL_COUNT=$((TOTAL_COUNT + 1))
    echo "Backing up database: $DB"
    
    # Create database backup
    mysqldump -u "$CPANEL_USER" -p"$MYSQL_PASSWORD" \
              --add-drop-table \
              --create-options \
              --disable-keys \
              --extended-insert \
              --single-transaction \
              "$DB" > "$TEMP_DIR/${DB}.sql" 2>/dev/null
    
    # Check if backup was successful
    if [ $? -eq 0 ] && [ -s "$TEMP_DIR/${DB}.sql" ]; then
        FILE_SIZE=$(du -h "$TEMP_DIR/${DB}.sql" | cut -f1)
        SUCCESS_COUNT=$((SUCCESS_COUNT + 1))
        echo "  ? Success! ($FILE_SIZE)"
    else
        echo "  ? Failed or empty"
        rm -f "$TEMP_DIR/${DB}.sql"
    fi
done

echo ""
echo "Backup Results: $SUCCESS_COUNT of $TOTAL_COUNT databases backed up"
echo ""

# Create ZIP file if we have successful backups
if [ $SUCCESS_COUNT -gt 0 ]; then
    echo "Creating ZIP archive..."
    cd "$TEMP_DIR"
    zip -q "$BACKUP_DIR/$BACKUP_FILE" *.sql
    
    if [ $? -eq 0 ]; then
        # Clean up temporary files
        rm -rf "$TEMP_DIR"
        
        # Show final results
        BACKUP_SIZE=$(du -h "$BACKUP_DIR/$BACKUP_FILE" | cut -f1)
        echo "? Archive created successfully!"
        echo ""
        # Clean up old backups (older than 30 days)
        echo "Cleaning up old backups (keeping last 30 days)..."
        find "$HOME" -name "database_backup_*.zip" -type f -mtime +30 -delete 2>/dev/null
        OLD_COUNT=$(find "$HOME" -name "database_backup_*.zip" -type f | wc -l)
        echo "? Cleanup completed. Total backup files remaining: $OLD_COUNT"
        echo ""
        
        echo "================================================"
        echo "BACKUP COMPLETED!"
        echo "================================================"
        echo "File location: $HOME/$BACKUP_FILE"
        echo "File size: $BACKUP_SIZE"
        echo "Databases included: $SUCCESS_COUNT"
        echo ""
        echo "You can download this file through cPanel File Manager"
        echo "or find it in your home directory."
        echo "================================================"
        
    else
        echo "? Failed to create ZIP file"
        rm -rf "$TEMP_DIR"
        exit 1
    fi
else
    echo "? No databases were successfully backed up"
    rm -rf "$TEMP_DIR"
    exit 1
fi
  1. Important: Find these two lines near the top of the script:

    CPANEL_USER="your_username_here"
    MYSQL_PASSWORD="your_password_here"
    
  2. Replace your_username_here with your actual cPanel username

  3. Replace your_password_here with your actual cPanel password

  4. Click Save Changes

Step 3: Make the Script Executable

  1. Right-click on the backup_databases.sh file
  2. Select Permissions
  3. Check the Execute boxes for Owner, Group, and World
  4. Or simply set permissions to 755
  5. Click Change Permissions

Step 4: Run the Script

Option A: Test the Script First (Optional)

  1. Look for a Terminal icon in cPanel and click it (if available)
  2. Type: ./backup_databases.sh
  3. Press Enter to test that it works

Option B: Set Up Automatic Weekly Backups (Recommended)

Instead of running the script manually, you can set it to run automatically every week using cron jobs:

  1. In cPanel, find and click Cron Jobs
  2. In the Add New Cron Job section:
    • Common Settings: Select "Once Per Week" from the dropdown
    • Command: Enter: /usr/bin/sh backup_databases.sh
  3. Click Add New Cron Job

Important Notes About Backup Frequency:

  • Weekly backups are recommended maximum - Running backups daily or multiple times per week will quickly fill up your disk space
  • Each backup can be several MB to GB depending on your database sizes
  • The script automatically deletes backups older than 30 days to prevent disk space issues
  • If you need more frequent backups, consider downloading and storing them locally instead of keeping them on the server

Example cron job settings:

  • Minute: 0
  • Hour: 2 (2 AM)
  • Day: 1 (Monday)
  • Month: * (every month)
  • Weekday: * (any day of week, but Day=1 makes it Monday)

This will run your backup every Monday at 2 AM when server usage is typically low.

What Happens When You Run the Script

The script will:

  1. Test your database connection
  2. Show you which databases it found
  3. Backup each database individually
  4. Show the progress and file sizes
  5. Create a ZIP file with all backups
  6. Save it directly in your home directory with a name like: database_backup_20240830_143022.zip

Finding Your Backup File

After the script runs successfully:

  1. Go to cPanel File Manager
  2. Look in your home directory (the main folder when you open File Manager)
  3. You'll see a file named database_backup_[DATE]_[TIME].zip
  4. Right-click the file and select Download to save it to your computer

Troubleshooting

"Cannot connect to database" Error

  • Double-check your cPanel username and password in the script
  • Make sure you're using your cPanel credentials, not your iFastNet client area credentials
  • Contact iFastNet support if the issue persists

"Permission denied" Error

  • Make sure you set the file permissions to 755 (executable)
  • The file should have execute permissions for all users

"No databases found" Message

  • This usually means you don't have any databases created yet
  • Check in cPanel > MySQL Databases to see if you have any databases

Script Won't Run

  • Verify the script filename is exactly backup_databases.sh
  • Make sure you're running it from your home directory via cPanel Terminal
  • Check that you have execute permissions on the file

Cron Job Not Running

  • Make sure the command in your cron job is exactly: /usr/bin/sh backup_databases.sh
  • Check that your script has execute permissions (755)
  • Verify your cron job timing settings are correct
  • Look in cPanel > Cron Jobs to see if there are any error messages

Getting Help

If you encounter any issues:

  1. First-time support users: Register at https://support.ifastnet.com/new_ticket.php
  2. Existing users: Log in and create a new ticket
  3. Include details about:
    • What error messages you're seeing
    • Which step you're stuck on
    • Your cPanel username (never include your password)

Security Note

Remember to:

  • Keep your backup files secure
  • Download them to your local computer for safe storage
  • Delete old backup files from the server to save space
  • Never share your cPanel password with anyone

CloudLinux Compatibility

This script is fully compatible with iFastNet's CloudLinux cPanel servers and uses standard MySQL commands that work within the CloudLinux security restrictions.

Quick Jump Menu