-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash.common
executable file
·80 lines (67 loc) · 1.75 KB
/
bash.common
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
#!/bin/bash
# Don't re-run installs or downloads unless MINAGE seconds have passed since last time
MAXAGE=604800
TS_NOW=$( date '+%s' )
function die() {
echo "FATAL ERROR: $*" >&2
exit 1
}
###
# If path exists and mtime is less than maxage, return 0
# Otherwise, return 1
# PARAMS:
# 1. path - path to dir or file
# 2. maxage - maximum age in secs to be considered recent
###
function is_recent() {
[[ $DEBUG -eq 1 ]] && set -x
local path=$1
local maxage=$2
local rv=1
local age
if [[ -e $path ]] ; then
ts=$( stat -c '%Y' $path )
let "age = $TS_NOW - $ts"
if [[ $age -lt $maxage ]] ; then
rv=0
fi
fi
return $rv
}
###
# Return 0 if current user is root, return 1 otherwise
###
function assert_root() {
[[ $(id -u) -eq 0 ]]
}
###
# Recursively sync files from SRCDIR to TGTDIR
# Perform a chown on each file changed in TGTDIR
###
function copy_files() {
[[ $DEBUG -eq 1 ]] && set -x
local srcdir="$1"
local tgtdir="$2"
local usr_grp=$( stat -c '%u:%g' "$tgtdir" )
chown -R $usr_grp $srcdir/
local rsync_verbose=''
[[ $VERBOSE -eq 1 ]] && rsync_verbose='-v'
rsync -rog $rsync_verbose "$srcdir/" "$tgtdir/"
# Verify copied files are identical
diff_verbose='-q'
[[ $VERBOSE -eq 1 ]] && diff_verbose='-s'
find "$srcdir" -type f \
| while read; do
tgt_fn=$tgtdir/${REPLY#$srcdir/}
diff $diff_verbose $REPLY $tgt_fn || die "Copied file mismatch: $REPLY $tgt_fn"
done
}
###
# Update apt libraries if they are older than one day
# If apt libraries are younger than one day, do nothing
###
function apt_update() {
[[ $DEBUG -eq 1 ]] && set -x
is_recent '/var/lib/apt/lists' 86400 \
|| sudo apt -yqq update
}