|
Lines 3-9
Link Here
|
| 3 |
TARSNAP_CMD=/usr/local/bin/tarsnap |
3 |
TARSNAP_CMD=/usr/local/bin/tarsnap |
| 4 |
|
4 |
|
| 5 |
path2name() { |
5 |
path2name() { |
| 6 |
res=$(echo $1 | sed -E -e 's,/+,/,g' -e 's,/$|^/,,g' -e 's,/,-,g') |
6 |
res=$(echo "$1" | sed -E -e 's,/+,/,g' -e 's,/$|^/,,g' -e 's,/,-,g') |
| 7 |
if [ -z "$res" ]; then |
7 |
if [ -z "$res" ]; then |
| 8 |
res='ROOT' |
8 |
res='ROOT' |
| 9 |
fi |
9 |
fi |
|
Lines 36-48
create_backup()
Link Here
|
| 36 |
exit 1 |
36 |
exit 1 |
| 37 |
esac |
37 |
esac |
| 38 |
|
38 |
|
| 39 |
fullname="$(path2name $path)-$now" |
39 |
fullname="$(path2name "$path")-$now" |
| 40 |
# look for an existing backup with this name |
40 |
# look for an existing backup with this name |
| 41 |
if $TARSNAP_CMD --list-archives | grep -q "^${fullname}$"; then |
41 |
if $TARSNAP_CMD --list-archives | grep -q "^${fullname}$"; then |
| 42 |
echo "* backup $fullname already exists, skipping..." |
42 |
echo "* backup $fullname already exists, skipping..." |
| 43 |
else |
43 |
else |
| 44 |
echo " * making backup: $fullname" |
44 |
echo " * making backup: $fullname" |
| 45 |
$TARSNAP_CMD -c -f $fullname $path || something_wrong=1 |
45 |
$TARSNAP_CMD -c -f "$fullname" "$path" || something_wrong=1 |
| 46 |
fi |
46 |
fi |
| 47 |
} |
47 |
} |
| 48 |
|
48 |
|
|
Lines 51-57
delete_backup()
Link Here
|
| 51 |
{ |
51 |
{ |
| 52 |
backup=$1 |
52 |
backup=$1 |
| 53 |
echo " * deleting old backup: $backup" |
53 |
echo " * deleting old backup: $backup" |
| 54 |
$TARSNAP_CMD -d -f $backup |
54 |
$TARSNAP_CMD -d -f "$backup" |
| 55 |
} |
55 |
} |
| 56 |
|
56 |
|
| 57 |
# make a $type backup of path, cleaning up old ones |
57 |
# make a $type backup of path, cleaning up old ones |
|
Lines 61-67
do_path()
Link Here
|
| 61 |
keep=$2 |
61 |
keep=$2 |
| 62 |
type=$3 |
62 |
type=$3 |
| 63 |
|
63 |
|
| 64 |
name="`path2name $path`" |
64 |
name="$(path2name "$path")" |
| 65 |
|
65 |
|
| 66 |
# create the regex matching the type of backup we're currently working on |
66 |
# create the regex matching the type of backup we're currently working on |
| 67 |
case "$type" in |
67 |
case "$type" in |
|
Lines 86-108
do_path()
Link Here
|
| 86 |
exit 1 |
86 |
exit 1 |
| 87 |
esac |
87 |
esac |
| 88 |
|
88 |
|
| 89 |
create_backup $path $type |
89 |
create_backup "$path" "$type" |
| 90 |
|
90 |
|
| 91 |
# get a list of all of the backups of this type sorted alpha, which |
91 |
# get a list of all of the backups of this type sorted alpha, which |
| 92 |
# effectively is increasing date/time |
92 |
# effectively is increasing date/time |
| 93 |
backups=$($TARSNAP_CMD --list-archives | grep $regex | sort) |
93 |
backups=$($TARSNAP_CMD --list-archives | grep "$regex" | sort) |
| 94 |
count=$(echo $backups | wc -l) |
94 |
count=$(echo "$backups" | wc -l) |
| 95 |
if [ $count -ge 0 ]; then |
95 |
if [ "$count" -ge 0 ]; then |
| 96 |
# how many items should we delete |
96 |
# how many items should we delete |
| 97 |
delete=$(expr $count - $keep) |
97 |
delete=$((count - keep)) |
| 98 |
count=0 |
98 |
count=0 |
| 99 |
# walk through the backups, deleting them until we've trimmed deleted |
99 |
# walk through the backups, deleting them until we've trimmed deleted |
| 100 |
for backup in $backups; do |
100 |
for backup in $backups; do |
| 101 |
if [ $count -ge $delete ]; then |
101 |
if [ $count -ge $delete ]; then |
| 102 |
break |
102 |
break |
| 103 |
fi |
103 |
fi |
| 104 |
delete_backup $backup |
104 |
delete_backup "$backup" |
| 105 |
count=$(expr $count + 1) |
105 |
count=$((count + 1)) |
| 106 |
done |
106 |
done |
| 107 |
fi |
107 |
fi |
| 108 |
} |
108 |
} |
|
Lines 117-125
do_backups()
Link Here
|
| 117 |
echo "" |
117 |
echo "" |
| 118 |
echo "Doing tarsnap $type backups:" |
118 |
echo "Doing tarsnap $type backups:" |
| 119 |
for path in $paths; do |
119 |
for path in $paths; do |
| 120 |
do_path $path $keep $type |
120 |
do_path "$path" "$keep" "$type" |
| 121 |
done |
121 |
done |
| 122 |
if [ -n something_wrong ]; then |
122 |
if [ -n "$something_wrong" ]; then |
| 123 |
exit 3 |
123 |
exit 3 |
| 124 |
fi |
124 |
fi |
| 125 |
} |
125 |
} |
|
Lines 128-131
if [ $# -ne 3 ]; then
Link Here
|
| 128 |
echo "Usage: $0 \"<paths>\" <keep> daily|weekly|monthly|yearly" |
128 |
echo "Usage: $0 \"<paths>\" <keep> daily|weekly|monthly|yearly" |
| 129 |
exit 2 |
129 |
exit 2 |
| 130 |
fi |
130 |
fi |
| 131 |
do_backups "$1" $2 $3 |
131 |
do_backups "$1" "$2" "$3" |