How to take the database backup automatically on linux server

By ongraph
April 1, 2014 | 1163 Views

Latest Industry insights to keep you updated on the latest happenings.

In my last project I came across the requirement of taking the database backup daily at 11:30 pm on linux server.

I have to configure server in such a way such that server automatically creates the db backup and upload it at a specified location at a particular time.

So, These are the steps I followed to achieve this and thought it worth sharing.

– In your home directory , create an executable file(.sh)

sudo touch dbBackupScript.sh

This will create a zero byte new file named dbBackupScript.sh.

– Now to make it executable to all users

sudo chmod a+x dbBackupScript.sh

– Now open the vi editor to add contents into the empty file dbBackupScript.sh

vi dbBackupScript.sh

– This will open the vi editor, now press i to insert contents into the file, then add these lines.

mysqldump -u root -p password dbname > /mnt/`date +%Y_%m_%d`_dump.sql
 

– This will create the sql dump at /mnt folder with file name today's year, month & day e.g 2014_03_19_dump

– To check if the file gives right output test & execute it via

./dbBackupScript.sh

– Check /mnt folder their you will see a dump file.

Now to automate this process, so that this file will get executed at a specified time automatically .

We need to make an entry into crontab in linux.

Linux has a great program for this called crontab. It allows tasks to be automatically run in the background at regular intervals.

– Open the crontab file, goto root direcotory via

cd /

Then do

sudo crontab -e

– here u will find all the existing cron expressions.

just write your own expression, like to run file daily at 11:30 I write

30 23 * * * dbBackupScript.sh

This means

  • 30 minute

  • of 11 hour

  • of every day of the month

  • of every month

  • and every day in the week.

Now, Save the file.

– Now it will automatically run this file at the specified time & your backup will be automatically uploaded at /mnt directory at 11:30 pm

& you don't have to worry of taking manual backups on server

Monthly industry insights to keep you updated on latest happenings

Follow us on Twitter
Follow us on Facebook
Follow us on Linkedin