How to set up a cron for database backup

If you are looking for the script which will take your database backup automatically on linux server then this tutorial will help you.

Step 1:
You need to save the below code as a "dbBachup.sh" .

 

#!/bin/sh

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

backupfolder="/var/www/dbBackups"

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=pksqueak –password='pk@admin$9' –default-character-set=utf8 pk_webblogs | gzip > "$fullpathbackupfile"

echo "mysqldump finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"

chown prafulkmr "$fullpathbackupfile"

chown prafulkmr "$logfile"

echo "file permission changed" >> "$logfile"

find "$backupfolder" -name db_backup_* -mtime +8 -exec rm {} ;

echo "old files deleted" >> "$logfile"

echo "operation finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"

echo "*****************" >> "$logfile"

exit 0

 

Step 2:
Now you need to set a cron by running the below command 

sudo crontab -u ubuntu -e

Step 3:
Now set file path, execution time and log file path in crontab and save and exit from the crontab. You need to restart the Apache to take effect the current changes.

Now you have done and your database backup will automatically taking every day as per your cron.

Step 2:
Now you need to set a cron by running the below command 

sudo crontab -u ubuntu -e

dbBackup

 

Posted in Linux | Tagged , , , , , | Comments Off

Comments are closed.