Benutzer-Werkzeuge

Webseiten-Werkzeuge


run_lvmsnap.sh

Optionsparameter




root:~# /usr/local/scripts/run_lvmsnap.sh -h

Dieses Script sichert einzeln  oder ueber definierte Gruppen LV's mit Hilfe eines Snapshot-Verfahren (LVM).
Die temporaer erstellten Snapshots werden dann per Diskdump (dd) 1:1 gesichert. Alle relevanten Metadaten
die bei einer evtl. Wiederherstellung des Systems nuetzlich sind, werden bei jeder Sicherung mitgesichert.
Die Konfigurationsdatei lvmsnap.conf findet man im Verzeichnis /etc. Eine Komprimierung der Backups findet
nicht statt, deshalb bitte auf den zu Verfuegung stehenden Speicherplatz achten.

Syntax: /usr/local/scripts/run_lvmsnap.sh [-h | -b <nfs|usb> | -s <all|basic|root|extra|<PATH_TO_LV> | -m]

 -h (help):         diese Hilfeseite
 -b (backupmedia):  verwendetes Backupmedium (NAS oder USB-Disk)
 -s (save):         Snapshots erstellen aus den definierten Gruppen all, basic, root, extra sowie von einen einzelnen LV
 -m (MBR):          Master-Boot-Record (MBR) sichern

Bitte beachten, die Optionen bei -s sind exklusiv, d.h. sie schliessen sich gegenseitig aus und somit nur einzelnen verwendet werden.


Konfigurationsdatei lvmsnap.conf zum Shellscript run_lvmsnap.sh

lvmsnap.conf
GROUP_BASIC="/ /home /var /mnt/share/userdata"
GROUP_EXTRA="/mnt/misc"
GROUP_ROOT="/"
GROUP_EXCLUDE=""
SWAP_EXCLUDE="LVswap"

Auzuschließende Volumes, die nicht mit gesichert werden sollen, können über die Variable GROUP_EXCLUDE benannt werden.


Aufrufbeispiel

Das Backupscript wird der Option -b usb, d.h. das Backupmedium ist eine externe USB-Disk. Die Option -s basic sichert die dieenigen Logical Volumes (LV) die in der Konfigurationdatei lvmsnap.conf in der Variable GROUP_BASIC hinterlegt sind.

# /usr/local/scripts/run_lvmsnap.sh -b usb -s basic

Der nachfolgende Aufruf sichert ein einzelnes LV, das z.B. in der Konfigurationsdatei /etc/lvmsnap.conf enthalten ist.

# /usr/local/scripts/run_lvmsnap.sh -b usb -s /dev/VGdata/LVphoto

Sichert alle die Volumes die in der Konfigurationsdatei /etc/lvmsnap.conf in Variable GROUP_EXTRA definiert sind.

# /usr/local/scripts/run_lvmsnap.sh -b usb -s extra

Sichert das Root Volume das in der Konfigurationsdatei /etc/lvmsnap.conf in Variable GROUP_ROOT definiert ist.

# /usr/local/scripts/run_lvmsnap.sh -b usb -s root


Codelisting

run_lvmsnap.sh
#!/bin/bash
#
########## Variablendefinitionen ##########
# Einlesen der Konfiguaration
. /etc/lvmsnap.conf
 
TIMESTAMP=`date '+%Y%m%d_%H%M'`
LOGFILE_TIMESTAMP=`date '+%d.%m.%Y'`
INDEX_CTRL="0"
LOGFILE="/var/log/lvmsnap.log"
SMTPCONF="/usr/local/scripts/smtpmail.conf"
SMTPMAILER=`which sendemail`
MAILRECIPIENT="michael@localhost"
LOCKFILE=`basename $0.lck`
METADATA="metadata.txt"
MD5SUMFILE="all_md5sums.txt"
ERRFILE="errorlog.txt"
PACKAGE_LIST="package_list.txt"
 
########## Funktionsdefinitionen ##########
usage()
{
	echo "Dieses Script sichert einzeln  oder ueber definierte Gruppen LV's mit Hilfe eines Snapshot-Verfahren (LVM)."
	echo "Die temporaer erstellten Snapshots werden dann per Diskdump (dd) 1:1 gesichert. Alle relevanten Metadaten"
	echo "die bei einer evtl. Wiederherstellung des Systems nuetzlich sind, werden bei jeder Sicherung mitgesichert."
	echo "Die Konfigurationsdatei lvmsnap.conf findet man im Verzeichnis /etc. Eine Komprimierung der Backups findet"
	echo "nicht statt, deshalb bitte auf den zu Verfuegung stehenden Speicherplatz achten."
	echo ""
	echo "Syntax: $0 [-h | -b <nfs|usb> | -s <all|basic|root|extra|<PATH_TO_LV> | -m]"
	echo ""
	echo " -h (help):         diese Hilfeseite"
	echo " -b (backupmedia):  verwendetes Backupmedium (NAS oder USB-Disk)"
	echo " -s (save):         Snapshots erstellen aus den definierten Gruppen all, basic, root, extra sowie von einen einzelnen LV"
	echo " -m (MBR):          Master-Boot-Record (MBR) sichern"
	echo ""
	echo "Bitte beachten, die Optionen sind exklusiv, d.h. sie schliessen sich gegenseitig aus und somit nur einzelnen verwendet werden."
	echo ""
	exit 1
}
 
debug_printarray()
{
	echo "-----------------Debuging Ausgabe--------------------"
	for I in ${ARRAY_LV[@]}
		do
		echo $I
	done
	echo "-----------------Debuging Ausgabe--------------------"
	echo ""
}
 
debug_printvariables()
{
	echo "-----------------Debuging Ausgabe--------------------"
	echo "OPTARG:      "$OPTARG
	echo "ROW:         "$ROW
	echo "TMP:         "$TMP
	echo "LVNAME:      "$LVNAME
	echo "MOUNTPOINT:  "$MOUNTPOINT
	echo "-----------------Debuging Ausgabe--------------------"
	echo ""
}
 
print_message()
{
	MISSINGARGUMENT="Bei der Option -$OPTION wurde ein falscher Mountpoint angegeben"
	NOTAVAILIBLEOPT="Option $2 ist nicht erlaubt und wird ignoriert"
	LOCKMESG="Ein anderer Prozess von $0 blockiert die Ausfuehrung - Abbruch des Scripts"
 
	if [ "$1" = "MISSINGARGUMENT" ]
		then
		echo $MISSINGARGUMENT
	elif [ "$1" = "NOTAVAILIBLEOPT" ]
		then
		echo $NOTAVAILIBLEOPT
	elif [ "$1" = "LOCKED" ]
		then
		echo $LOCKMESG 
	fi
 
}
 
create_LVlist()
{
	RC=0
	# Variable BACKUP_LVNAME liefert nur einen Wert wenn Backupverzeichnis ein LV ist
	BACKUP_LVNAME=`df -P $OF_ROOTDIR | egrep ^\/ | cut -d" " -f1 | cut -d"-" -f2`
	# Variable BACKUP_LVSINGLE liefert ein einzelnes LV als Wert zurueck wenn als LV vorhanden
	BACKUP_LVSINGLE=`df -P | awk '{print $1}' | egrep -w ^"$OPTARG"$`
 
	# fuer einzelnes FS das LV ermitteln
	if [ "$OPTARG" = "$BACKUP_LVSINGLE" ]
		then
		ROW=`/sbin/lvdisplay $BACKUP_LVSINGLE -c |cut -d":" -f1,8`
		TMP=${ROW##*/}
		LVNAME=`echo $TMP | cut -d":" -f1`
		#MOUNTPOINT=`mount | egrep "\-$LVNAME on " | cut -d" " -f3`
		MOUNTPOINT=`lsblk -n -r -o NAME,MOUNTPOINT | egrep "\-$LVNAME.*\/" | cut -d" " -f2`
		ARRAY_LV[$INDEX_LV]=$ROW:$LVNAME:$MOUNTPOINT
 
	# fuer alle anderen Optionen (Gruppen)
	elif [ "$OPTARG" != "$BACKUP_LVSINGLE" ]
		then
		# alle vorhandenen LV's durchsuchen 
		for ROW in `/sbin/lvdisplay -c |cut -d":" -f1,8`
			do
			TMP=${ROW##*/}
			LVNAME=`echo $TMP | cut -d":" -f1`
			#MOUNTPOINT=`mount | egrep "\-$LVNAME on " | cut -d" " -f3`
			MOUNTPOINT=`lsblk -n -r -o NAME,MOUNTPOINT | egrep "\-$LVNAME.*\/" | cut -d" " -f2`
 
			# Backup- und Swap-LV von der Sicherung ausschliessen
			if [ "$LVNAME" = "$BACKUP_LVNAME" ]
				then
				continue
			elif [ "$LVNAME" = "$SWAP_EXCLUDE" ]
				then
				continue
			fi
 
			# LV's von der Excludeliste ausschließen
			if [ "$GROUP_EXCLUDE" ]
				then
				for PART in $GROUP_EXCLUDE
					do
					LVNAME_EXCLUDE=`df -P $PART | egrep ^\/ | cut -d" " -f1 | cut -d"-" -f2`
					if [ "$LVNAME" = "$LVNAME_EXCLUDE" ]
						then
						continue 2
					fi
				done
			fi
 
			# ueber die definierten Gruppenamen die LV's ermitteln
			if [ "$OPTARG" = "all" ]
				then
				ARRAY_LV[$INDEX_LV]=$ROW:$LVNAME:$MOUNTPOINT
				INDEX_CTRL=`expr $INDEX_CTRL + 1`
			elif [ "$OPTARG" = "basic" ]
				then
				for PART in $GROUP_BASIC
					do
					case "$MOUNTPOINT" in
						$PART)	
						ARRAY_LV[$INDEX_LV]=$ROW:$LVNAME:$MOUNTPOINT
						INDEX_CTRL=`expr $INDEX_CTRL + 1`
						;;
					esac
				done
			elif [ "$OPTARG" = "root" ]
				then
				for PART in $GROUP_ROOT
					do
					case "$MOUNTPOINT" in
						$PART)	
						ARRAY_LV[$INDEX_LV]=$ROW:$LVNAME:$MOUNTPOINT
						INDEX_CTRL=`expr $INDEX_CTRL + 1`
						;;
					esac
				done
			elif [ "$OPTARG" = "extra" ]
				then
				for PART in $GROUP_EXTRA
					do
					case "$MOUNTPOINT" in
						$PART)	
						ARRAY_LV[$INDEX_LV]=$ROW:$LVNAME:$MOUNTPOINT
						INDEX_CTRL=`expr $INDEX_CTRL + 1`
						;;
					esac
				done
			else
				RC=1
			fi
			INDEX_LV=`expr $INDEX_LV + 1`
		done
	fi
}
 
do_remove_snapshot()
{
	TIME_INTERVAL="5"
	while [ -e "$VGPATH/SNAPSHOT_$LVNAME_SHORT" ]
		do
		WAIT_INTERVAL="3"
		echo "  --> Waiting $WAIT_INTERVAL seconds before delete SNAPSHOT_$LVNAME_SHORT" >> $LOGFILE 2>&1
		sleep $WAIT_INTERVAL
		/sbin/lvremove -f $VGPATH/SNAPSHOT_$LVNAME_SHORT 1>> $LOGFILE 2>> $OF_DIR/$ERRFILE
		RC_REMOVE=$?
		echo "  --> Waiting $WAIT_INTERVAL seconds after delete SNAPSHOT_$LVNAME_SHORT" >> $LOGFILE 2>&1
		sleep $WAIT_INTERVAL
		if [ "$RC_REMOVE" != "0" ]
			then
			echo "  --> Warning, SNAPSHOT_$LVNAME_SHORT could not be deleted (RC $RC_REMOVE) try again in $TIME_INTERVAL seconds" >> $LOGFILE 2>&1
			sleep $TIME_INTERVAL
		elif [ "$RC_REMOVE" = "0" ]
			then
			echo "  --> Succesfully, SNAPSHOT_$LVNAME_SHORT is deleted (RC $RC_REMOVE)" >> $LOGFILE 2>&1
		fi
	done
}
 
do_snapshot()
{
	for I in ${ARRAY_LV[@]}
		do
		if [ ! -d "$OF_DIR" ]
			then
			mkdir $OF_DIR >> $LOGFILE 2>&1
			if [ $? -eq 0 ]
				then
				echo " --> Create directory succesfully: $OF_DIR" >> $LOGFILE 2>&1 
				chmod 755 $OF_DIR >> $LOGFILE 2>&1
				echo "" >> $LOGFILE 2>&1 
			fi
		fi
 
		LVNAME_LONG=`echo $I | cut -d":" -f1`
		LECOUNTER=`echo $I | cut -d":" -f2`
		LVNAME_SHORT=`echo $I | cut -d":" -f3`
		VGPATH=`dirname $LVNAME_LONG`
		/sbin/lvcreate -l$LECOUNTER -s -n SNAPSHOT_$LVNAME_SHORT $LVNAME_LONG 1>> $LOGFILE 2>> $OF_DIR/$ERRFILE
		INFO_SIZE=`/sbin/lvs -o lv_name,size --noheadings | grep -m1 "$LVNAME_SHORT" | awk '{print $2}'`
		echo "  --> Make a 1:1 copy of the snapshot SNAPSHOT_$LVNAME_SHORT with dd ($INFO_SIZE)" >> $LOGFILE 2>&1
		dd if=$VGPATH/SNAPSHOT_$LVNAME_SHORT of=$OF_DIR/$LVNAME_SHORT-$TIMESTAMP.dd >> $LOGFILE 2>&1
		do_remove_snapshot	
 
		if [ -e $OF_DIR/$ERRFILE ]
			then
			echo -n "  --> Size of error file: `ls -ld $OF_DIR/$ERRFILE | cut -d' ' -f5` Byte" >> $LOGFILE 2>&1
			echo "" >> $LOGFILE 2>&1
			echo "  --> Location of error file://$OF_DIR/$ERRFILE" >> $LOGFILE 2>&1
			echo "" >> $LOGFILE 2>&1
		fi
 
	done
}
 
 
do_backup_boot_dev()
{
	BOOTDEVPATH=`mount | grep -w '/boot' | cut -d" " -f1`
	BOOTDEV=`basename $BOOTDEVPATH`
	BOOTNAME=$(basename `mount | grep -w '/boot' | cut -d" " -f3`)
 
	echo "  --> Make a 1:1 copy of the Bootpartition $BOOTDEV with dd" >> $LOGFILE 2>&1
	dd if=$BOOTDEVPATH of=$OF_DIR/$BOOTDEV$BOOTNAME-$TIMESTAMP.dd >> $LOGFILE 2>&1
	RC=$?
	if [ $RC -eq 0 ]
		then
		echo "  --> $BOOTDEVPATH succesfully saved" >> $LOGFILE 2>&1
	else
		echo "  --> $BOOTDEVPATH not succesfully saved" >> $LOGFILE 2>&1
	fi
 
	if [ -e $OF_DIR/$ERRFILE ]
		then
		echo -n "  --> Size of error file: `ls -ld $OF_DIR/$ERRFILE | cut -d' ' -f5` Byte" >> $LOGFILE 2>&1
		echo "" >> $LOGFILE 2>&1
		echo "  --> Location of error file://$OF_DIR/$ERRFILE" >> $LOGFILE 2>&1
		echo "" >> $LOGFILE 2>&1
	fi
}
 
do_mailsend()
{
	MAILSUBJECT="[BATCH: CREATE IMAGE] `hostname`: Partitionsimages erstellt "
	if [ "$SMTPMAILER" ]
	        then
		. "$SMTPCONF"
		sed -n "/\[Log started: $LOGFILE_START_TIMESTAMP\]/,/\[Log ended: $LOGFILE_END_TIMESTAMP\]/p" $LOGFILE | sendemail -q -f $SMTP_SENDER -t $SMTP_RECIPIENT -u "$MAILSUBJECT" -s $SMTP_SERVER -xu $SMTP_RECIPIENT -xp $SMTP_MAIL_PWD 
	else
		sed -n "/\[Log started: $LOGFILE_START_TIMESTAMP\]/,/\[Log ended: $LOGFILE_END_TIMESTAMP\]/p" $LOGFILE | mailx -s "$MAILSUBJECT" $MAILRECIPIENT
	fi
}
 
do_save_mbr()
{
	MBRDEVPATH="/dev/sda"
	MBRDEV=$(basename $MBRDEVPATH)
	MBRNAME="MBR"
	echo "" >> $LOGFILE 2>&1
	echo "  --> Try to save $MBRNAME" >> $LOGFILE 2>&1
	dd if=$MBRDEVPATH of=$OF_DIR/$MBRDEV$MBRNAME-$TIMESTAMP.dd bs=512 count=1 >> $LOGFILE 2>&1
	echo "  --> $MBRNAME from disk $MBRDEVPATH saved" >> $LOGFILE 2>&1
	echo -n "  --> Size of error file: `ls -ld $OF_DIR/$ERRFILE | cut -d' ' -f5` Byte" >> $LOGFILE 2>&1
	echo "" >> $LOGFILE 2>&1
	echo "  --> Location of error file://$OF_DIR/$ERRFILE" >> $LOGFILE 2>&1
}
 
do_save_mbr_advanced()
{
	MBRDEVPATH="/dev/sda"
	MBRDEV=$(basename $MBRDEVPATH)
	MBRNAME="MBRadvanced"
	echo "" >> $LOGFILE 2>&1
	echo "  --> Try to save $MBRNAME" >> $LOGFILE 2>&1
	dd if=$MBRDEVPATH of=$OF_DIR/$MBRDEV$MBRNAME-$TIMESTAMP.dd bs=1M count=1 >> $LOGFILE 2>&1
	echo "  --> $MBRNAME from disk $MBRDEVPATH saved" >> $LOGFILE 2>&1
	echo -n "  --> Size of error file: `ls -ld $OF_DIR/$ERRFILE | cut -d' ' -f5` Byte" >> $LOGFILE 2>&1
	echo "" >> $LOGFILE 2>&1
	echo "  --> Location of error file://$OF_DIR/$ERRFILE" >> $LOGFILE 2>&1
}
 
do_save_vgcfg()
{
	echo "" >> $LOGFILE 2>&1
	echo "  --> Try to save all backup volume group descriptor areas" >> $LOGFILE 2>&1
	for VGNAME in `/sbin/vgs --noheadings -o vg_name|awk '{print $1}'`
		do
		/sbin/vgcfgbackup --file $OF_DIR/$VGNAME"_descriptor-"$TIMESTAMP".cfg" $VGNAME 1>> $LOGFILE 2>> $OF_DIR/$ERRFILE
		RC=$?
		if [ $RC -eq 0 ]
			then
			echo "    --> vgcfgbackup from $VGNAME succesfully saved" >> $LOGFILE 2>&1
		else
			echo "    --> vgcfgbackup from $VGNAME not succesfully saved" >> $LOGFILE 2>&1
		fi
	done
	echo -n "  --> Size of error file: `ls -ld $OF_DIR/$ERRFILE | cut -d' ' -f5` Byte" >> $LOGFILE 2>&1
	echo "" >> $LOGFILE 2>&1
	echo "  --> Location of error file://$OF_DIR/$ERRFILE" >> $LOGFILE 2>&1
}
 
do_save_crypto_header()
{
	for CRYPTO_DEVPATH in `/sbin/blkid | grep -w "crypto_LUKS" | cut -d":" -f1`
		do
		if [ "$CRYPTO_DEVPATH" ]
			then
			CRYPTO_DEV=`basename $CRYPTO_DEVPATH`
			echo "" >> $LOGFILE 2>&1
			echo "  --> Try to save LUKS-Header from the cryptodevice $CRYPTO_DEVPATH" >> $LOGFILE 2>&1
			/sbin/cryptsetup luksHeaderBackup $CRYPTO_DEVPATH --header-backup-file $OF_DIR/$CRYPTO_DEV"_LuksHeader-"$TIMESTAMP".bin" 1>> $LOGFILE 2>> $OF_DIR/$ERRFILE
			RC=$?
			if [ $RC -eq 0 ]
				then
				echo "    --> LUKS-Header from cryptodevice $CRYPTO_DEV succesfully saved" >> $LOGFILE 2>&1
			else
				echo "    --> LUKS-Header from cryptodevice $CRYPTO_DEV not succesfully saved" >> $LOGFILE 2>&1
			fi
			echo -n "  --> Size of error file: `ls -ld $OF_DIR/$ERRFILE | cut -d' ' -f5` Byte" >> $LOGFILE 2>&1
			echo "" >> $LOGFILE 2>&1
			echo "  --> Location of error file://$OF_DIR/$ERRFILE" >> $LOGFILE 2>&1
		fi
	done
}
 
create_logentry()
{
	if [ "$1" = "start" ]
		then
		LOGFILE_START_TIMESTAMP=`date '+%Y-%m-%d %H:%M:%S'`
		echo "[Log started: $LOGFILE_START_TIMESTAMP]" >> $LOGFILE
	elif [ "$1" = "stop" ]
		then
		LOGFILE_END_TIMESTAMP=`date '+%Y-%m-%d %H:%M:%S'`
		echo "[Log ended: $LOGFILE_END_TIMESTAMP]" >> $LOGFILE
	fi
}
 
do_save_metadata()
{
	echo "" >> $LOGFILE 2>&1
	echo " --> Begin to save all metadata from `/bin/hostname`" >> $LOGFILE 2>&1
	echo "  --> All metadata stored in file://$OF_DIR/$METADATA" >> $LOGFILE 2>&1
	echo "  --> Diskgeometry" >> $LOGFILE 2>&1
	/sbin/fdisk -l 2>>/dev/null 1>> $OF_DIR/$METADATA
	echo "" >> $OF_DIR/$METADATA
	echo "  --> List Block Devices" >> $LOGFILE 2>&1
	/bin/lsblk 2>>/dev/null 1>> $OF_DIR/$METADATA
	echo "" >> $OF_DIR/$METADATA
	echo "  --> PVs" >> $LOGFILE 2>&1 
	/sbin/pvdisplay 2>>$OF_DIR/$ERRFILE | egrep -v " leaked on " >> $OF_DIR/$METADATA
	echo "" >> $OF_DIR/$METADATA
	echo "  --> VGs" >> $LOGFILE 2>&1
	/sbin/vgdisplay 2>>$OF_DIR/$ERRFILE | egrep -v " leaked on " >> $OF_DIR/$METADATA
	echo "" >> $OF_DIR/$METADATA
	echo "  --> LVs" >> $LOGFILE 2>&1
	/sbin/lvdisplay 2>>$OF_DIR/$ERRFILE | egrep -v " leaked on " >> $OF_DIR/$METADATA
	echo "" >> $OF_DIR/$METADATA
	echo "  --> Filesystem parameters" >> $LOGFILE 2>&1
	for FS in `mount | egrep "^/dev" | cut -d" " -f1`
		do
		/sbin/tune2fs -l $FS >> $OF_DIR/$METADATA
		echo "" >> $OF_DIR/$METADATA
	done
	echo "  --> Mountpoints" >> $LOGFILE 2>&1
	/bin/mount >> $OF_DIR/$METADATA
	echo "" >> $OF_DIR/$METADATA
	echo "  --> Diskfree" >> $LOGFILE 2>&1
	/bin/df -k >> $OF_DIR/$METADATA
	echo "" >> $OF_DIR/$METADATA
	echo "  --> Network configuration" >> $LOGFILE 2>&1
	/bin/cat /etc/network/interfaces >> $OF_DIR/$METADATA
	echo "" >> $OF_DIR/$METADATA
	echo "  --> Network configuration (systemd)" >> $LOGFILE 2>&1
	if [ -e /etc/systemd/network/bridge.netdev ]
		then
		/bin/cat /etc/systemd/network/bridge.netdev >> $OF_DIR/$METADATA
		echo "" >> $OF_DIR/$METADATA
	fi
	if [ -e /etc/systemd/network/bridge.network ]
		then
		/bin/cat /etc/systemd/network/bridge.network >> $OF_DIR/$METADATA
		echo "" >> $OF_DIR/$METADATA
	fi
	if [ -e /etc/systemd/network/eth.network ]
		then
		/bin/cat /etc/systemd/network/eth.network >> $OF_DIR/$METADATA
		echo "" >> $OF_DIR/$METADATA
	fi
	echo "  --> Hostfile" >> $LOGFILE 2>&1
	/bin/cat /etc/hosts >> $OF_DIR/$METADATA
	echo "" >> $OF_DIR/$METADATA
	echo "  --> NFS configuration" >> $LOGFILE 2>&1
	/bin/cat /etc/idmapd.conf >> $OF_DIR/$METADATA
	echo "" >> $OF_DIR/$METADATA
	if [ -e /etc/exports ]
		then
		/bin/cat /etc/exports >> $OF_DIR/$METADATA
		echo "" >> $OF_DIR/$METADATA
	fi
	if [ -e /etc/udev/rules.d/z99_myown.rules ]
		then
		echo "  --> Personally udev rules" >> $LOGFILE 2>&1
		echo "/etc/udev/rules.d/z99_myown.rules:" >> $OF_DIR/$METADATA
		/bin/cat /etc/udev/rules.d/z99_myown.rules >> $OF_DIR/$METADATA
		echo "" >> $OF_DIR/$METADATA
	fi
	echo "" >> $OF_DIR/$METADATA
	echo "  --> Hardware data" >> $LOGFILE 2>&1
	/usr/bin/inxi -c0 -v7 -r >> $OF_DIR/$METADATA
 
	echo "  --> File type of the image files to determine" >> $LOGFILE 2>&1
	file $OF_DIR/*.dd >> $OF_DIR/$METADATA 2>&1
	echo "  --> Create a list of currently installed packages" >> $LOGFILE 2>&1
	/usr/bin/dpkg -l > $OF_DIR/$PACKAGE_LIST
	echo "  --> Location of package file: file://$OF_DIR/$PACKAGE_LIST" >> $LOGFILE 2>&1
	echo "" >> $LOGFILE 2>&1
	echo -n "  --> Size of error file: `ls -ld $OF_DIR/$ERRFILE | cut -d' ' -f5` Byte" >> $LOGFILE 2>&1
	echo "" >> $LOGFILE 2>&1
	echo "  --> Location of error file: file://$OF_DIR/$ERRFILE" >> $LOGFILE 2>&1
	echo " --> All metadata saved from `/bin/hostname`" >> $LOGFILE 2>&1
}
 
do_checksum()
{
	RC=""
	echo "" >> $LOGFILE 2>&1
	echo " --> Start with creating the md5 sums" >> $LOGFILE 2>&1
	echo "  --> All Checksums stored in file://$OF_DIR/$MD5SUMFILE" >> $LOGFILE 2>&1
	for FILE in $OF_DIR/*.dd
		do
		echo "  --> Create an MD5 checksum from file `basename $FILE`" >> $LOGFILE 2>&1
		md5sum $FILE >> $OF_DIR/$MD5SUMFILE
		RC=$?
		if [ $RC -eq 0 ]
			then
			echo "    --> Successfully created" >> $LOGFILE 2>&1
		elif [ $RC -ne 0 ]
			then
			echo "    --> Failed to create the checksum" >> $LOGFILE 2>&1
		fi
	done
	echo " --> All md5 sums generated" >> $LOGFILE 2>&1
	echo "" >> $LOGFILE 2>&1
	RC=""
}
 
do_lock()
{
	if [ "$1" = "on" ]
		then
		echo " --> Processing begins now" >> $LOGFILE 2>&1
		pidof -x $0 > $LOCKPATH/$LOCKFILE
		echo " --> Set lock in file $LOCKPATH/$LOCKFILE" >> $LOGFILE 2>&1
	elif [ "$1" = "off" ]
		then
		rm -v $LOCKPATH/$LOCKFILE >> $LOGFILE 2>&1
		echo " --> Done, lock file $LOCKPATH/$LOCKFILE succesfully removed" >> $LOGFILE 2>&1
		echo " --> Script run complete" >> $LOGFILE 2>&1
	fi
}
 
do_naswakeup()
{
	ping -c4 -q -W10 diskstation >/dev/null 2>&1
	RC_PING=`echo $?`
	if [ "$RC_PING" = "0" ]
		then
		echo " --> Backup media (NAS) is awake" >> $LOGFILE 2>&1
 
		# Aufwachen des NAS erzwingen durch nachfolgende Aktion (ls) und anschließendern Sleep
		ls -ld $BACKUP_MOUNTDIR/* > /dev/null 2>&1
		sleep 10
 
		RC_NFS=`mount | grep -o "$BACKUP_MOUNTDIR type nfs " | cut -d" " -f3`
		if [ "$RC_NFS" = "nfs" ]
			then
			echo " --> NFS on backup media (NAS) is available" >> $LOGFILE 2>&1
		elif [ "$RC_NFS" != "nfs" ]
			then
			echo " --> NFS on backup media (NAS) is not available" >> $LOGFILE 2>&1
		fi
	elif [ "$RC_PING" != "0" ]
		then
		echo " --> Backup media (NAS) is not awake" >> $LOGFILE 2>&1
	fi
}
 
do_availability_usbstation()
{
	RC=""
	BACKUP_MOUNTDIR_CHECK=`mount | grep -o "$BACKUP_MOUNTDIR type ext4 " | cut -d" " -f1`
	if [ "$BACKUP_MOUNTDIR_CHECK" = "$BACKUP_MOUNTDIR" ]
		then
		RC=0
		echo " --> Remote backup (USB-Station) directory is available" >> $LOGFILE 2>&1
	else
		mount $BACKUP_MOUNTDIR >> $LOGFILE 2>&1
		if [ $? = "0" ]
			then
			RC=0
		else
			RC=1
			echo " --> No backup media (USB-Station) available" >> $LOGFILE 2>&1
		fi
	fi
}
 
do_availability_backupmedia()
{
	RC=""
	BACKUP_MOUNTDIR_CHECK=`mount | grep -o "$BACKUP_MOUNTDIR type nfs " | cut -d" " -f1`
	if [ "$BACKUP_MOUNTDIR_CHECK" = "$BACKUP_MOUNTDIR" ]
		then
		RC=0
		echo " --> Remote backup (NAS-Station) directory is available" >> $LOGFILE 2>&1
	else
		mount $BACKUP_MOUNTDIR >> $LOGFILE 2>&1
		if [ $? = "0" ]
			then
			RC=0
		else
			RC=1
			echo " --> No backup media (NAS-Station) available" >> $LOGFILE 2>&1
		fi
	fi
}
 
do_runtest()
{
	if [ ! -e $LOCKPATH/$LOCKFILE ]
		then
		do_lock on
	else
		print_message LOCKED
		echo "No run available (lockfile: $LOCKPATH/$LOCKFILE)" >> $LOGFILE 2>&1
		create_logentry stop
		exit 2
	fi
}
 
do_set_enviroment()
{
	if [ "$BACKUP_MEDIA_TYPE" = "nfs" ]
		then
		BACKUP_MOUNTDIR="/mnt/import/diskdump"
		OF_ROOTDIR="/mnt/import/diskdump/`hostname`"
		OF_DIR="$OF_ROOTDIR/`date '+%Y-%m-%d-%H%M'`"
		LOCKPATH="$BACKUP_MOUNTDIR/lock"
	elif [ "$BACKUP_MEDIA_TYPE" = "usb" ]
		then
		BACKUP_MOUNTDIR="/mnt/share/usbstation"
		OF_ROOTDIR="/mnt/share/usbstation/diskdump/`hostname`"
		OF_DIR="$OF_ROOTDIR/`date '+%Y-%m-%d-%H%M'`"
		LOCKPATH="$BACKUP_MOUNTDIR/lock"
	else
		echo "Wrong backup media type $BACKUP_MEDIA_TYPE (nfs|usb)"
		exit 4
	fi
 
}
 
do_set_rights()
{
	chmod 640 $OF_DIR/*.dd >> $LOGFILE 2>&1
	chmod 644 $OF_DIR/*.txt $OF_DIR/*.cfg >> $LOGFILE 2>&1
}
 
if [ $# -eq 0 -o $# -gt 4 ]; then
	usage
fi
 
########## Hauptprogramm ##########
while getopts hb:s:n:mx: OPTION; do
	case $OPTION in
		h)
		usage
		;;
 
		b)
		BACKUP_MEDIA_TYPE=$OPTARG
		do_set_enviroment
		;;
 
		s)
		do_set_enviroment
		create_logentry start
		if [ "$BACKUP_MEDIA_TYPE" = "nfs" ]
			then
			do_naswakeup
		fi
		#do_runtest
		if [ "$BACKUP_MEDIA_TYPE" = "nfs" ]
			then
			do_availability_backupmedia
		elif [ "$BACKUP_MEDIA_TYPE" = "usb" ]
			then
			do_availability_usbstation
		fi	
		# do_runtest erst ab hier möglich da Lockfile jetzt auf Backupmedium liegt
		do_runtest
		if [ "$RC" = "1" ]
			then
			do_lock off
			create_logentry stop
			do_mailsend
		elif [ "$RC" = "0" ]
			then
			create_LVlist
			if [ "$RC" = "0" ]
				then
				do_snapshot
				do_backup_boot_dev
				do_save_mbr
				do_save_mbr_advanced
				do_save_vgcfg
				do_save_crypto_header	
				do_save_metadata
				do_checksum
				do_set_rights
				do_lock off
				create_logentry stop
				do_mailsend
			elif [ "$RC" = "1" ]
				then
				echo " Specified backup group $OPTARG does not exist" >> $LOGFILE 2>&1
				do_lock off
				create_logentry stop
				do_mailsend
			fi
		fi
		;;
 
		x)
		create_LVlist
		debug_printarray
		echo "RC="$RC
		;;
 
		m)
		create_logentry start
		do_naswakeup
		do_runtest
		do_availability_backupmedia
		if [ "$RC" = "1" ]
			then
			do_lock off
			create_logentry stop
			do_mailsend
		elif [ "$RC" = "0" ]
			then
			do_save_mbr
			do_save_mbr_advanced
			do_checksum
			do_set_rights
			do_lock off
			create_logentry stop
			do_mailsend
		fi
		;;
 
		\?)
		create_logentry start
		FALSEOPT=$1
		print_message NOTAVAILIBLEOPT $FALSEOPT
		do_lock off
		create_logentry stop
		read ret
		exit 3
		;;
	esac
done
shift `expr $OPTIND - 1`
exit 0
#EOF
run_lvmsnap.sh.txt · Zuletzt geändert: 2020/04/23 11:43 von dwadmin