Skip to content

Commit

Permalink
made database name configurable
Browse files Browse the repository at this point in the history
added script to copy database from production server
  • Loading branch information
mldangelo committed Mar 28, 2017
1 parent 1e8c858 commit 81eb3e5
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 2 deletions.
2 changes: 2 additions & 0 deletions sample.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
NODE_ENV=production
PORT=7999

HOSTNAME=http://localhost:7999
DB_NAME=mldangelo

GITHUB_OAUTH=ABCDEFG

Expand Down
115 changes: 115 additions & 0 deletions scripts/mongo-copy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/bin/bash

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# This scripts makes a copy of the production database for a #
# development server or your local machine. #
# run with -h for arguments or view file below #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

NOW=`date +%s`
PRODUCTION_USER="ubuntu"
PRODUCTION_HOST="mldangelo.com"

# Parse arguments
while [[ $# > 0 ]]
do
key="$1"
case $key in
-s|--server)
SERVER="$2"
shift # past argument
;;
-k|--key)
KEY="$2"
shift # past argument
;;
-n|--name)
DB_NAME="$2"
shift # past argument
;;
-h|--help)
HELP=1
;;
*) # unknown option
UNKNOWN=1
;;
esac
shift # past argument or value
done

if [[ "$UNKNOWN" -eq 1 ]]
then
echo -e "\nError: Unkown argument."
HELP=1
fi

if [[ "$HELP" -eq 1 ]]
then
echo -e "\nMongo copy allows you to quickly make a copy of a remote"
echo -e "mongo database and add it to your local mongodb instance.\n"
echo -e "Allowed arguments:"
echo -e "\t --help, -h: Display this message"
echo -e "\t --key, -k: Specify path to ssh key"
echo -e "\t --name, -n: Specify mongodb database name"
echo -e "\t --server, -s: Specify user@host or ssh identity\n"
exit 2
fi

# set server
if [ -z "$SERVER" ]
then
SERVER=$PRODUCTION_USER@$PRODUCTION_HOST
fi

# set key
if [ ! -z "$KEY" ]
then
SSHCMD="-i $KEY $SERVER"
else
SSHCMD="$SERVER"
fi

if [ -z "$DB_NAME" ]
then
DB_NAME="mldangelo"
fi

# Directory where temporary files will be temporarily stored
BACKUP_DIR=/tmp/${DB_NAME}_mongo_backup_${NOW}

# Check if monogodb is running
if pgrep "mongod" > /dev/null
then
echo "Local instance of mongodb is running."
else
echo "Local instance of mongodb is not running. Please start mongodb and run script again."
exit -1
fi

# Check if ssh works
ssh -q $SSHCMD exit
if [ "$?" -eq "0" ]
then
echo "SSH CONNECTION SUCCESSFUL"
else
echo "SSH ERROR: check manual connection before trying again."
exit -1
fi

COPY_CMD="mkdir $BACKUP_DIR; mongodump -d $DB_NAME -o $BACKUP_DIR"
DELETE_CMD="rm -rf $BACKUP_DIR"

echo "EXPORTING DB:$COPY_CMD"
ssh $SSHCMD $COPY_CMD

echo "TRANSFERING DB"
rsync -avzP -e ssh $SSHCMD:$BACKUP_DIR /tmp

echo "IMPORTING DB"
mongorestore --noIndexRestore -d $DB_NAME --drop $BACKUP_DIR/$DB_NAME

echo "REMOVING TEMPORARY FILES"
ssh $SSHCMD $DELETE_CMD
rm -rf $BACKUP_DIR
END=`date +%s`
echo "Finished copying database. Operation took $((END - NOW)) seconds."
5 changes: 3 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import auth from './auth';

const port = process.env.PORT || 7999;
const env = process.env.NODE_ENV || 'development';
const database = process.env.DB_NAME || 'mldangelo';

const app = express();

Expand All @@ -28,10 +29,10 @@ app.use(cookieParser());

const MongoDBStore = mongoStore(session);

mongoose.connect('mongodb://localhost/mldangelo');
mongoose.connect(`mongodb://localhost/${database}`);

const store = new MongoDBStore({
uri: 'mongodb://localhost/mldangelo',
uri: `mongodb://localhost/${database}`,
collection: 'sessions',
});

Expand Down

0 comments on commit 81eb3e5

Please sign in to comment.