How to take a smart MySQL backup using CRON

VPS and dedicated server security tips. Preventing from attacks, ddos and more.

How to take a smart MySQL backup using CRON

Postby Mark » Tue Dec 22, 2009 9:25 pm

Create and assign a DB user to the database you want to backup with all privileges from your Cpanel >> Mysql section.

following is a simple script to take backup using cron. Just create the file, chmod it to 750 and execute it using cron.

#============================

Code: Select all
#/bin/bash
/usr/bin/mysqldump –user=db_User –password=xyz –databases db_name | gzip > /home/foo/db_name-`date “+%Y%m%d%H%M”`.sql.gz


#============================

where,

db_user is your database user who has access on your database.

db_name is the database name you are scheduling backup for.

xyz is the actual password of above database user.


+%Y%m%d%H%M is the time stamp which will help to distinguish between backup date and time.

the backup destination in above example is “/home/foo/”


add following line in cron to take backup everyday at 23:00 and will store in separate file

Code: Select all
0 23 * * * /PathToScript.sh


Security Note
1) Don't use the command directly in cron. Put the command in a file and execute that file in cron.

2) I asked to chmod to 750 so that other will not be able to view your files.
If the file is readable by others anyone may view it and get your DB access very easily.
Mark
 
Posts: 124
Joined: Fri Dec 11, 2009 2:18 pm

Return to Securing your server

Who is online

Users browsing this forum: No registered users and 1 guest

cron