-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmbackup
executable file
·100 lines (86 loc) · 2.71 KB
/
mbackup
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
#!/bin/bash
################################################################################
# mbackup - mobile backup using rsync.
################################################################################
unset copy_log exclude
interval=0
batt_interval=0
nice_level=20
script_name=$(basename ${0})
last_file="${HOME}/.${script_name}.last"
log_file="${HOME}/.${script_name}.log"
lock_file="${TMPDIR:-/tmp}/${script_name}.lock"
################################################################################
print_usage() {
cat <<EOM
Usage: ${script_name} [-b batt_interval [-c log_dest] [-e exclude-file]
[-i interval] [-n nice] source destination
Syncs 'source' to 'destination' using rsync. This tool is designed to be run
as a semi-frequent cron job, but only executes if the specified interval has
elapsed since the last backup.
Options:
* -b batt_interval interval to use when on battery power.
* -c log_dest copy the backup log to 'log_dest' if errors occur.
* -e exclude_file ignore entries in 'exclude_file' while backing up.
* -i interval number of hours between backups.
* -n nice niceness level; defaults to ${nice_level}.
EOM
}
on_battery() {
pmset -g batt | grep 'Battery Power' &> /dev/null
}
while getopts "b:c:e:i:n:" flag; do
case ${flag} in
b) batt_interval=${OPTARG} ;;
c) copy_log=${OPTARG} ;;
e) exclude="--exclude-from=${OPTARG}" ;;
i) interval=${OPTARG} ;;
n) nice_level=${OPTARG} ;;
?) print_usage; exit 1 ;;
esac
done
shift $((OPTIND - 1))
src=${1}
dest=${2}
if [[ -z "${src}" || -z "${dest}" ]]; then
echo "error: source and destination not specified."
print_usage
exit 1
fi
if on_battery; then
if [[ ${batt_interval} -gt 0 ]]; then
interval=${batt_interval}
fi
fi
# Only execute if (now - last) is greater or equal to the backup interval.
interval_secs=$((interval * 60 * 60))
last=$(cat "${last_file}" 2> /dev/null)
now=$(date +%s)
if [[ $((now - last)) -lt ${interval_secs} ]]; then
exit 0
fi
# Don't run multiple instances.
if [[ -e "${lock_file}" ]]; then
pid=$(cat "${lock_file}" 2> /dev/null)
if ps "${pid}" &> /dev/null; then
echo "Backup instance already running."
exit 1
fi
fi
echo $$ > "${lock_file}"
nice -n ${nice_level} \
rsync -avz --delete-after --inplace --partial \
--exclude=".${script_name}.log" \
--exclude=".${script_name}.last" \
"${exclude}" \
"${src}" "${dest}" &> ${log_file}
result=$?
rm "${lock_file}"
if [[ ${result} -eq 0 ]]; then
date +%s > "${last_file}"
else
if [[ ${result} -ne 255 && -n "${copy_log}" ]]; then
cp "${log_file}" "${copy_log}-$(date '+%F')"
fi
exit 1
fi