-
Notifications
You must be signed in to change notification settings - Fork 0
/
rzfsnapup.sh
executable file
·112 lines (100 loc) · 2.13 KB
/
rzfsnapup.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
#!/bin/sh
# rsync backup to zfs and creating snapshots with zfsnap
# Copyright 2014,2016 Alexander Dahl <[email protected]>
set -e
set +x
# set your os paths here
DATE='/bin/date'
RSYNC='/usr/local/bin/rsync'
SSH='/usr/bin/ssh'
ZFSNAP='/usr/local/sbin/zfSnap'
# function definitions
print_usage() {
echo "Usage: $0 [options] remotehost remotepath localzfs"
echo "(on FreeBSD put options _there_ directly after ${0} )"
echo ''
echo 'Options:'
echo ' -h this help'
echo ' -i identity_file path to private key file'
echo ' -s sparseconf config file for sparse trees'
echo ' -x pass --one-file-system to rsync'
}
# "main"
# we only use classic getopt because this runs on linux and freebsd
IDENTITY_FILE='/root/.ssh/backup'
X=''
ARGS=`getopt hi:s:x $*`
if [ $? -ne 0 ]
then
print_usage
exit 2
fi
set -- $ARGS
while true
do
case "$1" in
-h)
print_usage
exit 0
;;
-i)
IDENTITY_FILE="$2"
shift; shift
;;
-s)
SPARSE_CONFIG="$2"
shift; shift
;;
-x)
X='--one-file-system'
shift
;;
--)
shift; break;;
esac
done
# sync with rsync
if [ -z "${SPARSE_CONFIG}" ]
then
echo "> syncing ${1}:${2}"
${RSYNC} --stats -zahH ${X} --delete -e "${SSH} -i '${IDENTITY_FILE}'" ${1}:${2}/ /${3}
echo ''
else
if [ -f "${SPARSE_CONFIG}" ]
then
echo ">> using ${SPARSE_CONFIG} as sparse tree config"
. ${SPARSE_CONFIG}
else
echo "sparse config '${SPARSE_CONFIG}' not found"
exit 1
fi
for _idx in $(seq ${SPARSE_FOLDER_N})
do
eval _folder='$SPARSE_FOLDER_'${_idx}
if [ -z "${_folder}" ]
then
break
fi
mkdir -p /${3}/${_folder}
echo "> syncing ${1}:${2}/${_folder}"
${RSYNC} ${X} -zahH --delete -e "${SSH} -i '${IDENTITY_FILE}'" \
${1}:${2}/${_folder}/ /${3}/${_folder}
done
echo ''
fi
# take snapshot
DAY_OF_MONTH="$(${DATE} +%d)"
DAY_OF_WEEK="$(${DATE} +%u)"
if [ "${DAY_OF_MONTH}" = '12' ]
then
echo '> creating monthly zfs snapshot'
${ZFSNAP} -v -s -S -a 2y ${3}
elif [ "${DAY_OF_WEEK}" = '3' ]
then
echo '> creating weekly zfs snapshot'
${ZFSNAP} -v -s -S -a 2m ${3}
else
echo '> creating daily zfs snapshot'
${ZFSNAP} -v -s -S -a 2w ${3}
fi
echo ''