-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.sh
134 lines (119 loc) · 3.54 KB
/
functions.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/zsh
# common functions for backups
set -o errexit
set -o nounset
MOCK=
#MOCK=echo
RSYNC=rsync
YABRC=/Users/hunter/go/bin/yabrc
YABRC_DIR=/Users/hunter/Backup/yabrc # location of config files and indexes
UPDATE=(update --autosave --fast) # default to fast updates, only checksumming new / changed files
# user confirmation function
function ok () {
vared -p "Does this look ok? " -c ok
if [ "$ok" != "y" ]; then
exit 1
fi
unset ok
echo
}
MEDIA_EXCLUDES=(--exclude='*.lrdata' --exclude='*.lroldplugin' --exclude='*.photoslibrary' --exclude='Photo Booth Library')
# asssumes SOURCE and DEST are defined
function backup() {
# default to mirroring backups by deleting files in destination that are not in source
mirror="--delete"
dry_run=""
silent=false
# parse options; note MacOS getopt is not GNU, so easier to just fake it
# does not handle errors or out of order options
while true; do
case "$1" in
--copy_only)
mirror="";;
--dry_run)
dry_run=(--dry-run --itemize-changes);;
--silent)
silent="true";;
*)
break;;
esac
shift
done
if [ "$silent" = "false" ]; then
if [ -z "$dry_run" ]; then
echo "Copying files... "
else
echo "Checking what will be copied... "
fi
fi
# the rest of the args will usually be a named location
# this provides symmetry with the yabrc_ functions
while [ $# -gt 0 ]; do
location=$1
opts=""
src=
dest=$DEST
local mirror=$mirror
# note that any --exclude (globs) need to match up with ignoredDirs (regexes) in yabrc configs
case "$location" in
backup)
src=$SOURCE/Backup;;
documents)
opts=--exclude=Adobe
src=$SOURCE/Documents;;
development)
opts=(--exclude=bin --exclude=build --exclude=target --exclude=apk_cache --exclude=__pycache__ --exclude=htmlcov)
src=$SOURCE/Development;;
media)
opts=$MEDIA_EXCLUDES
src=$SOURCE/Media;;
pictures)
# laptop Pictures -> Backup/Media/Images
# note trailing slash to copy contents _under_ Pictures to Media/Images
opts=(--exclude=RAW $MEDIA_EXCLUDES)
src=$SOURCE/Pictures/
dest=$DEST/Media/Images;;
partial_raw)
# laptop RAW is a partial copy; never mirror!
mirror=""
src=$SOURCE/Pictures/RAW/
dest=$DEST/Media/Images/RAW;;
*)
# unknown location, assume provided args are a source and destination
src=$1
shift
dest=$1;;
esac
shift
$MOCK $RSYNC -rth --copy-links $mirror $dry_run $opts $src $dest
done
}
function yabrc_update () {
# $1 is the backup name / disk
# all other params are index names to update
# YABRC indexes are named <disk_name>/<disk_name>_<index_name>
local disk=$1
local path=$YABRC_DIR/$1
shift
echo "Updating indexes for $disk..."
for index_name in "$@"; do
echo "\n----- ${index_name} -----\n"
$MOCK $YABRC $UPDATE $path/${disk}_${index_name}.properties
done
echo
}
function yabrc_compare () {
# $1 is the source backup name / disk, $2 is the destination
source=$1
dest=$2
shift 2
echo "Comparing $source with $dest..."
for index_name in "$@"; do
echo "\n----- ${index_name} -----\n"
local index1=$YABRC_DIR/$source/${source}_${index_name}.properties
local index2=$YABRC_DIR/$dest/${dest}_${index_name}.properties
set +o errexit # compare returns non-zero if there are changes; ensure multiple compares can run
$MOCK $YABRC compare $index1 $index2
set -o errexit
done
}