How to create a cron for files and sql backup every day, every week or every minute on Linux


Hi to All,

Today i will explain how to handle the cron jobs on linux servers. It helps lot for Website development Guys. Here is the step by step process.

Target : We will create a cron for backup files and sql file using linux commands at every minute.

Step 1: Sql file generate every minute with certain location. We need to know database name, Database username and Database User Password.

Database Name: testdatabase
Database User Name: testdatabaseuser
Database User Password: testdatabasepassword

* * * * * mysqldump -u testdatabaseuser -ptestdatabasepassword testdatabase > /home/manimaran/public_html/$(date, +\%Y\%m\%d\%M\%S).sql

First * – minute (0-59),
Second * – hour (0-23),
Third * – day of the month (1-31),
Fourth * – month of the year (1-12),
Fifth * – day of the week (0-6 with 0=Sunday).

That’s it.

How to execute this code for Cron:

Execute this command line from Shell window : crontab -e
Then you can get the editor to edit some things. Here you can insert our cron code like this.

* * * * * mysqldump -u testdatabaseuser -ptestdatabasepassword testdatabase > /home/manimaran/public_html/$(date, +\%Y\%m\%d\%M\%S).sql

Just save it. The cron is Activated Now. If you want to see the cron is enable, please execute the below command

crontab -l

Then, you can see the list of cron jobs of activated. Here after the sql file generates certain location.

Step 2: Website files tar file generate every minute with certain location.

You have to insert some commands to crontab editor, what you have done so far. You can use like this way,

* * * * * tar -zcf /home/manimaran/public_html/backup-$(date -u +\%Y\%m\%d\%M\%S).tar /home/manimaran/public_html/

You should append this command to crontab editor(crontab -e). The tar file will generate every minute to certain location with specific date name.

That’s it. The cron job is ready for Back up files and sql file.

If any thing do you need let me know.

Thanks Guys.

One thought on “How to create a cron for files and sql backup every day, every week or every minute on Linux

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s