#15 - Remote Backup with mongodump
Problem: I have a mongodb that needs backing up. However the space on that server is limited. Lets call this server Production.
Solution: Do a mongodump from another server with plenty of space. We will call this server Backup
- On Backup, create a user that can be used to ssh into the Production. Once done, create a ssh keypair. Then copy the public key into Production.
$ssh-keygen -t rsa -b 2048
$ssh-copy-id .ssh/id_rsa_pub user@production -v
-
Install mongodump. I suggest referring to the documentation on the official website. On Ubuntu, just install the mongodb-org-tools which contains mongodump.
-
Create ssh tunnel with local port forwarding on Backup. This will allow you to access the database locally via port 27071
$ssh -nNT -L 27071:127.0.0.1:27071 -v
- Run mongodump [1]
$mongodump --gzip --database database_name
mongodump will connect to mongodb on Production and dump the data on Backup. All of this happens via ssh.
By default, mongodump will create a directory called dump. Do check out other options for mongodump on the official website.