summaryrefslogtreecommitdiffstats
path: root/simplebackup/backup.sh
blob: 0eaf643c79fa9133702e9e0f79f387fb394f9b3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/sh

targetnode="/dev/disk/by-uuid/MY-UUID"
targetmp="/mnt/backup/target"
targetsub="datensicherung"
sourcepath="/mnt/backup/snapshots/root"
btrfs=1

die()
{
	local msg1="$1"
	local msg2="$2"

	if [ -n "$msg1" ]; then
		echo "$msg1" >&2
	fi
	echo >&2
	echo >&2
	echo >&2
	echo >&2
	echo "### FEHLER! FEHLER! FEHLER! ###" >&2
	echo "###" >&2
	if [ -n "$msg2" ]; then
		echo "### $msg2" >&2
	else
		echo "### Die Datensicherung wurde abgebrochen und ist unvollständig!" >&2
	fi
	echo "###" >&2
	echo "### FEHLER! FEHLER! FEHLER! ###" >&2
	echo >&2
	read -p "" x
	exit 1
}

# Become root.
mypath="$(realpath -e "$0")"
if [ "$(id -u)" != "0" ]; then
	if [ "$1" = "SECOND_STAGE" ]; then
		die "Second stage failed."
	else
		exec sudo "$mypath" SECOND_STAGE
		exit 1
	fi
fi

# Setup cleanup handler.
cleanup()
{
	umount -f "$targetmp" >/dev/null 2>&1
	if [ $btrfs -ne 0 ]; then
		btrfs subvolume delete "$sourcepath" >/dev/null 2>&1
	fi
}
trap cleanup EXIT

# Create the snapshot
if [ $btrfs -ne 0 ]; then
	btrfs subvolume delete "$sourcepath" >/dev/null 2>&1
	btrfs subvolume snapshot -r / "$sourcepath" ||\
		die "btrfs snapshot failed"
fi

# Mount the backup drive.
mkdir -p "$targetmp" || die "mkdir target failed."
if ! [ -b "$targetnode" ]; then
	die "dev node not present" "The Backup-Festplatte ist nicht angeschlossen!"
fi
umount -f "$targetnode" >/dev/null 2>&1
umount -f "$targetmp" >/dev/null 2>&1
mount "$targetnode" "$targetmp" || die "target mount failed"

# Sync the backup drive with the source drive.
mkdir -p "$targetmp/$targetsub" || die
while true; do
	rsync -aHAX --inplace --delete-before --progress \
		"$sourcepath"/ \
		"$targetmp/$targetsub"
	res=$?
	[ $res -eq 24 ] && continue
	[ $res -ne 0 ] && die
	break
done
if [ $btrfs -ne 0 ]; then
	while true; do
		rsync -aHAX --inplace --delete-before --progress \
			/boot/ \
			"$targetmp/$targetsub"_boot
		res=$?
		[ $res -eq 24 ] && continue
		[ $res -ne 0 ] && die
		break
	done
fi
umount "$targetmp" || die "umount failed"
if [ $btrfs -ne 0 ]; then
	btrfs subvolume delete "$sourcepath" ||\
		die "btrfs snapshot delete failed"
fi

echo
echo
echo
echo "########################################################"
echo "###   Alles Ok!                                      ###"
echo "###   Die Festplatte kann jetzt abgesteckt werden.   ###"
echo "########################################################"
read -p "" x
bues.ch cgit interface