forked from RGBboy/mongodb-s3-backup
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrestore.sh
108 lines (92 loc) · 2.13 KB
/
restore.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
#!/bin/bash
#
# To Do - Add logging of output.
# To Do - Abstract bucket region to options
set -e
export PATH="$PATH:/usr/local/bin"
usage()
{
cat << EOF
usage: $0 options
This script retrieves the latest mongo dumpfile from Amazon S3,
and restores it to specified database.
OPTIONS:
-help Show this message
# -u OPTIONAL: Mongodb user
# -p OPTIONAL: Mongodb password
-o OPTIONAL: Default is "localhost:27017"
Mongodb host <hostname><:port>
-f Mongodb database(from)
-t OPTIONAL: Default is same as -t options value
Mongodb database(to)
-b Amazon S3 bucket name
EOF
}
# MONGODB_USER=
# MONGODB_PASSWORD=
MONGODB_HOST=
MONGODB_DATABASE_FROM=
MONGODB_DATABASE_TO=
S3_BUCKET=
while getopts “h:u:p:o:f:t:b:” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
# u)
# MONGODB_USER=$OPTARG
# ;;
# p)
# MONGODB_PASSWORD=$OPTARG
# ;;
o)
MONGODB_HOST=$OPTARG
;;
f)
MONGODB_DATABASE_FROM=$OPTARG
;;
t)
MONGODB_DATABASE_TO=$OPTARG
;;
b)
S3_BUCKET=$OPTARG
;;
?)
usage
exit
;;
esac
done
if [[ -z $S3_BUCKET ]] || [[ -z $MONGODB_DATABASE_FROM ]]
then
usage
exit 1
fi
if [[ -z $MONGODB_HOST ]]
then
MONGODB_HOST="localhost:27017"
fi
if [[ -z $MONGODB_DATABASE_TO ]]
then
MONGODB_DATABASE_TO=$MONGODB_DATABASE_FROM
fi
# Get the directory the script is being run from
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $DIR
OUT_DIR=$DIR/restore
DATE_YYYY=$(date -u "+%Y")
DATE_YYYYMM=$(date -u "+%Y-%m")
LATEST_FILE_PATH=$(s3cmd ls s3://$S3_BUCKET/$DATE_YYYY/$DATE_YYYYMM/ | sort -r | head -1 | awk '{print $4}')
echo $LATEST_FILE_PATH
s3cmd get $LATEST_FILE_PATH $OUT_DIR/
TAR_FILE_NAME="$( ls -rt $OUT_DIR | tail -1 )"
# Untar Gzip the file
tar zxvf $OUT_DIR/$TAR_FILE_NAME -C $OUT_DIR
# Remove the tar file
rm $OUT_DIR/$TAR_FILE_NAME
# restore
DUMP_DIR="$( ls -rt $OUT_DIR | tail -1 )"
echo $OUT_DIR/$DUMP_DIR/$MONGODB_DATABASE_FROM
mongorestore --host $MONGODB_HOST --db $MONGODB_DATABASE_TO --drop $OUT_DIR/$DUMP_DIR/$MONGODB_DATABASE_FROM