Categories: cPanel

How to backup all the MySQL databases daily using Shell script/cron job.

Hi,

How to take full MySQL backup daily using shell script ?

 

First we need to create the shell script on the server  and add below code in it. After creating the shell script. After this you can add this shell script to daily crons to perform daily backup automatically. You can directly put the above code in the daily cron for example :

vi /etc/cron.daily/mysqlbackup.sh

Here is the code :

======================================

#!/bin/bash

# Simple script to backup MySQL databases

# Parent backup directory
backup_parent_dir=”/home/backups/mysql”
# MySQL settings

mysql_user=”root”
mysql_password=$(cat /root/.my.cnf | grep pass | sed ‘s/^pass=//g’ | tr -d ‘\”‘)
mysql_password=$(cat /root/.my.cnf | grep pass | sed ‘s/^password=//g’ | tr -d ‘\”‘)

# Read MySQL password from stdin if empty
if [ -z “${mysql_password}” ]; then
echo -n “Enter MySQL ${mysql_user} password: ”
read -s mysql_password
echo
fi

# Check MySQL password
echo exit | mysql –user=${mysql_user} –password=${mysql_password} -B 2>/dev’/null
if [ “$?” -gt 0 ]; then
echo “MySQL ${mysql_user} password incorrect”
exit 1
else
echo “MySQL ${mysql_user} password correct.”
fi

# Create backup directory and set permissions
backup_date=`date +%Y_%m_%d_%H_%M`
backup_dir=”${backup_parent_dir}/${backup_date}”
echo “Backup directory: ${backup_dir}”
mkdir -p “${backup_dir}”
chmod 700 “${backup_dir}”

# Get MySQL databases
mysql_databases=`echo ‘show databases’ | mysql –user=${mysql_user} –password=${mysql_password} -B | sed /^Database$/d`

# Backup and compress each database
for database in $mysql_databases
do
if [ “${database}” == “information_schema” ] || [ “${database}” == “performance_schema” ]; then
additional_mysqldump_params=”–skip-lock-tables”
else
additional_mysqldump_params=””
fi
echo “Creating backup of \”${database}\” database”
mysqldump ${additional_mysqldump_params} –user=${mysql_user} –password=${mysql_password} ${database} | gzip > “${backup_dir}/${database}.gz”
chmod 600 “${backup_dir}/${database}.gz”
done

======================================

 

This will simply run your MySQL on daily basis and keeps your System updated in case of MySQL crash or requirement of Old Data.

 

In addition to this, to save the disk space you can remove old database backup by adding simple 2 lines to above code :

 

# Terminate old backup
find /home/backups/mysql/ -mtime +10 -exec rm -rf {} \;
done

This will delete the backup which is older than 10 days.


Vishwajit Kale
Vishwajit Kale blazed onto the digital marketing scene back in 2015 and is the digital marketing strategist of Hostripples, a company that aims to provide affordable web hosting solutions. Vishwajit is experienced in digital and content marketing along with SEO. He's fond of writing technology blogs, traveling and reading.

Recent Posts

This Diwali, Give Your Website the Glow It Deserves — Host Smarter,Rank Higher

Every Diwali Brings New Light, New Beginnings, and New Opportunities In today’s digital world, your website is the Diya that…

3 days ago

What Are the Best WordPress Photo Gallery Plugins for 2026?

Photography is not just about taking pictures—it’s about presenting them beautifully. For photographers and creative bloggers, WordPress photo gallery plugins…

2 weeks ago

How Can You Prepare Your Ecommerce Site/blog for Festivals?

Festivals are more than cultural celebrations—they’re golden opportunities for e-commerce stores and bloggers to attract, engage, and convert audiences. During…

3 weeks ago

Shared Hosting: Migrating Your Site Seamlessly

If you’re new to web hosting, the idea of moving your website from one provider to another might sound intimidating.…

1 month ago

Top 15 WooCommerce Plugins to Power Your eCommerce Store in 2025

WooCommerce powers over 5 million online stores, and its true potential comes alive with plugins that enhance functionality. The right…

1 month ago