-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-bucket.sh
executable file
·114 lines (86 loc) · 1.76 KB
/
backup-bucket.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
#!/bin/bash
#
# Backs up Splunk buckets to a specified location.
#
# Errors are fatal
set -e
#
# Syntax check.
#
if test ! "$2"
then
echo "! "
echo "! Syntax: $0 source_dir target_dir"
echo "! "
echo "! Backs up all buckets in the source directory to the target directory."
echo "! The target directory will be created if it does not exist."
echo "! "
exit 1
fi
SOURCE=$1
TARGET=$2
if test ! -d $SOURCE
then
echo "! $0: Source directory '$SOURCE' does not exist!"
exit 1
fi
FIRST_CHAR=${TARGET:0:1}
if test "$FIRST_CHAR" != "/"
then
echo "! "
echo "! $0: The target ${TARGET} should be an absolute path! "
echo "! If in doubt, prepend with \$PWD :-)"
echo "! "
exit 1
fi
#
# Go to our source directory
#
cd $SOURCE
#
# Now find all buckets under it
#
for DIR in $(find . -type d -name db_\*)
do
#
# What directory are we backing up?
#
DIR2=$(basename $DIR)
#
# Make sure our target directory exists and set our target filename
#
mkdir -p "$TARGET"
TARGET_TGZ="${TARGET}/${HOSTNAME}-${DIR2}.tgz"
#
# Go into the parent directory and then tar up the child directory
#
pushd $DIR/.. > /dev/null
TARGET_SANITIZED=$(echo $TARGET_TGZ | sed -e "s/[^A-Za-z0-9]/_/g")
TOUCH_FILE="${DIR2}/.backedup-${TARGET_SANITIZED}"
if test -f $TOUCH_FILE
then
echo "# "
echo "# The directory $DIR2 has already been backed up to '${TARGET_TGZ}'! Skipping..."
echo "# "
else
#
# Now tar up the bucket
#
echo "# "
echo "# Tarring up directory ${DIR2} to ${TARGET_TGZ}..."
echo "# "
tar cfz "$TARGET_TGZ" $DIR2
touch $TOUCH_FILE
echo "# "
echo "# Done!"
echo "# "
fi
#
# Return to the previous directory
#
popd >/dev/null
done
#
# Change ownership to Ubuntu so that BitTorrent Sync can replicate it.
#
chown -R ubuntu:ubuntu "$TARGET"