In MongoDB we can take backups with database dumps or filesystem snapshots. In large databases it is recommended to use file system snapshots rather than dump backups. Any file system snapshot tool can be used as LVM snapshots. In our system LVM can be used.
The process is given bleow:
Step 1: Disable the balancer.
connect to the mongos session:
1 2 3 | use config sh.stopBalancer() |
Step 2: Start LVM snapshot for data files directory
I have db files in /db directory which has the filesystem : /dev/mapper/dbvg-dblv
1 2 3 4 5 6 7 8 | df -h Filesystem Size Used Avail Use% Mounted on ..... /dev/mapper/dbvg-dblv 949G 843G 107G 89% /db ..... |
So I need to create the lvm sbapshot of /dev/mapper/dbvg-dblv
1 2 3 4 5 6 7 8 9 10 11 | sudo lvcreate --size 500M --snapshot --name mdb-snap01 /dev/mapper/dbvg-dblv Using default stripesize 64.00 KiB. Logical volume "mdb-snap01" created. cd /dev/mapper/ ls dbvg-dblv dbvg-dblv-real dbvg-mdbshard01--snap01 dbvg-mdbshard01--snap01-cow dbvg-mdb--snap01 dbvg-mdb--snap01-cow dbvg-mdb--snap02 dbvg-mdb--snap02-cow |
Step 3: take backup to another directory
The snapshot file is created. But the backup process is not finished. We need to take the backup to another storage. So first we need to unmount the snap.
1 2 3 | sudo umount /dev/mapper/dbvg-mdb--snap01 umount: /dev/mapper/dbvg-mdb--snap01: not mounted |
Then we can zip the snap file and copy it to another directory. So we can use it if anything happens to the datafile directory.
1 | [ mapper]$ sudo dd if=/dev/mapper/dbvg-mdb--snap01 | gzip > /mongodbbackup/mdb-snap01.gz |
Step 4: Reenable the balancer
connect to the mongos session:
1 2 3 | use config sh.startBalancer() |