Skip to content

Instantly share code, notes, and snippets.

@ggrandes
Last active April 25, 2017 14:45
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 ggrandes/72c2c1f41d28664cff2d73a5ad5c5d05 to your computer and use it in GitHub Desktop.
Save ggrandes/72c2c1f41d28664cff2d73a5ad5c5d05 to your computer and use it in GitHub Desktop.
Simple Backup (LVM + rsync)
#!/bin/bash
# Original Source:
# https://gist.github.com/ggrandes/72c2c1f41d28664cff2d73a5ad5c5d05
#
# Sample Execution:
# nohup ./backup.sh vg1 root "--delete" 1>backup.log 2>&1 & tail -f backup.log
# Sample Params:
# vg1 root "--delete"
# vg1 vmware
LVM_VG="$1"
LVM_LV_SRC="$2"
OPTS="$3"
#
LVM_LV_SNAP="${LVM_LV_SRC}.SNAP"
SRC_PART="/dev/$LVM_VG/$LVM_LV_SRC"
SNAP_PART="/dev/$LVM_VG/$LVM_LV_SNAP"
SNAP_DIR="/tmp/backups/src/$LVM_LV_SRC/"
BACKUP_DIR="/mnt/backups/$(hostname)/$LVM_LV_SRC/"
#
[ ! -b "$SRC_PART" ] && {
echo "ERROR: $SRC_PART not exist"
exit 1;
}
[ -b "$SNAP_PART" ] && {
echo "### Destroy $SNAP_PART"
umount $SNAP_PART
lvremove -f $SNAP_PART < /dev/null
}
[ ! -d "$SNAP_DIR" ] && {
echo "### Create $SNAP_DIR"
mkdir -pm700 $SNAP_DIR
}
[ ! -d "$BACKUP_DIR" ] && {
echo "### Create $BACKUP_DIR"
mkdir -pm700 $BACKUP_DIR
}
echo "### BEGIN: $(date +'%Y-%m-%d %H:%M:%S')"
echo "### Syncing"
sync
echo "### Create $SNAP_PART"
lvcreate --snapshot "$SRC_PART" --name $LVM_LV_SNAP --permission r --extents "50%FREE" < /dev/null
echo "### Mount $SNAP_PART"
mount $SNAP_PART $SNAP_DIR -o ro
echo "### Listing $SNAP_DIR $BACKUP_DIR"
ls -al $SNAP_DIR $BACKUP_DIR
echo "### Backup $SNAP_PART to $BACKUP_DIR"
rsync -HAXvax --numeric-ids --inplace $OPTS "$SNAP_DIR" "$BACKUP_DIR"
echo "### Unmount $SNAP_PART"
umount $SNAP_PART
echo "### Destroy $SNAP_PART"
lvremove -f $SNAP_PART < /dev/null
echo "### Syncing"
sync
echo "### END: $(date +'%Y-%m-%d %H:%M:%S')"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment