Last active
December 28, 2022 05:15
-
-
Save ismet55555/5841ab812b38325be28678b96163e896 to your computer and use it in GitHub Desktop.
LVM Storage Remote Backup
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 | |
# ========================================================= | |
# LVM Snapshot Remote Backup | |
# Ismet Handzic 2019 | |
# ========================================================= | |
# | |
# This script creates a LVM partition snapshot and sends | |
# it to a remote location via rsync | |
# | |
# IMPORTANT: | |
# - The system parition must be in LVM formate | |
# - You must have free unalocated space in your volume group | |
# - To reduce the current logical volume (LV), you must unmount the disk | |
# - Can be done via Live USB (create with Balena Etcher) | |
# - More info: | |
# - https://www.tecmint.com/extend-and-reduce-lvms-in-linux/ | |
# - https://www.digitalocean.com/community/tutorials/how-to-use-lvm-to-manage-storage-devices-on-ubuntu-18-04#removing-or-downsizing-lvm-components | |
# - You must set up a SSH key with the remote location | |
# - Create a public and private SSH Key: | |
# ssh-keygen (default location, no password) | |
# - Distribute the public key to remote machine: | |
# ssh-copy-id <REMOTE USERNAME>@<REMOTE HOST ADDRESS/IP> | |
# | |
# | |
# NOTES: | |
# - The size of the snapshot is the size of the changes. | |
# So, lots of anticipated changes, more size. However, | |
# add more than you anticipate. | |
# - You can use this scrip with a cron job running it | |
# at regular intervals | |
# - Can use the folloiwng to troubleshoot: | |
# - pvs - list physical volumes | |
# - vgs - list volume groups | |
# - lvs - list logical volumes | |
# - pvscan - details about physical volumes | |
# - vgdisplay - details about volume groups | |
# - lvdisplay - details about locial volumes | |
# ========================================================= | |
echo "===============================================" | |
echo | |
echo $(date) | |
echo | |
echo "Starting System LVM Snapshot Backup ..." | |
echo | |
# ========================================================= | |
# This stops the script immediately if an error occurs | |
set -e | |
# ========================================================= | |
# Script Variables for conveneince | |
SNAPSHOT_NAME="system_backup_snapshot_$(date +%Y_%m)" | |
LVM_SNAPSHOT_SIZE="50G" | |
REMOTE_USER="YOUR REMOTE USERNAME" | |
REMOTE_IP="192.168.X.XX" | |
REMOTE_SSH_PRIVATE_KEY="/home/$REMOTE_USER/.ssh/id_rsa" | |
REMOTE_DIR="/home/$REMOTE_USER/Backup_Jenkins_Server/Snapshot" | |
echo "Script Settings:" | |
echo " Snapshot image name: $SNAPSHOT_NAME" | |
echo " Remote user name: $REMOTE_USER" | |
echo " Remote IP address: $REMOTE_IP" | |
echo " SSH key location: $REMOTE_SSH_PRIVATE_KEY" | |
echo " Remote backup directory: $REMOTE_DIR" | |
echo | |
# ========================================================= | |
# Creat the snapshot logical volume | |
echo "$(date +%T) - Creating snapshot logical volume ..." | |
lvcreate --size $LVM_SNAPSHOT_SIZE --snapshot --name $SNAPSHOT_NAME /dev/ubuntu-vg/root | |
# ========================================================= | |
# Make the directory in the /mnt/ folder if it doesnt already exist | |
echo "$(date +%T) - Creating mount directory in /mnt ..." | |
if [ ! -d $SNAPSHOT_NAME ]; then | |
mkdir /mnt/$SNAPSHOT_NAME | |
fi | |
# ========================================================= | |
# Mount the snapshot (read-only) | |
echo "$(date +%T) - Mounting snapshot logical volume ..." | |
mount -o ro /dev/ubuntu-vg/$SNAPSHOT_NAME /mnt/$SNAPSHOT_NAME/ | |
# ========================================================= | |
# Check to see if remote backup directory exists | |
echo "$(date +%T) - Creating directory in remote/destination location ..." | |
if ssh -i $REMOTE_SSH_PRIVATE_KEY $REMOTE_USER@$REMOTE_IP "[ ! -d $REMOTE_DIR ]"; then | |
# If it does not exist, create it | |
ssh -i $REMOTE_SSH_PRIVATE_KEY $REMOTE_USER@$REMOTE_IP "mkdir $REMOTE_DIR" | |
fi | |
# ========================================================= | |
# Send the snapshot to a remote location | |
# Options: | |
# -v - verbose | |
# -r - recursive into directories | |
# -a - archive mode | |
# -z - compress | |
# -h - human readable | |
# -p - perserver permission | |
# -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" | |
echo "$(date +%T) - Transfering snapshot files to remote location ..." | |
echo | |
rsync -razh -e "ssh -i $REMOTE_SSH_PRIVATE_KEY" /mnt/$SNAPSHOT_NAME/ $REMOTE_USER@$REMOTE_IP:$REMOTE_DIR | |
echo | |
echo "$(date +%T) - Transfer complete" | |
# ========================================================= | |
# Unmount the snapshot | |
echo "$(date +%T) - Unmounting snapshot logical volume ..." | |
umount --lazy /mnt/$SNAPSHOT_NAME | |
# ========================================================= | |
# Remove the snapshot logical volume | |
echo "$(date +%T) - Removing snapshot logical volume ..." | |
lvremove -y ubuntu-vg/$SNAPSHOT_NAME | |
# ========================================================= | |
# Remove the directory in /mnt/ | |
echo "$(date +%T) - Removing mount directory in /mnt/ ..." | |
rm -r /mnt/$SNAPSHOT_NAME | |
echo | |
echo "LVM snapshot backup complete! :)" | |
echo | |
echo | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment