-
Notifications
You must be signed in to change notification settings - Fork 2
/
time-travel.sh
executable file
·60 lines (53 loc) · 2.41 KB
/
time-travel.sh
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
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# VARIABLES
# -----------------------------------------------------------------------------
EXIT_CODE="1"
SOURCE="$1"
DESTINATION="$2"
EXCLUDE_FILE="$3"
TMPDIR=/tmp
if [[ ! -d $TMPDIR ]]; then
TMPDIR=$(mktemp -d)
echo "/tmp does not exist. Creating temporary directory at " $TMPDIR
fi
SCRIPTDIR=$(dirname $0)
# Create lock file
if [[ ! -f $TMPDIR/.time-travel.lock ]]; then
touch $TMPDIR/.time-travel.lock
else
EXIT_CODE="1"
$SCRIPTDIR/terminal-notifier.app/Contents/MacOS/terminal-notifier -title "Time Travel" -message "Previous backup still running."
exit $EXIT_CODE
fi
# -----------------------------------------------------------------------------
# NOTIFICATION-CENTER STARTUP POPUP
# -----------------------------------------------------------------------------
$SCRIPTDIR/terminal-notifier.app/Contents/MacOS/terminal-notifier -title "Time Travel" -message "Backup started..."
# -----------------------------------------------------------------------------
# RUN RSYNC_TIME_BACKUP
# -----------------------------------------------------------------------------
nice -n 10 $SCRIPTDIR/rsync-time-backup/rsync_tmbackup.sh --rsync-set-flags "-D --compress --numeric-ids --links --hard-links --itemize-changes --times --recursive --perms --owner --group --stats --human-readable" $SOURCE $DESTINATION $EXCLUDE_FILE > $TMPDIR/time-travel.log
# Save rsync-time-backup exit-code
EXIT_CODE=$?
# -----------------------------------------------------------------------------
# ERROR HANDLING
# -----------------------------------------------------------------------------
TOTALTRANSFERREDSIZE=$(grep 'Total transferred file size' $TMPDIR/time-travel.log)
if [[ -z "$TOTALTRANSFERREDSIZE" || $EXIT_CODE != "0" ]]; then
# Error
$SCRIPTDIR/terminal-notifier.app/Contents/MacOS/terminal-notifier -title "Time Travel" -message "Backup was interrupted. Code: $EXIT_CODE"
EXIT_CODE="1"
else
$SCRIPTDIR/terminal-notifier.app/Contents/MacOS/terminal-notifier -title "Time Travel" -message "$TOTALTRANSFERREDSIZE"
EXIT_CODE="0"
fi
# -----------------------------------------------------------------------------
# CLEAN UP
# -----------------------------------------------------------------------------
if [ -z "$TOTALTRANSFERREDSIZE" ]; then
# Only delete log file in case of success
rm -rf $TMPDIR/time-travel.log
fi
rm $TMPDIR/.time-travel.lock
exit $EXIT_CODE