-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcephfs-snap
106 lines (88 loc) · 2.98 KB
/
cephfs-snap
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
#!/bin/bash
#
# cephfs-snap - make periodic snapshots of cephfs filesystem
# Brett Kelly 2018 [email protected]
#
# Adapted from btrfs-snap
# Copyright (C) 2010 Birger Monsen [email protected]
#
# This program is distributed under the GNU General Public License
# http://www.gnu.org/licenses/gpl.txt
#
LOG_FACILITY=local0
VERSION="1.0"
prog=${0##*/}
USAGE="Usage: ${prog} -h for usage help
${prog} -V for version
${prog} <mountpoint> <prefix> <count>"
SYNOPSIS="${prog} <mountpoint> <prefix> <count>
<mountpoint> is the mountpoint of the cephfs file system to make a
snapshot of
<prefix> is the prefix to be used in the name of the snapshot.
E.g. hourly, daily, weekly...
<count> The number of snapshots with the given prefix to keep.
cephfs-snap /mnt/server hourly 24
would make a snapshot in /mnt/server/.snap/ called hourly_<date>_<time>
where <date> is on the form YYYY-MM-DD and <time> is on the form
HH:MM:SS. This format makes shure snapshot names sort correctly in
cronological order even when sorted alphabetically. The 24 newest
snapshots matching the prefix are kept around. The rest are deleted.
Snapshots can be created within any subdirectory of a cephfs filseystem, and cover all data in the filesystem under that directory.
Example usage for a system with a cephfs file system mounted as
/mnt/fileshare (remember to make these scripts executable):
/etc/cron.hourly/cephfs-snap
#!/bin/bash
${0} / hourly 24
/etc/cron.daily/cephfs-snap
#!/bin/bash
${0} / daily 7
/etc/cron.weekly/cephfs-snap
#!/bin/bash
${0} / weekly 4
Snspahots are created read-only. To restore a file, just copy it back from
a snapshot to the main branch."
while getopts "hV" arg; do
case "${arg}" in
h )
echo "$SYNOPSIS"
exit 0
;;
V )
echo "${prog} Version ${VERSION}"
exit 0
;;
* )
echo "$USAGE"
exit 1
;;
esac
done
if [ $# -ne 3 ] ; then
echo "$USAGE"
exit 1
fi
mp=$1
pf=$2
cnt=$(( $3+1 ))
if [ ! -d ${mp} ] ; then
echo "Error: ${mp} does not exist"
logger -p ${LOG_FACILITY}.err -t ${prog} "Error: ${mp} does not exist"
exit 1
fi
## Create snapshot
dt=`date +'%Y-%m-%d_%H%M%S'`
out=`mkdir ${mp}/.snap/${pf}_${dt} 2>&1`
if [ $? -eq 0 ] ; then
logger -p ${LOG_FACILITY}.info -t ${prog} "Created cephfs snapshot ${mp}/.snap/${pf}_${dt}"
else
logger -p ${LOG_FACILITY}.err -t ${prog} "Error creating cephfs snapshot ${mp}/.snap/${pf}_${dt}"
fi
## Remove snapshot
ls -dr ${mp}/.snap/${pf}_* | tail -n +${cnt} | while read snap ; do
out=`rmdir ${snap} 2>&1`
if [ $? -eq 0 ] ; then
logger -p ${LOG_FACILITY}.info -t ${prog} "Removed cephfs snapshot: ${snap}"
else
logger -p ${LOG_FACILITY}.err -t ${prog} "Error removing cephfs snapshot: ${snap}"
fi
done