Jürgen Paul
Jürgen Paul

Reputation: 15007

Check if database exist

How do I check if a database exists in bash?

I'm planning to make an automated backup script, where each website's db name is:

(siteName) mysite.com  = mysite_com (dbName)

Upvotes: 3

Views: 1066

Answers (1)

daveh
daveh

Reputation: 3696

You can check if a file exists with the -e bash command. MongoDB creates a Namespace file for each database, so search for those. Something like

if [ -e /data/db/$DBNAME.ns ]; then
  `mongodump -d $DBNAME --out /backup/directory`
fi

Assuming your data is stored in /data/db

Have a look at the mongodump command which can be used to backup data from your mongo db instance http://www.mongodb.org/display/DOCS/Backups

Upvotes: 5

Related Questions