Project

General

Profile

1 9800 aaronmk
#!/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 10030 aaronmk
upload() # usage: local_dir=... remote_url=serv:dir [swap=1] [subpath=...] \
9
# upload [opt|path]...
10 10009 aaronmk
# swap: downloads instead of uploads
11 10030 aaronmk
# subpath: relative to *currdir*, not $local_dir as in `put`
12 10015 aaronmk
# path: relative to *currdir*, not $local_dir. when invoking from a script, use
13
#     an absolute path (e.g. "$(dirname "${BASH_SOURCE[0]}")"/...).
14 9800 aaronmk
# requires put from https://uutils.googlecode.com/svn/trunk/bin/put
15
{
16 13273 aaronmk
	log-- echo_func; log-- kw_params local_dir remote_url
17
	log-- echo_vars swap subpath
18 9800 aaronmk
19 10030 aaronmk
	# 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 9889 aaronmk
	# change path args to --includes
26 12955 aaronmk
	local has_includes= only_explicit_files=1 args=()
27 9889 aaronmk
	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 10144 aaronmk
			path_parents "$(base_dir="$local_dir" canon_dir_rel_path "$a")"
32 9889 aaronmk
			args+=("${dirs[@]/#/--include=/}") # prepend each with --include=/
33 9890 aaronmk
			has_includes=1
34 12955 aaronmk
			if ! could_be_file "$a"; then only_explicit_files=; fi
35 9889 aaronmk
		fi
36
	done
37 12955 aaronmk
	if test ! "$has_includes"; then only_explicit_files=; fi #root dir->not file
38 9890 aaronmk
	set -- "${args[@]}" ${has_includes:+--exclude="**"}
39 9811 aaronmk
40 12955 aaronmk
	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 10000 aaronmk
50 9890 aaronmk
	src="$local_dir" dest="$remote_url" command put "$@"
51 9800 aaronmk
}
52
53 13264 aaronmk
db_copy() # usage: [live=] from=... [to=...] db_copy rsync_opts...
54 13260 aaronmk
# **IMPORTANT**: to produce a consistent snapshot, DB must be shut down
55 13258 aaronmk
{
56 13260 aaronmk
	echo_func; kw_params live from to; : "${from:?}"
57 13258 aaronmk
	local_export live="${live-1}"; local to="${to:-$from.bak}"
58
	sudo mkdir "$to"
59
60 13263 aaronmk
	local_dir="$from" remote_url="$to" inplace=1 sudo upload --delete-excluded \
61 13264 aaronmk
--exclude=.DO_NOT_BACKUP "$@" # allow tape backup
62 13260 aaronmk
		# inplace: for large files, don't re-copy entire file
63
}
64
65 13264 aaronmk
db_snapshot() # usage: [live=] ctl=... from=... [to=...] db_snapshot rsync_opts
66 13260 aaronmk
# ctl(cmd:{start|stop}): daemon controller that takes command param
67
{
68
	echo_func; kw_params ctl from to; : "${ctl:?}"
69
70 13261 aaronmk
	# copy changes before stopping DB to minimize the time that it's shut down
71 13264 aaronmk
	db_copy "$@" # copy changes since last backup
72
	db_copy "$@" # copy changes that occurred during the copy operation
73 13261 aaronmk
74
	# make copy a consistent snapshot
75 13258 aaronmk
	sudo "$ctl" stop
76 13264 aaronmk
	try db_copy "$@"
77 13258 aaronmk
	sudo "$ctl" start
78 13276 aaronmk
	end_try
79 13258 aaronmk
}
80
81 9800 aaronmk
fi