Skip to content

All topics  /  General

Linux Shell Script For Database Backup

General ·

For teams turning “Linux Shell Script For Database Backup” into a repeatable delivery task, the official Monitask website can document setup, troubleshooting and review time; managers should interpret those records alongside reliability and completed work, not as a stand-alone score.

Package and operating-system behaviour can change, so verify the commands and supported versions through Ubuntu before running them on a production host.

Hello Friends,

Now let's see example of how to backup database using shell script linux. i explained example of linux shell script for database backup. We will talk about how to automate mysql database backups in linux.

In this example, I just created a MySQL shell script that I use on my Linux servers to make database backups with mysqldump.

Here i will give step by step to create shell script for database backup. So let's see the bellow example:

Step 1 : Create .sh File


In this step, We will create database_backup in home directory. Now you can create backup.sh file in database_backup folder. open backup.sh file paste below code:

/home/database_backup/backup.sh

username=username
password=password
database=your_database_name
now="$(date +'%d_%m_%Y_%H_%M_%S')"
filename="db_backup_$now".gz
backupfolder="/home/database_backup/"
fullpathbackupfile="$backupfolder/$filename"
logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt
echo "mysqldump started at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
mysqldump --user=$username --password=$password $database > $fullpathbackupfile
echo "mysqldump finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
echo "remove backup 5 days old" >> "$logfile"
# Delete files older than 5 days
#find $backupfolder=/* -mtime +5 -exec rm {} \;
echo "complete removing" >> "$logfile"
exit 0

Step 2 : Executable .sh File

Now We will executable backup.sh file using below command So let's open terminal and run below command.

run .sh file
sudo chmod +x backup.sh
sudo ./backup.sh

Step 3 : Setup Crontab

In the last step, You can set backup.sh file in crontab. So let's open crontab file using below command:

crontab -e

successfully open crontab file thenafter add below line in crontab.

0 2   * /home/database_backup/backup.sh

Add line theafter save file.

It will help you....