Shell Script for a Periodic MySQL Backup

I’ve been playing around with the UNIX shell off late, and its helped me greatly in reducing backup times for backing up my MySQL databases from at least 10 minutes to around 10 seconds. Also, since it’s a little tricky to manage periodic backups, I made a little shell script that you can use to make periodic backups of your MySQL databases quickly and easily. So, here we go…

CODE:
  1. #! /bin/csh
  2. mkdir `date +%m_%d_%Y`
  3. cd `date +%m_%d_%Y`
  4. mysqldump -h hostname -u username -ppassword dbname> dbbackup.sql
  5. exit 0

You can use multiple lines of mysqldump to backup multiple databases quickly and easily, although some hosts wont be happy if you give them such processor intensive jobs. The above script simply creates a directory of the format mm_dd_yyyy and stores the MySQL databases of that particular date over there.

POSTED BY Abhinav on 26 May 2006
in Code-n-Stuff

 

Please leave a Comment

If you would like to make a comment, please fill out the form below.

Name (required)

Email (required)

Website

Comments

5 Comments so far

  1. Shantanu Goel said on March 16, 2008 at 5:51:32 am

    I apologize for diggin up such an old post..just stumbled onto here somehow..but that is a very risky approach as you are keeping your username/pwd in clear text. Moreover, am not sure whether those username and pwds will be transmitted in cleartext only over the net, in which case it is even more dangerous…
    (Don’t know why your blog is flagging it for spam?)

  2. Amit said on March 17, 2008 at 3:52:02 pm

    Nobody’s username or password is in clear text here Shantanu, none at all. The “username” & “password” in the above script has been placed just as placeholders to indicate it to readers that this is where their username & password should go if they want to use this script! :)

  3. Abhinav said on March 20, 2008 at 12:33:23 am

    Wow, what a coincidence, I ended up here after months trying to find an old script I had made for file backups, and found someone posting a comment just 4 days ago!

    @Shantanu - If you’re worried about security, just dont place these files in a public folder, most hosts allow for that, but even better *nix has a great file permissions system so you can set up a cron and set the script’s permission to execute by owner only.

  4. Abhinav said on March 20, 2008 at 12:36:12 am

    Oh, and thanks Amit for doing a great job of keeping this website up and running!

  5. Amit said on April 15, 2008 at 5:11:14 am

    If you’re worried about security, just dont place these files in a public folder

    If Shantanu was referring to shell scripts being put in publicly accessible directories on a website then I’d just say that shell scripts(of any kind) should never be placed in publicly accessible directories unless they are meant to be given as downloads in which case they should be passed on as plain text files & should be stored as same as well with no execution permissions! Web Security 101 I think! :)

    Oh, and thanks Amit for doing a great job of keeping this website up and running!

    You are welcome! :)

© 2005-2008 diGit Blog [Disclaimer]. Content of this Blog Licensed Under Attribution-Noncommercial-No Derivative Works 3.0 Unported
Riding Stoutly on WordPress   ||   Powered by iNetwork.IN
Decorated in GreenTech Theme