Cron Tab for Daily Backup and DB Restoration
This article provides a step-by-step guide to automate MySQL database backups and perform database restoration on a Linux-based system.
The backup process utilizes a scheduled Cron job to ensure consistent and timely backups with minimal manual intervention.
Automation enhances data reliability, minimizes the risk of data loss, and facilitates recovery in the event of failures.
Prerequisites
A Linux-based system with root or sudo access
Sufficient storage space for backup files
Basic understanding of CronTab and shell scripting
(Optional) NFS mount if backup files must be stored on a network location
Installed MySQL client utilities (e.g.,
mysqldump)
MySQL Database Backup
Create a Backup Script
Log in to the MySQL VM.
Create a directory for storing backup scripts and files:
mkdir -p /home/ovaledge cd /home/ovaledgeCreate a script file named
mysql_backup.shand add the following content:#!/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 doesn't 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"
Assign Execution Permission
Make the script executable:
chmod +x mysql_backup.sh
Test the Backup Script
Run the script manually to validate functionality:
If successful, the MySQL dump file (
*.sql) is created in the backup directory.Log details are stored in
backup.log.
./mysql_backup.sh
Automate Backup with Cron
Open the CronTab editor:
crontab -eAdd the following entry to schedule a daily backup at 2:00 AM:
0 2 * * * /home/ovaledge/mysql_backup.sh >> /home/ovaledge/backup.log 2>&1Save and exit.
MySQL Database Restoration
Connect to MySQL
Log in to the MySQL VM and start the MySQL terminal:
mysql -u root -p
Create Database and User
Run the following SQL commands:
CREATE DATABASE ovaledgedb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER IF NOT EXISTS 'ovaledge'@'localhost' IDENTIFIED BY '<password>'; CREATE USER IF NOT EXISTS 'ovaledge'@'%' IDENTIFIED BY '<password>'; GRANT ALL ON ovaledgedb.* TO 'ovaledge'@'localhost'; GRANT ALL ON ovaledgedb.* TO 'ovaledge'@'%';At this stage, the
ovaledgedbdatabase is created, and access permissions are granted.
Restore from Backup
Inside the MySQL terminal, execute:
source /home/ovaledge/ovaledgedb-22-May-2025.sql;
Update Application Configuration
Modify the
oasis.propertiesfile with the correct database connection details.Restart the Tomcat service to apply changes:
sudo systemctl start tomcatWait 3–5 minutes, then verify the application in a web browser.
Error Handling and Recovery
If backup script fails:
Review
backup.logfor error messages.Ensure
mysqldumpis installed and accessible in the systemPATH.
If restoration fails:
Confirm the backup file exists and is readable.
Check MySQL user privileges for the restore operation.
Copyright © 2025, OvalEdge LLC, Peachtree Corners, GA, USA.
Last updated
Was this helpful?

