1
|
#!/bin/bash -e
|
2
|
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
|
3
|
|
4
|
if self_not_included; then
|
5
|
|
6
|
alias use_sync='declare prefix=sync_; import_vars; unset prefix'
|
7
|
|
8
|
upload() # usage: local_dir=... remote_url=serv:dir [swap=1] [subpath=...] \
|
9
|
# upload [opt|path]...
|
10
|
# swap: downloads instead of uploads
|
11
|
# subpath: relative to *currdir*, not $local_dir as in `put`
|
12
|
# path: relative to *currdir*, not $local_dir. when invoking from a script, use
|
13
|
# an absolute path (e.g. "$(dirname "${BASH_SOURCE[0]}")"/...).
|
14
|
# requires put from https://uutils.googlecode.com/svn/trunk/bin/put
|
15
|
{
|
16
|
log-- echo_func; log-- kw_params local_dir remote_url
|
17
|
log-- echo_vars swap subpath
|
18
|
|
19
|
# make subpath relative to currdir
|
20
|
if test "$subpath"; then
|
21
|
local subpath="$(base_dir="$local_dir" canon_rel_path "$subpath")"
|
22
|
if test "$subpath" = .; then unset subpath; fi
|
23
|
fi
|
24
|
|
25
|
# change path args to --includes
|
26
|
local has_includes= only_explicit_files=1 args=()
|
27
|
local a; for a in "$@"; do
|
28
|
if starts_with -- "$a"; then args+=("$a") # option, so leave as is
|
29
|
else # path
|
30
|
# also need to --include parent dirs for each --include path
|
31
|
path_parents "$(base_dir="$local_dir" canon_dir_rel_path "$a")"
|
32
|
args+=("${dirs[@]/#/--include=/}") # prepend each with --include=/
|
33
|
has_includes=1
|
34
|
if ! could_be_file "$a"; then only_explicit_files=; fi
|
35
|
fi
|
36
|
done
|
37
|
if test ! "$has_includes"; then only_explicit_files=; fi #root dir->not file
|
38
|
set -- "${args[@]}" ${has_includes:+--exclude="**"}
|
39
|
|
40
|
if test ! "$only_explicit_files"; then # don't rsync-ignore explicit files
|
41
|
# use .rsync_ignore
|
42
|
set -- --filter="dir-merge,- .rsync_ignore" "$@"
|
43
|
|
44
|
# use directional .rsync_filter
|
45
|
local filter_type=upload; if test "$swap"; then filter_type=download; fi
|
46
|
set -- --filter="dir-merge .rsync_filter.$(hostname -s).$filter_type" \
|
47
|
--filter="dir-merge .rsync_filter.$filter_type" "$@"
|
48
|
fi
|
49
|
|
50
|
src="$local_dir" dest="$remote_url" command put "$@"
|
51
|
}
|
52
|
|
53
|
db_copy() # usage: [live=] from=... [to=...] db_copy rsync_opts...
|
54
|
# **IMPORTANT**: to produce a consistent snapshot, DB must be shut down
|
55
|
{
|
56
|
echo_func; kw_params live from to; : "${from:?}"
|
57
|
local_export live="${live-1}"; local to="${to:-$from.bak}"
|
58
|
sudo mkdir "$to"
|
59
|
|
60
|
local_dir="$from" remote_url="$to" inplace=1 sudo upload --delete-excluded \
|
61
|
--exclude=.DO_NOT_BACKUP "$@" # allow tape backup
|
62
|
# inplace: for large files, don't re-copy entire file
|
63
|
}
|
64
|
|
65
|
db_snapshot() # usage: [live=] ctl=... from=... [to=...] db_snapshot rsync_opts
|
66
|
# ctl(cmd:{start|stop}): daemon controller that takes command param
|
67
|
{
|
68
|
echo_func; kw_params ctl from to; : "${ctl:?}"
|
69
|
become_sudo # don't let program hang at later sudo prompts
|
70
|
|
71
|
# trim bloated temp files (eg. from rolled back changes)
|
72
|
sudo "$ctl" restart
|
73
|
|
74
|
# copy changes before stopping DB to minimize the time that it's shut down
|
75
|
db_copy "$@" # copy changes since last backup
|
76
|
db_copy "$@" # copy changes that occurred during the copy operation
|
77
|
|
78
|
# make copy a consistent snapshot
|
79
|
sudo "$ctl" stop
|
80
|
try db_copy "$@"
|
81
|
sudo "$ctl" start
|
82
|
end_try
|
83
|
}
|
84
|
|
85
|
fi
|