Last active
October 2, 2024 19:15
-
-
Save ggrandes/72c2c1f41d28664cff2d73a5ad5c5d05 to your computer and use it in GitHub Desktop.
Simple Backup (LVM + rsync)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 kvm | |
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/" | |
LOCK_FILE="/var/lock/backup.lock" | |
# | |
do_backup () { | |
[ "$UID" = "0" ] || { | |
echo "ERROR: You must be root" | |
exit 1; | |
} | |
[ ! -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 --preallocate $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')" | |
} | |
( | |
flock -n 9 || { echo ERROR: Unable to lock, another backup is running?; exit 1; } | |
do_backup | |
) 9>$LOCK_FILE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# /etc/fstab | |
# UUID=XX /mnt/backups ext4 noatime,noauto 0 0 | |
# | |
[ "$UID" = "0" ] || { | |
echo "You need root" | |
exit 1 | |
} | |
cd /mnt/backups | |
[ -x ./scripts/backup.sh ] || { | |
echo "Incorrect path" | |
exit 1 | |
} | |
touch backup.log | |
case "$1" in | |
forked) | |
./scripts/backup.sh vg1 root "--delete" | |
./scripts/backup.sh vg1 kvm "--delete" | |
;; | |
start) | |
nohup $0 forked 1>backup.log 2>&1 < /dev/null & | |
;; | |
tail) | |
tail -f backup.log | |
;; | |
*) | |
echo "$0 <start|tail>" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment