aboutsummaryrefslogtreecommitdiff
path: root/bin/Backup
blob: 9fe96230e41d20ccd3c4160a3bfc826a4a32c30a (plain) (blame)
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
#!/bin/bash
destination="jmbp@openmediavault.dmz::jmbp"
backupPaths=("Desktop" "Movies" "Music" "Documents" "Pictures" "_code" "Google Drive")

spinner() {
	tput sc; tput setaf 3
	i=1
	sp="/-\|"
	echo -n ' '
	while kill -0 $1 2> /dev/null
	do
		sleep 0.1
	  printf "\b${sp:i++%${#sp}:1}"
	done
	tput sgr0; tput rc; tput el
}
clearLines() {
	tput sc
	for i in 1 2 3 4
	do
		printf "\n"
		tput ed
		tput el
	done
	tput rc
}

tput civis

mtmpdir=$(mktemp -d)
mtempfile=$(mktemp)
#printf "Directory for log files: '$mtmpdir'.\n"
printf "\nLogfile: '$mtempfile'.\n"

# Run rsync on all the directories
error=false
for path in "${backupPaths[@]}"; do
	# Just print the status-text
	tput setaf 4
	printf "\n%-22s" "$main$path..."
	tput sgr0
	printf "\n\n*********************\nSyncing directory '$path'.\n*********************\n" >> "$mtempfile"
	
	# Actually start rsync for the directory
  rsync -az --info=all --log-file="$mtempfile" "$path" "$destination" &> /dev/null &
	rsyncPID=$!
	trap "kill $rsyncPID 2> /dev/null" EXIT
	spinner $rsyncPID
	
	# Catch rsync's exit code by using wait on a dead PID and hope for the best
	wait $rsyncPID
	rsyncExitCode=$?
	
	# Clear a few lines to get rid of the temporary tail command string
	clearLines
	
	# Print final status of the rsync command
	if [ "$rsyncExitCode" -eq "0" ]
	then
		tput setaf 2
		printf "OK"
	else
		tput setaf 1
		printf "Err\n"
		tput setaf 8
		printf "($mtmpdir/$path.log)"
		error=true
	fi
	tput sgr0
done

# Show result
printf "$reset\n\n"
if [ "$error" = true ]; then
	tput setaf 1
	printf "Some part of the backup-process failed, please take a look at the logs\n"
	tput sgr0
	printf "Press any key to exit..."
	read -n1
else
	tput setaf 2
	printf "Backup successfull\n"
	tput sgr0
	printf "Exiting in 5"
	for i in 4 3 2 1 0
	do
		sleep 1
		tput cub1
	  printf "$i"
	done
fi
tput cnorm