Setup a Cronjob to backup database

Discussion in 'MySQL' started by Abhik, Apr 14, 2009.

  1. #1
    Hi
    I want to setup a cronjob to backup my mysql database autometically everyday to a folder in my server.
    What are the commands should I use? And, I want to gzip the output file for compressing.

    How can I do that?
     
    Abhik, Apr 14, 2009 IP
  2. twhiting9275

    twhiting9275 Active Member

    Messages:
    305
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    80
    #2
    Here's what I use when I'm after backing things up. Setup a cron job to run every day, and run something like this:

    
    mysqldump --add-drop-table database -u=dbuser -pyourpassword >> ~/yourdb.sql
    
    Code (markup):
    I wouldn't advise compressing it, as compression takes a good bit of server resources.

    Good luck!
     
    twhiting9275, Apr 14, 2009 IP
  3. Abhik

    Abhik ..:: The ONE ::..

    Messages:
    11,337
    Likes Received:
    606
    Best Answers:
    0
    Trophy Points:
    410
    Digital Goods:
    2
    #3
    This is what I found somewhere else. Please let me if it will work for me.
    mysqldump -uDBUSER -pDBPASSWORD -hlocalhost DBNAME > /home/usr/dump/backupdb.sql  gzip -9 backupdb.sql
    Code (markup):
     
    Abhik, Apr 15, 2009 IP
  4. twhiting9275

    twhiting9275 Active Member

    Messages:
    305
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    80
    #4
    That's exactly what I said, only mine has the --add-drop-table argument to it which is going to come in handy when you have to use the backup whatever reason. Without that, you'd get tons of "table already exists" errors, and a whole lot of other stuff.
     
    twhiting9275, Apr 15, 2009 IP
  5. Abhik

    Abhik ..:: The ONE ::..

    Messages:
    11,337
    Likes Received:
    606
    Best Answers:
    0
    Trophy Points:
    410
    Digital Goods:
    2
    #5
    Thanks, I'll use the command you mentioned.
    But, whats the command for compressing the database?
     
    Abhik, Apr 15, 2009 IP