forked from halvves/mongodb-backup-s3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.sh
executable file
·114 lines (92 loc) · 3.58 KB
/
run.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
112
113
114
#!/bin/bash
MONGODB_HOST=${MONGODB_PORT_27017_TCP_ADDR:-${MONGODB_HOST}}
MONGODB_HOST=${MONGODB_PORT_1_27017_TCP_ADDR:-${MONGODB_HOST}}
MONGODB_PORT=${MONGODB_PORT_27017_TCP_PORT:-${MONGODB_PORT}}
MONGODB_PORT=${MONGODB_PORT_1_27017_TCP_PORT:-${MONGODB_PORT}}
MONGODB_USER=${MONGODB_USER:-${MONGODB_ENV_MONGODB_USER}}
MONGODB_PASS=${MONGODB_PASS:-${MONGODB_ENV_MONGODB_PASS}}
S3PATH="s3://$BUCKET/$BACKUP_FOLDER"
[[ ( -z "${MONGODB_USER}" ) && ( -n "${MONGODB_PASS}" ) ]] && MONGODB_USER='admin'
[[ ( -n "${MONGODB_USER}" ) ]] && USER_STR=" --username ${MONGODB_USER}"
[[ ( -n "${MONGODB_PASS}" ) ]] && PASS_STR=" --password '${MONGODB_PASS}'"
[[ ( -n "${MONGODB_DB}" ) ]] && DB_STR=" --db ${MONGODB_DB}"
MONGO_DUMP_COMMAND="mongodump --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR}${DB_STR} --archive=\${BACKUP_NAME} --gzip ${EXTRA_OPTS}"
[[ ( -n "${MONGODB_URI}" ) ]] && MONGO_DUMP_COMMAND="mongodump --uri='${MONGODB_URI}' --archive=\${BACKUP_NAME} --gzip ${EXTRA_OPTS}"
MONGO_RESTORE_COMMAND="mongorestore --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR}${DB_STR} --drop --gzip ${EXTRA_OPTS} --archive=\${RESTORE_ME}"
[[ ( -n "${MONGODB_URI}" ) ]] && MONGO_RESTORE_COMMAND="mongorestore --uri='${MONGODB_URI}' --drop --gzip ${EXTRA_OPTS} --archive=\${RESTORE_ME}"
# Export AWS Credentials into env file for cron job
printenv | sed 's/^\([a-zA-Z0-9_]*\)=\(.*\)$/export \1="\2"/g' | grep -E "^export AWS" > /root/project_env.sh
if [ -n "${MONGODB_URI}" ]; then
echo "=> Create a backup on the startup"
/backup.sh
fi
echo "=> Creating backup script"
rm -f /backup.sh
cat <<EOF >> /backup.sh
#!/bin/bash
TIMESTAMP=\`/bin/date +"%Y%m%dT%H%M%S"\`
BACKUP_NAME=\${TIMESTAMP}.dump.gz
S3BACKUP=${S3PATH}\${BACKUP_NAME}
S3LATEST=${S3PATH}latest.dump.gz
#Configure AWS S3 to use Signature Version 4
aws configure set default.s3.signature_version s3v4
echo "=> Backup started"
if ${MONGO_DUMP_COMMAND} && aws s3 cp \${BACKUP_NAME} \${S3BACKUP} && aws s3 cp \${S3BACKUP} \${S3LATEST} && rm \${BACKUP_NAME} ;then
echo " > Backup succeeded"
else
echo " > Backup failed"
fi
echo "=> Done"
EOF
chmod +x /backup.sh
echo "=> Backup script created"
echo "=> Creating restore script"
rm -f /restore.sh
cat <<EOF >> /restore.sh
#!/bin/bash
if [[( -n "\${1}" )]];then
RESTORE_ME=\${1}.dump.gz
else
RESTORE_ME=latest.dump.gz
fi
S3RESTORE=${S3PATH}\${RESTORE_ME}
#Configure AWS S3 to use Signature Version 4
aws configure set default.s3.signature_version s3v4
echo "=> Restore database from \${RESTORE_ME}"
if aws s3 cp \${S3RESTORE} \${RESTORE_ME} && ${MONGO_RESTORE_COMMAND} && rm \${RESTORE_ME}; then
echo " Restore succeeded"
else
echo " Restore failed"
fi
echo "=> Done"
EOF
chmod +x /restore.sh
echo "=> Restore script created"
echo "=> Creating list script"
rm -f /listbackups.sh
cat <<EOF >> /listbackups.sh
#!/bin/bash
#Configure AWS S3 to use Signature Version 4
aws configure set default.s3.signature_version s3v4
aws s3 ls ${S3PATH}
EOF
chmod +x /listbackups.sh
echo "=> List script created"
ln -s /restore.sh /usr/bin/restore
ln -s /backup.sh /usr/bin/backup
ln -s /listbackups.sh /usr/bin/listbackups
touch /mongo_backup.log
if [ -n "${INIT_BACKUP}" ]; then
echo "=> Create a backup on the startup"
/backup.sh
fi
if [ -n "${INIT_RESTORE}" ]; then
echo "=> Restore store from lastest backup on startup"
/restore.sh
fi
if [ -z "${DISABLE_CRON}" ]; then
echo "${CRON_TIME} . /root/project_env.sh; /backup.sh >> /mongo_backup.log 2>&1" > /crontab.conf
crontab /crontab.conf
echo "=> Running cron job"
cron && tail -f /mongo_backup.log
fi