-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongo_backup
executable file
·136 lines (114 loc) · 4.48 KB
/
mongo_backup
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
# ------------------------------------------------------------------
# [hhslepicka] mongo_backup
# Simple Backup Script for MongoDB
# https://github.com/NSLS-II-LIX/mongobak
# ------------------------------------------------------------------
# Definitions
MONGO_HOST=`hostname`
MONGO_PORT="27017"
MONGODUMP_PATH=`which mongodump`
DUMP_PATH='/backup/mongo_backup'
# Timestamp for filename
TIMESTAMP=`date +%F--%H-%M-%S`
# Save the current working dir
CURR_DIR=$PWD
# Ensure the dump dir exists or exit with error
if [ -z $DUMP_PATH ]; then
echo "ERROR!!! mongo DUMP_PATH can not be an empty string! exiting mongo_backup!"
exit -1
fi
if [ ! -d $DUMP_PATH ]; then
echo "WARNING!!! mongo dump dir ($DUMP_PATH) doesn't exist! attempting to create..."
if ! mkdir -p $DUMP_PATH; then
echo "ERROR!!! mongo dump dir ($DUMP_PATH) doesn't exist and couldn't be created! exiting mongo_backup!"
exit -1
fi
fi
# Switch to the dump dir
if ! cd $DUMP_PATH; then
echo "ERROR!!! failed to cd to mongo dump dir ($DUMP_PATH)!!! exiting mongo_backup!"
exit -1
fi
latest_backup=`ls -drt */ 2>/dev/null | tail -1`
latest_backup=${latest_backup%?}
# Ensure to not overwrite one existing dump by incrementing the number at the end
backup_folder=$TIMESTAMP
if [[ -d ${DUMP_PATH}/${backup_folder} ]] ; then
i=0
while [[ -d ${DUMP_PATH}/${backup_folder}-$i ]] ; do
let i++
done
backup_folder=$backup_folder-$i
fi
if [ -z $backup_folder ]; then
echo "ERROR!!! mongo backup_folder can not be an empty string! exiting mongo_backup!"
exit -1
fi
echo "Running Mongo Dump Command"
# Dump Command
$MONGODUMP_PATH --host $MONGO_HOST:$MONGO_PORT --out ${DUMP_PATH}/$backup_folder
# If we have a previous backup we need to check if the new one differs from it
if [ ! -z "$latest_backup" ] ; then
echo "Comparing with previous backup at: $latest_backup"
# Check if the current backup is equal to the previous one
equals=false
for i in `find -L $latest_backup -type f -name "*"`
do
result=$((cmp $i ${i//$latest_backup/$backup_folder}) 2>&1)
if [[ ! -z "$result" ]]; then
equals=false
break
fi
done
# If the result is equal we just need to create
# a link for the previous backup folder
if [ "$equals" = true ] ; then
echo "The new backup is equals to the previous one"
echo "Removing the new backup"
rm -rf ${DUMP_PATH}/${backup_folder}
echo "Creating a link for the previous backup: $latest_backup"
ln -s ${latest_backup} ${DUMP_PATH}/${backup_folder}
else
echo "The new backup is different from the previous one"
# Check if latest_backup is a link
link="`readlink $latest_backup`"
# if it is a link...
if [ ! -z "$link" ] ; then
# Create the file in which we will store the link relations
touch ${DUMP_PATH}/map.txt
# Store the first link relation
echo "$latest_backup#$link" | cat - map.txt > ${DUMP_PATH}/map_temp && mv ${DUMP_PATH}/map{_temp,.txt}
# Keep storing the links relation until the real directory
while [ ! -z "$link" ] ; do
aux="`readlink $link`"
echo "$link#$aux" | cat - map.txt > map_temp && mv map_temp map.txt
link=$aux
done
# Read the map file line by line
while IFS='' read -r line || [[ -n "$line" ]]; do
arr=(${line//#/ })
# If it is the real directory we need to compress it
if [ -z "${arr[1]}" ] ; then
tar -czvf ${DUMP_PATH}/${arr[0]}.tar.gz ${arr[0]}
rm -rf ${DUMP_PATH}/${arr[0]}
else
# Not the real directory, just create the
# proper links for the tar.gz files
ln -s ${arr[1]}.tar.gz ${DUMP_PATH}/${arr[0]}.tar.gz
rm ${DUMP_PATH}/${arr[0]} 2> /dev/null
rm ${DUMP_PATH}/${arr[1]} 2> /dev/null
fi
done < "map.txt"
# Remove the map file
rm ${DUMP_DIR}/map.txt
else
echo "Compressing the old backup"
tar -czvf ${DUMP_PATH}/${latest_backup}.tar.gz ${latest_backup}
echo "Removing the old backup folder"
rm -rf ${DUMP_PATH}/${latest_backup}
fi
fi
fi
# CD back to the previous working dir
cd $CURR_DIR