Hi,

As the title suggests, I am completely clueless when it comes to shell scripts. However, I am in a position where I need to write one so I thought would ask for some help. What I need to do is dump every database in my instance of mysql into a directory (/home/foo/bar for example). This is on an offsite location and I am doing this so that I can make the task of backing up website data a much simpler process. Also, I know that I can call mysqldump for each database that I have, but I am representing an institution with many databases and new projects are always starting so I need a more elegant and maintainable solution.

Thank you,
Nick

Hi Nick,

We'll need a little more info to help you I think. You say you know about mysqldump, so I assume you just need to know how to script a mysql dump for each database?

If that's the case, you can do something like this:

#!/bin/sh
# First let's get a list of databases, and 
# loop through the results.  This mysql
# command will list all databases in the 
# instance.  
for i in $(mysql -uUSER -pPASSWD -B -e 'show databases'); do

# Next run your mysqldump
  mysqldump --user=USER --password=PASSWD $i

done

If you want to dump all databases, just run mysqldump -A --user=USER --password=PASSWD You can also substitute the mysql command in my for loop with something that will provide a list of just the databases that you want ot back up, if you don't want them all. A text file would probably be easier. One database per line. You could then change that "for" statement to something like this: for i in $(cat database.list); do I hope this helps! Show us what you've got so far and we might have a better idea of how to help :)

-G

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.