Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if database exist

Tags:

bash

mongodb

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)
like image 526
Jürgen Paul Avatar asked Dec 06 '25 10:12

Jürgen Paul


1 Answers

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

like image 113
daveh Avatar answered Dec 08 '25 00:12

daveh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!