Cron Tab for Daily Backup and DB Restoration

This document describes the procedure for automating MySQL database backups and restores using CronTab on a Linux-based system. The process ensures consistent backup execution and supports timely recovery in case of data loss. It reduces manual intervention and improves the reliability of metadata storage for the OvalEdge platform.

Prerequisites

  • Linux-based system with root or sudo access

  • Sufficient storage space for backup files

  • Basic knowledge of CronTab and shell scripting

  • NFS mount (optional) for storing backups in a network location

Steps Involved

  1. Configure MySQL Database Backup

    • Log in to the MySQL server.

    • Create a directory to store backup scripts and files.

    • Create a file named mysql_backup.sh. Sample Reference Screenshot:

    • Add the following script to the file:

      #!/bin/bash
      # Database credentials
      user="root"
      password="<mysql root user password>"
      host="<localhost or RDS endpoint>"
      db_name="ovaledgedb"
      # Backup storage path
      backup_path="<full path where backup should be stored>"
      # Date format for filename
      date=$(date +"%d-%b-%Y")
      # Log file path
      log_file="$backup_path/backup.log"
      # Create backup directory if it does not exist
      mkdir -p "$backup_path"
      # Perform mysqldump
      echo "[$(date)] Starting backup for $db_name" >> "$log_file"
      mysqldump --user="$user" --password="$password" --host="$host" "$db_name" > "$backup_path/${db_name}-${date}.sql" 2>>"$log_file"
      if [ $? -eq 0 ]; then
          echo "[$(date)] Backup successful: ${db_name}-${date}.sql" >> "$log_file"
      else
          echo "[$(date)] Backup failed for $db_name" >> "$log_file"
      fi
      # Delete backups older than 10 days
      find "$backup_path" -name "${db_name}-*.sql" -type f -mtime +10 -exec rm -f {} \; >> "$log_file" 2>&1
      echo "[$(date)] Old backups (older than 10 days) cleaned up." >> "$log_file"
      

      Sample Reference Screenshot:

    • Update the following parameters based on the environment:

      • MySQL username and password

      • Host (local or RDS endpoint)

      • Backup directory path

    • Grant execute permission to the script:

      chmod +x mysql_backup.sh

      Sample Reference Screenshot:

    • Run the script manually to verify execution:

      sh mysql_backup.sh

      Sample Reference Screenshot:

    • Confirm that the backup file is created in the specified directory.

  2. Schedule Backup Using CronTab

    • Open the crontab editor:

      crontab -e
    • Add the following entry to schedule daily execution at 2:00 AM:

      0 2 * * * /home/ovaledge/mysql_backup.sh >> /home/ovaledge/backup.log 2>&1

      Sample Reference Screenshot:

This configuration runs the backup script daily at 02:00 AM, and appends logs to the specified log file.

  1. Perform MySQL Database Restoration

    • Connect to the MySQL server:

      Sample Reference Screenshot:

    • Create the database and user, and grant required permissions:

      Sample Reference Screenshot:

    • Replace with the required password.

    • Restore the database using the backup file:

      Sample Reference Screenshot:

    • Verify that the database restoration is completed successfully.

  2. Update Configuration and Start Services

    • Update the oasis.properties file with the correct database details. Sample Reference Screenshot:

    • Start the Tomcat service:

      Sample Reference Screenshot:

    • Verify that the application is accessible after a few minutes.


Copyright © 2026, OvalEdge LLC, Peachtree Corners, GA, USA.

Last updated

Was this helpful?