forked from woxxy/MySQL-backup-to-Amazon-S3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongotos3.sh
68 lines (54 loc) · 2.06 KB
/
mongotos3.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
#!/bin/sh
# Updates etc at: https://github.com/woxxy/MySQL-backup-to-Amazon-S3
# Under a MIT license
# change these variables to what you need
#MONGOADMIN=root
#MONGOPASSWORD=password
S3BUCKET=bucketname
FILENAME=filename
DATABASE=''
# the following line prefixes the backups with the defined directory. it must be blank or end with a /
S3PATH=mongo_backup/
# when running via cron, the PATHs MIGHT be different. If you have a custom/manual Mongo install, you should set this manually like MONGODUMPPATH=/usr/bin/mongodump
MONGODUMPPATH=
#tmp path.
TMP_PATH=~/tmp/mongo-backups/
DATESTAMP=$(date +".%m.%d.%Y")
DAY=$(date +"%d")
DAYOFWEEK=$(date +"%A")
PERIOD=${1-day}
if [ ${PERIOD} = "auto" ]; then
if [ ${DAY} = "01" ]; then
PERIOD=month
elif [ ${DAYOFWEEK} = "Sunday" ]; then
PERIOD=week
else
PERIOD=day
fi
fi
echo "Selected period: $PERIOD."
echo "Starting backing up the database to a file..."
# dump all databases
${MONGODUMPPATH}mysqldump --db ${DATABASE} --out ${TMP_PATH}${FILENAME}
echo "Done backing up the database to a folder."
echo "Starting compression..."
tar czf ${TMP_PATH}${FILENAME}${DATESTAMP}.tar.gz ${TMP_PATH}${FILENAME}
echo "Done compressing the backup folder."
# we want at least two backups, two months, two weeks, and two days
echo "Removing old backup (2 ${PERIOD}s ago)..."
s3cmd del --recursive s3://${S3BUCKET}/${S3PATH}previous_${PERIOD}/
echo "Old backup removed."
echo "Moving the backup from past $PERIOD to another folder..."
s3cmd mv --recursive s3://${S3BUCKET}/${S3PATH}${PERIOD}/ s3://${S3BUCKET}/${S3PATH}previous_${PERIOD}/
echo "Past backup moved."
# upload all databases
echo "Uploading the new backup..."
s3cmd put -f ${TMP_PATH}${FILENAME}${DATESTAMP}.tar.gz s3://${S3BUCKET}/${S3PATH}${PERIOD}/
echo "New backup uploaded."
echo "Removing the cache files..."
# remove databases dump
# Uncomment only when you have set TMP_PATH and FILENAME; if these variables are unset, rm -rf can be something bad.
#rm -rf ${TMP_PATH}${FILENAME}
rm ${TMP_PATH}${FILENAME}${DATESTAMP}.tar.gz
echo "Files removed."
echo "All done."