-
-
Save clexanis/8677626e9e67b05f835022b62911d3cd to your computer and use it in GitHub Desktop.
A script to take daily snapshots of a LVM volume.
This file contains hidden or 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 | |
KEEP_DAYS=15 | |
VG="vg" # LVM volume group we are snapshoting | |
LV="data-volume-name" # Name of LVM-volume to take a snapshot of | |
BACKUP_PREFIX="snap-${LV}-" # Prefix of snapshot volume name. | |
SIZE=10%ORIGIN # Amount of disk space to allocate for the snapshot | |
# Create new snapshot | |
TODAY="$(date +%F)" | |
NEW_VOLUME="$BACKUP_PREFIX$TODAY" | |
if ! lvs | grep -q -F "$NEW_VOLUME"; then | |
/sbin/lvcreate --extents $SIZE --permission r --snapshot "$VG/$LV" --name "$NEW_VOLUME" | |
else | |
echo "Backup already exists: $NEW_VOLUME" | |
fi | |
# Clean old snapshots. | |
lvs -o lv_name --noheadings | sed -n "s@$BACKUP_PREFIX@@p" | while read DATE; do | |
TS_DATE=$(date -d "$DATE" +%s) | |
TS_NOW=$(date +%s) | |
AGE=$(( (TS_NOW - TS_DATE) / 86400)) | |
if [ "$AGE" -ge "$KEEP_DAYS" ]; then | |
VOLNAME="$BACKUP_PREFIX$DATE" | |
/sbin/lvremove -f "$VG/$VOLNAME" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment