-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup.sh
More file actions
executable file
·77 lines (67 loc) · 2.04 KB
/
backup.sh
File metadata and controls
executable file
·77 lines (67 loc) · 2.04 KB
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
#!/bin/bash
# backup.sh: System backup using rsync
SRCDIR=""
DESTFS="unix"
# cmd line args
while getopts "d:s:w" opt; do
case $opt in
d)
DESTDIR=$OPTARG
;;
s)
SRCDIR=$OPTARG
;;
w)
DESTFS="windows"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
if [ -z "$DESTDIR" ]; then
echo "No destination defined. Usage: $0 -d destination" >&2
exit 1
elif [ ! -d "$DESTDIR" ]; then
echo "Invalid path: $DESTDIR" >&2
exit 1
elif [ ! -w "$DESTDIR" ]; then
echo "Directory not writable: $DESTDIR" >&2
exit 1
fi
case "$DESTDIR" in
"/mnt") ;;
"/mnt/"*) ;;
"/media") ;;
"/media/"*) ;;
"/run/media/"*) ;;
*) echo "Destination not allowed." >&2
exit 1
;;
esac
START=$(date +%s)
# TODO: Windows incremental sync maybe broken
# TODO: Find out better rsync command that does not involve listing all the source dirs on cmd line
# TODO: add cmd line args and output to debug commands being run
if [ "$DESTFS" = unix ]; then
rsync -aAXv --delete-after "${SRCDIR}"/* "$DESTDIR" --exclude={/dev/*,/proc/*,/sys/*,/run/*,/mnt/*,/media/*,/lost+found,/swapfile,/home/*/.gvfs,/home/*/.cache/*}
elif [ "$DESTFS" = windows ]; then
# for ntfs and fat
##rsync -vrc --no-p --delete-after "${SRCDIR}"/* "$DESTDIR" --exclude={'$RECYCLE.BIN/','System Volume Information/'}
rsync -vrt --modify-window=2 --no-p --delete-after "${SRCDIR}"/* "$DESTDIR" --exclude={'$RECYCLE.BIN/','System Volume Information/'}
else
echo "Invalid destination filesystem"
exit 1
fi
status=$?
FINISH=$(date +%s)
# Reporting
content_to_write="total time: $(( (FINISH - START) / 60 )) minutes, $(( (FINISH - START) % 60 )) seconds"
time_to_write="$(date '+%Y-%B-%d-%A-%T' | sed 's/:/_/g')"
if [ "$status" -eq 0 ]; then
mkdir -p "$DESTDIR/backup"
echo "$content_to_write" | tee "$DESTDIR/backup/Backup-from-${time_to_write}"
# same needs to be present in source as well or it will be deleted on next sync!
mkdir -p "$SRCDIR/backup"
echo "$content_to_write" | tee "$SRCDIR/backup/Backup-from-${time_to_write}"
fi