Linux Shell Script For Database Backup

Laravel 8 , Ubuntu , Laravel 7 , Laravel , PHP , Laravel 6




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 file in database_backup folder. open file paste below code:





now="$(date +'%d_%m_%Y_%H_%M_%S')"




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 file using below command So let's open terminal and run below command.

run .sh file

sudo chmod +x

sudo ./

Step 3 : Setup Crontab

In the last step, You can set 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/

Add line theafter save file.

It will help you....

Recommended Posts