Loading
Krishan Chawla

Technical Lead

Software Engineer

Automation Expert

Tech Enthusiast

  • About
  • Resume
  • Awards
  • Projects
  • Blogs
  • Contact
Krishan Chawla

Technical Lead

Software Engineer

Automation Expert

Tech Enthusiast

Download Resume

Recent Posts

  • How to Configure MySQL Master-Slave Replication for Data Redundancy and Scalability
  • Java Spring Boot Docker Deployment with Oracle Made Simple
  • Oracle XE In Docker On Windows: An Ultimate Guide
  • How to quickly Instrument JaCoCo agent with WebLogic
  • How to setup JaCoCo Code Coverage with Maven Project

Recent Comments

No comments to show.

Archives

  • March 2025
  • December 2024
  • May 2024
  • April 2024

Categories

  • Blog
  • Quick Byte
Blog Post

Automate MySQL Database Backups on Linux: A Step-by-Step Guide

30 April, 2024 Quick Byte by Krishan Chawla
Automate MySQL Database Backups on Linux: A Step-by-Step Guide

Ensuring the safety and security of your MySQL databases is crucial for any business. Regular MySQL database backups are a key component of a solid data protection strategy. In this blog, I’ll show you how to automate MySQL database backups on Linux using a simple shell script.

STEP 1: Create a Backup Shell Script

First, create a new shell script named mysql_backup_script.sh and add the following content:

#!/bin/bash

# MySQL database credentials
DB_USER="<DB_USER>"
DB_PASS="<DB_PASSWORD>"
DB_NAME="<DB_NAME>"

# Backup directory
BACKUP_DIR="<path/to/backup/directory>"

# Backup file name
BACKUP_FILE="$BACKUP_DIR/$DB_NAME-$(date +%Y%m%d%H%M%S).sql"

# Dump the MySQL database
mysqldump --user=$DB_USER --password=$DB_PASS $DB_NAME > $BACKUP_FILE

# Optional: Compress the backup file
# gzip $BACKUP_FILE

Replace <DB_USER>, <DB_PASSWORD>, <DB_NAME>, and <path/to/backup/directory> with your actual MySQL credentials, database name, and backup directory path.

Step 2: Configure executable permissions for the script

Add the executable permissions to the script file by running:

chmod +x mysql_backup.sh

Step 3: Schedule the MySQL database Backups

Schedule the backup script to run automatically on a specified time using cron. Open your crontab file by running:

crontab -e

Add the following line to schedule the backup to run daily at midnight:

0 0 * * * /path/to/mysql_backup_script.sh

Replace /path/to/mysql_backup_script.sh with the actual path to your backup script.

You can utilize the following reference, if you wish to configure different schedule for your backup.

Post Views: 4
Share:
Tags: AutomationBackup AutomationBackup ScriptsCron JobsData ProtectionDatabase BackupDatabase ManagementlinuxLinux CommandsLinux ServerLinux TutorialsMySQLMySQL Adminmysql backupMySQL on LinuxServer ManagementSystem Administration
Related Posts
MySQL Master Slave Replication
How to Configure MySQL Master-Slave Replication for Data Redundancy and Scalability

Master-slave replication is one of the most reliable techniques for maintaining multiple copies of MySQL data, ensuring data redundancy, and…

Setup Oracle XE on Dockers
Oracle XE In Docker On Windows: An Ultimate Guide

Setting up Oracle XE in your local development environment can significantly speed up application development by giving you a powerful…

Post navigation

Prev
Next
Write a comment Cancel Reply