## Backup and Restore Using LVM on a Linux System 

This section provides an overview of a simple backup process using LVM on a Linux system. While the tools, com- mands, and paths may be (slightly) different on your system the following steps provide a high level overview of the backup operation. 

Note: Only use the following procedure as a guideline for a backup system and infrastructure. Production backup systems must consider a number of application specific requirements and factors unique to specific environments. 

Create a Snapshot To create a snapshot with LVM , issue a command as root in the following format: 

lvcreate --size 100M --snapshot --name mdb-snap01 /dev/vg0/mongodb 

This command creates an LVM snapshot (with the --snapshot option) named mdb-snap01 of the mongodb volume in the vg0 volume group. 

This example creates a snapshot named mdb-snap01 located at /dev/vg0/mdb-snap01 . The location and paths to your systems volume groups and devices may vary slightly depending on your operating system’s LVM configuration. 

The snapshot has a cap of at 100 megabytes, because of the parameter --size 100M . This size does not re- flect the total amount of the data on the disk, but rather the quantity of differences between the current state of /dev/vg0/mongodb and the creation of the snapshot (i.e. /dev/vg0/mdb-snap01 .) 

Warning: Ensure that you create snapshots with enough space to account for data growth, particularly for the period of time that it takes to copy data out of the system or to a temporary image. If your snapshot runs out of space, the snapshot image becomes unusable. Discard this logical volume and create another. 

The snapshot will exist when the command returns. You can restore directly from the snapshot at any time or by creating a new logical volume and restoring from this snapshot to the alternate image. 

While snapshots are great for creating high quality backups very quickly, they are not ideal as a format for storing backup data. Snapshots typically depend and reside on the same storage infrastructure as the original disk images. Therefore, it’s crucial that you archive these snapshots and store them elsewhere. 

Archive a Snapshot After creating a snapshot, mount the snapshot and copy the data to separate storage. Your system might try to compress the backup images as you move the offline. Alternatively, take a block level copy of the snapshot image, such as with the following procedure: 

umount /dev/vg0/mdb-snap01 dd if =/dev/vg0/mdb-snap01 | gzip > mdb-snap01.gz 

The above command sequence does the following: 

• Ensures that the /dev/vg0/mdb-snap01 device is not mounted. Never take a block level copy of a filesys- tem or filesystem snapshot that is mounted. 

• Performs a block level copy of the entire snapshot image using the dd command and compresses the result in a gzipped file in the current working directory. 

Warning: This command will create a large gz file in your current working directory. Make sure that you run this command in a file system that has enough free space. 

