Backup-Skript

Hier als Bei­spiel mein Back­up-Skript. Es moun­tet, falls noch nicht ge­sche­hen, die NAS- und die Back­up-Plat­te und führt mit­tels Rsync ein Back­up des NAS durch.

#! /bin/bash
# backup from nas to /backupdisk
# root directories on NAS must exist or
# the corresponding share will not be backuped

NASROOT=/auto/nas
BACKUPROOT=/backupdisk

# definitions for findNewDisk()
MASK="/dev/sd?"
TIMEOUT=60

# Waits for a new disk to be connected
# returns device path in $newDisk if success
# or times out after $TIMEOUT seconds
WaitNewDisk()
{
disks=($MASK)
newDisk=""
t=0
while [ $t -lt $TIMEOUT ]; do
	x=($MASK)
	for d in ${x[@]}; do
		if [[ ! "${disks[@]}" =~ "$d" ]];
		then
		newDisk="$d"
		return
		fi
	done
	sleep 1
	let t=t+1
done
}

#####################################################

RSYNCOPTS="-a -H -v --delete"

# Note: this will automount the nas disk
nasdirs=($NASROOT/*)

if ! grep -q "$NASROOT" /proc/mounts
then
	echo "nas not mounted!"
	exit
fi

if ! grep -q "$BACKUPROOT" /proc/mounts
then
	# power on backup disk
	relay 1 on
	WaitNewDisk
	if [[ "$newDisk" = "" ]];
	then
		echo backup disk failed to appear
		relay 1 off
		exit
	fi
	echo backup disk is $newDisk
	# assuming ${newDisk}1 as target partition
	mount ${newDisk}1 $BACKUPROOT
else
	echo backup disk is already mounted
fi

# backup is done by separate rsyncs for each root folder
# the corresponding directory must already exist on the
# backup disk or it will be skipped!
# this allows to split the backup do several disks
# e.g. one for each directory
for d in ${nasdirs[@]}
do
	d="$(basename $d)"
	if [[ -d "$BACKUPROOT/$d" ]]
		then
		echo Syncing $d
		rsync $RSYNCOPTS $NASROOT/$d/ $BACKUPROOT/$d
	else
		echo skipping $d - directory does not exist on destination
	fi
done

umount /usbdisk && sync && hdparm -Y /dev/sdc
sync
sleep 1
# power off backup disk
relay 1 off

UUIDs

Es gibt ei­ni­ge Ver­wir­rung über UUIDs auf Fest­plat­ten, des­halb hier ei­ne kurze Zu­sam­men­fas­sung:

UUID

Das ist die UUID des Da­tei­sys­tems ei­ner Par­ti­ti­on.

PARTUUID

Die­se UUID kenn­zeich­net ein­deu­tig ei­ne Par­ti­ti­on der Fest­plat­te.

PTUUID

Das ist die UUID der Par­ti­ti­ons­ta­bel­le, al­so prak­tisch die UUID der Fest­plat­te. Sie ist für al­le Par­ti­ti­onen einer Festplatte gleich.