Skip to content

Instantly share code, notes, and snippets.

@leogao2
Last active March 13, 2022 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leogao2/573f5c4f8a3ae4f9852649c59bcc8d10 to your computer and use it in GitHub Desktop.
Save leogao2/573f5c4f8a3ae4f9852649c59bcc8d10 to your computer and use it in GitHub Desktop.
A fully self contained script for making a backup on an Ubuntu system using LVM. On a typical system, only configuration you need to do is 1. change BKP_LOC to wherever you want to save your backup and 2. make a password file at ~/borg_password (and you might need to enter this password first time running the script, but it's automated thereafter)
#!/bin/bash
# change these values before use!
# where you want the backup to go
BKP_LOC=/mnt/backup/primary
# the name of the source LV
SOURCE_LVM=vgubuntu/root
# where the repo password is stored
PASSWORD_FILE=~/borg_password
TMP_CKPT_SIZE=20G
SNAPSHOT_NAME=$(date '+%Y-%m-%d.%H-%M')
SOURCE_VG=$(echo $SOURCE_LVM | cut -d/ -f 1)
# make LVM snapshot
sudo lvcreate --size $TMP_CKPT_SIZE --snapshot --name root_bkp_snapshot /dev/$SOURCE_LVM
sudo mkdir -p /mnt/bkp_tmp
sudo mount /dev/$SOURCE_VG/root_bkp_snapshot /mnt/bkp_tmp
# backup with borg
borg -V || sudo apt install borgbackup -y
borg init --encryption=repokey $BKP_LOC
BORG_PASSPHRASE=$(cat $PASSWORD_FILE) sudo -E borg create --progress --stats $BKP_LOC::$SNAPSHOT_NAME /mnt/bkp_tmp
sudo umount /mnt/bkp_tmp
sudo lvremove $SOURCE_VG/root_bkp_snapshot -y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment