How to backup your sql database automatically using cron jobs

Best Division

Best Division  @bestdivision

Published On - Last Updated -

Some more reasons why you should back up your database regularly:

Working as a freelancer I came across several projects where the clients' has lost all their users because their databases was destroyed by some sort of attack like:-

  1. SQL Injection (A very common one): Most PHP websites are vulnerable to this attack if proper care is not taken. Even a single form present on your website, where users are allowed to fill the form, can leave your website vulnerable for SQL Injection, if not sanitized and validated properly.

Websites coding doesn't get affected much. But all we lose is our database where various users register daily with the hope of staying connected with you. If they subscribed to your email notification whenever you post something new... then what? Your goodwill compels some users to register and subscribe again..but various will not do that again... Why are we even discussing this if we have a simple solution for that. Thanks to a cronjob. ????

So, depending on how much traffic your website gets daily you can schedule a cronjob for every minute, hour, month, etc. (We can run many cronjobs parallelly. So don't worry about it).

Method 1: Write cron job directly in command in CPanel

If you just want to run backup command then can use this method. But is considered an insecure way of writing because you are revealing the password in the command you wrote. So I prefer the second method. 

Method 2: Write cron job in a file (here PHP) and then execute that script using CURL in the command in CPanel

/usr/bin/mysqldump -u USERNAMEOFDB-p'PASSWORDOFDB' DBNAME> /home/usernameofcpanel/public_html/db_backup_`date +\%Y\%m\%d\%H\%M`.sql
Similar Blogs
0 Commentssort Sort By

@abcd 1 days ago

Aquí los que apoyamos a Los del limit desde sus inicios..