-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcephfs-snp
executable file
·97 lines (81 loc) · 3.08 KB
/
cephfs-snp
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
#!/bin/bash
#
# Script that creates cephfs snapshots, manually or from cron
#
# Usage:
# sudo cephfs-snp <dir> (<tag>) (<limit>) (<seconds>)
#
# Copyleft 2020 by Stefano Marinelli <stefano@dragas.it>
# GPL licensed (see end of file) * Use at your own risk!
#
# Based on btrfs-snp by Ignacio Nunez Hernanz
#
# Based on btrfs-snap by Birger Monsen
#
function cephfs-snp()
{
local BIN="${0##*/}"
local DIR="${1}"
local TAG="${2:-snapshot}"
local LIMIT="${3:-0}"
local TIME="${4:-0}"
local DST="${1:-.snap}"
## usage
[[ "$*" == "" ]] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] && {
echo "Usage: $BIN <dir> (<tag>) (<limit>) (<seconds>) (<destdir>)
dir │ create snapshot of <dir>
tag │ name the snapshot <tag>_<timestamp>
limit │ keep <limit> snapshots with this tag. 0 to disable
seconds │ don't create snapshots before <seconds> have passed from last with this tag. 0 to disable
Cron example: Hourly snapshot for one day, daily for one week, weekly for one month, and monthly for one year.
cat > /etc/cron.hourly/$BIN <<EOF
#!/bin/bash
/usr/local/sbin/$BIN /home hourly 24 3600
/usr/local/sbin/$BIN /home daily 7 86400
/usr/local/sbin/$BIN /home weekly 4 604800
/usr/local/sbin/$BIN / weekly 4 604800
/usr/local/sbin/$BIN /home monthly 12 2592000
EOF
chmod +x /etc/cron.hourly/$BIN"
return 0
}
## checks
local SNAPSHOT=${TAG}_$( date +%F_%H%M%S )
[[ ${EUID} -ne 0 ]] && { echo "Must be run as root. Try 'sudo $BIN'"; return 1; }
[[ -d "$SNAPSHOT" ]] && { echo "$SNAPSHOT already exists" ; return 1; }
local SNAPS=( $( ls -d "$DST/.snap/${TAG}_"* 2>/dev/null ) )
## check time of the last snapshot for this tag
[[ "$TIME" != 0 ]] && [[ "${#SNAPS[@]}" != 0 ]] && {
local LATEST=$( sed -r "s|.*_(.*_.*)|\\1|;s|_([0-9]{2})([0-9]{2})([0-9]{2})| \\1:\\2:\\3|" <<< "${SNAPS[-1]}" )
LATEST=$( date +%s -d "$LATEST" ) || return 1
[[ $(( LATEST + TIME )) -gt $( date +%s ) ]] && { echo "No new snapshot needed for $TAG"; return 0; }
}
## do it
mkdir "$DIR/.snap/$SNAPSHOT" || return 1
## prune older backups
[[ "$LIMIT" != 0 ]] && \
[[ ${#SNAPS[@]} -ge $LIMIT ]] && \
echo "Pruning old snapshots..." && \
for (( i=0; i <= $(( ${#SNAPS[@]} - LIMIT )); i++ )); do
echo Delete "${SNAPS[$i]}"
rmdir "${SNAPS[$i]}"
done
echo "snapshot $SNAPSHOT generated"
}
cephfs-snp "$@"
# License
#
# This script is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA