Backup of LVM2 logical volume

I’d like to make backup on external drive of LVM2 logical volume in dd manner.

Approach I consider is to

  • make lv on external drive with identical size
  • copy with dd

(Please let me know if you see something really wrong in such approach. I need something reliable and fast.)

I would like to automate the whole process; this is what I have:

# Sanity check that lv is not used
lvuses="$( lvdisplay -c /dev/mapper/vgA-AA | cut -d ':' -f 6 )"
if [ $lvuses -gt 0 ]; then exit 1 ; fi
#obtain lv size (in sectors)
lvsize="$( lvdisplay -c /dev/mapper/vgA-AA | cut -d ':' -f 7 )"
#create destination
lvcreate -L "${lvsize}s" vgB -n BB || exit 1
# copy
dd if=/dev/mapper/vgA-AA of=/dev/mapper/vgB-BB

Is it all right? Have I missed something?

(In my case vgA-AA is LVM snapshot and I would like to backup it to external drive and take this drive to other geographical location)

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your script looks fine, but I’d use a different approach: create a mirror, then break it. You can do this online, as far as I know. Untested:

lvconvert -m /dev/mapper/vgA-AA /dev/sdz98 /dev/sdz99
lvconvert --splitmirrors 1 --name BB /dev/mapper/vgA-AA

If you want to move the new logical volume to a different volume group, I think you have to ensure that the LV you want to send over is on its own physical volume(s), and transfer the PV(s) from one VG to the other with vgsplit.

Method 2

Don’t use dd. It is a dumb animal that will copy everything in the volume, including the free space, and result in a filesystem with the same UUID as the original, which can confuse the system. Instead, use a tool like partclone or ghost4linux or fsarchiver that can copy the system to a compressed image file and skip the free blocks.

Method 3

  • mount external disk
  • stop the application
  • check if the file system is unused ($ sudo fuser -M /path/to/filesystem/mountpoint)
  • create snapshot ($ sudo lvcreate -s ……….. )
  • start application
  • backup using rsync (check man rsync for –update and –link-dest)
  • or check http://dirvish.org or http://backuppc.sourceforge.net/ for implementations
  • umount external disk
  • remove snapshot LV
    This procedure creates a directory on the external disk per backup. It only copies changed/new files from the source disk ànd it saves space by hardlinking duplicate files between backups.

Method 4

Reliable and fast. I would recommend LVM snapshots.

It is extremely fast and you can guarantee that the backup will happen without any file changes happening during the backup. Also, should you have a database on the volume you won’t have to take it offline.

This is also a good way to test changes to your volume. Snapshot it, make your changes, they fail you merge the snapshot back. If they succeed you delete the snapshot.

Edit: Code

lvcreate -L 10G -s -n snapshot /dev/VG/LV

Then backup the snapshot volume to wherever you want however you want.

For filesystem testing you can snapshot and merge back.

lvconvert --merge /dev/VG/snapshot

This merge will auto-delete the snapshot volume.

Note: snapshots require kernel version 2.6.33 or newer and LVM tools 2.02.58 or newer

Edit. Links:

TLDP: Taking a Backup Using Snapshots

HowtoForge: Back Up and Restore Partitions Using LVM Snapshots

Cyberciti: Consistent Backup with LVM Snapshots


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x