Technology blogs
Apr 29

The MySql database backup website backup is very similar to the previous backup that I showed you 2 days ago. We are going to create a php file that is going to dump our database into filename with the current date and time.

Ok so let’s get going:
1. Create the file “backup_database.php” and input the following code into it:
<?php exec(‘cd /home/your_user/backup/db/;mysqldump –add-drop-table -h localhost –user=your_user –password=your_pass database_name > `date +%y-%m-%d_%H-%M`.sql’); ?>

Sometimes the path to your home dir looks different than ‘/home/your_user/’, so execute the ‘pwd’ command like explained in my previous post

Here we use the command mysqldump in a tutorial that I wrote previously about the dumping the sql database from one host to another. Now the database will be saved directly on the server that is hosting your website

2. Create /backup/ and /backup/db/ directories via SSH or FTP if not created.

Of course you can use any names. But better do not create the /backup/ directory in the /public_html/ directory for security reasons ;)

3. Upload “backup_database.php” to the /home/your_user/backup  directtory

If you upload backup_database.php to www.yoursite.com/backup_database.php by accessing it directly in your browser you will create a backup of your database.  However I do not recommend doing this :) First because someone can overload your space quota by accessing the backup_database.php many times and second because you probably don’t want to enter the URL in your browser every time you want to perform database backup.

4. Go to CPanel -> Cron Jobs and input the path to backup_database.php:

Cron Job DataBase backup
Click on the `Add new Cron Job` button…     and you are pimped out. In my case I added the option this cron job to be executed twice a day. In case you database is updated more often/rarely you can modify this setting to suit your needs.
You can also add option to save the dump of the database in tar format to save some space in your hdd quota by changing the script a bit.
Full database backup in tar format (save more space):
backup_database.php:
&lt;?php exec(‘FILE=`date +%y-%m-%d_%H-%M`;cd /home/ps3igri/backup/db/;mysqldump –add-drop-table -h localhost –user=your_mysql_user –password=your_pass db_name > $FILE.sql;tar czpf $FILE.tar $FILE.sql;rm -f $FILE.sql’); ?>
As you see here we use the ‘FILE’ variable to store information about the filename. We create a SQL Dump, then we archive it, and after that we delete the original file to save some disk space.
Good luck with the backup and see you next time :)

12 Responses to “Full backup of website’s mysql database using cron job”

  1. Job Says:

    Searching for a job during these times is very difficult indeed. As a recruitment consultant and I contribute to a few job websites out there. In my expereince the people who end up getting the jobs are very confident in what they say. They won’t know the answer to every question but they sure know how to answer. Also if your applying for a specific company go and do some research about it first and see how you can adapt yourself to it. Using that knowledge the interview process becomes much easier. Just be confident and keep trying, eventually you will find a job.

    Just thought I’de give my advice. Brilliant article mate.

    Expert Job Blogger
    http://www.jobvacanciesinlondon.co.uk

  2. Learn Wordpress Says:

    Very well written post, do you have an rss feed I can subscribe to?

  3. Georgi Mitev Says:

    http://georgi-mitev.com/feed/ here it is :)

  4. games addicting Says:

    I check your posts more often! Really interesting articles.I enjoyed reading it. I need to read more on this topic..Thanks for sharing a nice info…

  5. Brian Haddad Says:

    okey which means i’m lost lol exactly how? do u get hold of so many mafia since i no more than own 12 ( among them me! ) and also i doubt i will about to get higher and also im level 26 plus getting greater thus how do i grow my mafia members fast at this sort of a minimal level Or is this a place to question?

  6. forex Says:

    I must say I’ve been searching for a good read regarding topic . Searching in Yahoo I found this amazing site. After reading this information I’m glad to say that I have found exactly what I was searching for. I will make sure to bookmark http://georgi-mitev.com/2010/04/29/full-backup-mysql-database-db-using-cron-job/ and come again regularly . Thanks! :-)

  7. aaa designer handbags Says:

    I check your posts more often! Really interesting articles.I enjoyed reading it. I need to read more on this topic

  8. louis vuitton speedy bag Says:

    Very well written post, do you have an rss feed I can subscribe to?
    http://www.louisvuittonbagmall.com/Louis-Vuitton-Graffiti-Neverfull-MM(green)-goods-24.html louis vuitton neverfull mm

  9. cansu Says:

    Siteniz çok güzel,oyunlar için teşekkürler.

  10. Justin Marzano Says:

    Really appreciate you sharing this blog post.Really thank you!

  11. Home Furniture stores Says:

    Insightful. Inspirational. Beautifully Designed :) . The experiences and skills gained through your website be carried throughout my professional career. If only everyone had the same level of perseverance – the world would be an enjoyable place to live in. Ciao for now :) . I hope this message is worthy of being posted.

  12. Find Jobs Says:

    Of course, what a great site and advisory posts, Can I add backlink – import your rss feed? Regards, Reader.

Leave a Reply

Spam Protection by WP-SpamFree