HDD disk
source / license: PublicDomainPictures.net

Simple script to create backups from LVM snapshots

24/10/2018
mDfRg

Below I present a smiple script that automates the process of making a backup using the power of LVM. Provided that you have your root on LVM partition you can then make a live backup on your working system with zero down-time.

What does the script do:

  1. Creates a snapshot of your root partition
  2. Mounts the snapshot
  3. Archives the snapshot to the backup folder (and omits unnecessary files like sockets)
  4. Does the clean-up (assures the backups permissions, unmounts and deletes the snapshot etc)

Here goes the script:

#!/bin/bash

today=$(date +%Y%m%d)
machine=`cat /etc/hostname`
name=$today\_$machine\_roo
tmntpt=/mnt/lvm/root_snap
backuppt=/home/backup
archivebp=$backuppt/$name.tar.gz
userbp=yourusername

sudo lvcreate -s -L 1g -n $name $machine/root
sudo mkdir -p $mntpt
sudo mkdir -p $backuppt
sudo mount /dev/$machine/$name $mntpt
sudo find / -mount -type s > /tmp/sockets.lst
sudo tar czpvf $archivebp --warning='no-file-ignored' --one-file-system --exclude-from=/tmp/sockets.lst  -C $mntpt .
sudo chown $userbp:$userbp $archivebp
sudo umount /dev/$machine/$name
sudo lvremove -y /dev/$machine/$name

exit 1

Consider running it as a cronjob.