I have mongod instance running on 1.2.3.4 for example and have another backup server on 100.90.80.1.
I need to make backup mongodb data from 1.2.3.4 to 100.90.80.1 with the following bash script:
#!/bin/bash
set -e
MONGODUMP_PATH="/usr/bin/mongodump"
MONGO_HOST="1.2.4.4"
MONGO_PORT="27017"
MONGO_DATABASE="db"
MONGO_USERNAME="login"
MONGO_PASSWORD="pass"
TIMESTAMP=`date +%F-%H%M`
BACKUP_FILE_PATH="/var/backups/tokumx-backup-$TIMESTAMP"
error_exit()
{
echo "Backup TokuMX filed due Error: $1" 1>&2
rm -rf $BACKUP_FILE_PATH
rm -rf $BACKUP_FILE_PATH.tar
exit 1
}
# Create backup
$MONGODUMP_PATH --host $MONGO_HOST --port $MONGO_PORT --db $MONGO_DATABASE --use
rname $MONGO_USERNAME --password $MONGO_PASSWORD --out $BACKUP_FILE_PATH
# Make archive
tar cf $BACKUP_FILE_PATH.tar -C $BACKUP_FILE_PATH/ .
# Remove backup folder
rm -rf $BACKUP_FILE_PATH
But I can't connect to 1.2.3.4 due this option in /etc/mongodb.conf
bind_ip = 127.0.0.1
I know that I may to change the restriction above to bind_ip = 0.0.0.0 but it's not secure.
What is the best way to make secure connections to remote mongod server and backup files to another one with my bash script above?
P.S: I run the script above such as the following:
sudo sh /scripts/tokumx_backup_script
On live machine:
Login as test, and do this:
Then copy the file ~test/.ssh/id_rsa (from live server) to the file ~backupuser/.ssh/id_rsa_live.pem on the backup server.
Next step: login as backupuser on backup machine, cd to .ssh directory and create (or append to) the file "~backupuser/.ssh/config" and enter this:
After this, you should be able to connect from the backup server to the live server without entering a password:
Please test this first. Make sure that you can login without giving a password.
At this point, you have the option to copy from live to backup with the scp command:
So you could create a backup on the live server and then save it locally on the backup server. You can also send shell commands this way:
Finally, if you still isinst on running the backup on the backup server, you can create a new tunnel with this command:
and then you can use 127.0.0.1:12345 for backing up. But please be aware that the tunnel will run in the background, so you will have to find a way to stop it.