Difference between revisions of "Create root filesystem snapshots with LVM"

From ArchWiki
Jump to: navigation, search
(Usage)
Line 10: Line 10:
  
 
== Setup ==
 
== Setup ==
 +
 +
Check if [[Lvm#Snapshots|LVM snapshots]] are correctly setup.
  
 
During system start a clean snapshot of the root volume is
 
During system start a clean snapshot of the root volume is
 
created using a new service. Create
 
created using a new service. Create
<tt>/etc/systemd/system/mk-lvm-snapshots.service</tt> containing:
+
{{ic|/etc/systemd/system/mk-lvm-snapshots.service}} containing:
<pre>
+
{{bc|[Unit]
[Unit]
+
Descriptionm&#61;ake LVM snapshots
Description=make LVM snapshots
+
Requires&#61;local-fs-pre.target
Requires=local-fs-pre.target
+
DefaultDependencies&#61;no
DefaultDependencies=no
+
Conflicts&#61;shutdown.target
Conflicts=shutdown.target
+
After&#61;local-fs-pre.target
After=local-fs-pre.target
+
Before&#61;local-fs.target
Before=local-fs.target
+
  
 
[Install]
 
[Install]
WantedBy=make-snapshots.target
+
WantedBy&#61;make-snapshots.target
  
 
[Service]
 
[Service]
Type=oneshot
+
Type&#61;oneshot
ExecStart=/usr/sbin/lvcreate -L10G -n snap-root -s lvmvolume/root
+
ExecStart&#61;/usr/sbin/lvcreate -L10G -n snap-root -s lvmvolume/root}}
</pre>
+
  
Adapt the <tt>lvcreate</tt> command to match your root volume group and volume name.
+
Adapt the {{ic|lvcreate}} command to match your root volume group and volume name.
  
Create a new systemd target <tt>/etc/systemd/system/make-snapshots.target</tt> with this service:
+
Create a new systemd target {{ic|/etc/systemd/system/make-snapshots.target}} with this service:
<pre>
+
{{bc|[Unit]
[Unit]
+
Description&#61;Make Snapshots
Description=Make Snapshots
+
Requires&#61;multi-user.target}}
Requires=multi-user.target
+
Adapt the base target, if {{ic|multi-user.target}} is not your default target.
</pre>
+
Adapt the base target, if <tt>multi-user.target</tt> is not your default target.
+
  
Enable the new service with <tt>systemctl enable mk-lvm-snapshots.service</tt>.
+
Enable the new service with {{ic|systemctl enable mk-lvm-snapshots.service}}.
  
 
If the system is started with the new target, LVM snapshot(s) are created just after mounting the local filesystems.
 
If the system is started with the new target, LVM snapshot(s) are created just after mounting the local filesystems.
To get a [[grub]] menu entry starting this target create <tt>/boot/grub/custom.cfg</tt> based on the
+
To get a [[grub]] menu entry starting this target create {{ic|/boot/grub/custom.cfg}} based on the
<tt>grub.cfg</tt> entry for your normal startup. The kernel command line is extended to start the new <tt>make-snapshots.target</tt>:
+
{{ic|grub.cfg}} entry for your normal startup. The kernel command line is extended to start the new
<pre>
+
{{ic|make-snapshots.target}}:
### make snapshots ###
+
{{bc|### make snapshots ###
 
menuentry 'Arch GNU/Linux, make snapshots' --class arch --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-core repo kernel-true-...' {
 
menuentry 'Arch GNU/Linux, make snapshots' --class arch --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-core repo kernel-true-...' {
 
...
 
...
 
         echo    'Loading Linux core repo kernel ...'
 
         echo    'Loading Linux core repo kernel ...'
         linux  /boot/vmlinuz-linux root=/dev/mapper/lvmvolume-root ro systemd.unit=make-snapshots.target
+
         linux  /boot/vmlinuz-linux root&#61;/dev/mapper/lvmvolume-root ro systemd.unit&#61;make-snapshots.target
 
         echo    'Loading initial ramdisk ...'
 
         echo    'Loading initial ramdisk ...'
 
         initrd  /boot/initramfs-linux.img
 
         initrd  /boot/initramfs-linux.img
}
+
} }}
</pre>
+
  
Remember to adjust <tt>custom.cfg</tt> if <tt>grub.cfg</tt> changes.
+
Remember to adjust {{ic|custom.cfg}} if {{ic|grub.cfg}} changes.
  
After restarting the system with this grub entry <tt>lvs</tt> should show up the newly created snapshot.
+
After restarting the system with this grub entry {{ic|lvs}} should show up the newly created snapshot.
 
To get the messages of the new service use
 
To get the messages of the new service use
<tt>journalctl -u mk-lvm-snapshots.service</tt>.
+
{{ic|journalctl -u mk-lvm-snapshots.service}}.
  
 
== Usage ==
 
== Usage ==

Revision as of 08:56, 27 April 2013

This article will show you how to setup LVM snapshot creation during system start. Such snapshots can be used for full system backups with minimal downtime.


Prerequisites

A system with LVM root and systemd.


Setup

Check if LVM snapshots are correctly setup.

During system start a clean snapshot of the root volume is created using a new service. Create /etc/systemd/system/mk-lvm-snapshots.service containing:

[Unit]
Descriptionm=ake LVM snapshots
Requires=local-fs-pre.target
DefaultDependencies=no
Conflicts=shutdown.target
After=local-fs-pre.target
Before=local-fs.target

[Install]
WantedBy=make-snapshots.target

[Service]
Type=oneshot
ExecStart=/usr/sbin/lvcreate -L10G -n snap-root -s lvmvolume/root

Adapt the lvcreate command to match your root volume group and volume name.

Create a new systemd target /etc/systemd/system/make-snapshots.target with this service:

[Unit]
Description=Make Snapshots
Requires=multi-user.target

Adapt the base target, if multi-user.target is not your default target.

Enable the new service with systemctl enable mk-lvm-snapshots.service.

If the system is started with the new target, LVM snapshot(s) are created just after mounting the local filesystems. To get a grub menu entry starting this target create /boot/grub/custom.cfg based on the grub.cfg entry for your normal startup. The kernel command line is extended to start the new make-snapshots.target:

### make snapshots ###
menuentry 'Arch GNU/Linux, make snapshots' --class arch --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-core repo kernel-true-...' {
...
        echo    'Loading Linux core repo kernel ...'
        linux   /boot/vmlinuz-linux root=/dev/mapper/lvmvolume-root ro systemd.unit=make-snapshots.target
        echo    'Loading initial ramdisk ...'
        initrd  /boot/initramfs-linux.img
} 

Remember to adjust custom.cfg if grub.cfg changes.

After restarting the system with this grub entry lvs should show up the newly created snapshot. To get the messages of the new service use journalctl -u mk-lvm-snapshots.service.

Usage

To use this functionality for a full system backup, restart your system with the snapshot creation target. Mount the snapshot volume (and further volumes, if required), preferably using the read only (-o) option. Then backup your system, for example with tar as described in Full_System_Backup_with_tar.

During backup you can continue your system normally, since all changes to your regular volumes are invisible in the snapshots. Do not forget to delete the snapshot volume after the backup -- changes to your regular volume will use up space in the snapshot due to the copy-on-write operations. If the snapshot space becomes fully used LVM will deny further writes to your regular volumes, which should be avoided.