-
Notifications
You must be signed in to change notification settings - Fork 0
Database backup and restore samsa stack
We want to be able to create backup files for the database on the server and be able to restore the database from these files. This is necessary since a job running on the server kills the docker containers and we do not want to always reinitialize the database.
Whenever a backup is made, we want the file to be named according to the current date and time. Each file will have the next name structure: "dump{month}{day}{year}-{hour}_{minute}.tar.gz". These files are stored in the database container in the data/backup folder.
In order to create such a dump file, the mongodump command is used. The batch file creating these files constructs the name of the dump file then runs the mongodump command inside the database container with the right flags.
Description of the mongodump command from official documentation:
mongodump is a utility for creating a binary export of the contents of a database. mongodump can export data from either mongod or mongos instances; i.e. can export data from standalone, replica set, and sharded cluster deployments.
Usage:
mongodump <options> <connection-string>
To connect to a local MongoDB instance running on port 27017 and use the default settings to export the content, we run mongodump without any command-line options. We do not need to specify a host, since the database is running on port 27017.
We want to be able to restore the database from the last backup made or by any dump file specified by us. If a file name is specified, the restore will happen according to the name given, if not, it will restore the database from the newest backup file.
In order to restore from a dump file created with mongodump the mongorestore command is used. The batch file doing this restore, simply chooses the file name of the most recent backup or uses a file name which is given to it, then runs the mongorestore command inside the database container.
Description of the mongorestore command from official documentation:
mongorestore recreates indexes recorded by mongodump. In general, use corresponding versions of mongodump and mongorestore. That is, to restore data files created with a specific version of mongodump, use the corresponding version of mongorestore.
Usage: mongorestore <options> <connection-string> <directory or file to restore>
tes