Skip to content
This repository was archived by the owner on Nov 29, 2020. It is now read-only.

Commit c4d7e56

Browse files
committed
remove backups only on when new backup succeeds
1 parent b3114d8 commit c4d7e56

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

run.sh

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#!/bin/bash
22

3-
if [ "${MONGODB_ENV_MONGODB_PASS}" == "**None**" ]; then
4-
unset MONGODB_ENV_MONGODB_PASS
5-
fi
6-
73
MONGODB_HOST=${MONGODB_PORT_27017_TCP_ADDR:-${MONGODB_HOST}}
84
MONGODB_HOST=${MONGODB_PORT_1_27017_TCP_ADDR:-${MONGODB_HOST}}
95
MONGODB_PORT=${MONGODB_PORT_27017_TCP_PORT:-${MONGODB_PORT}}
@@ -17,25 +13,31 @@ MONGODB_PASS=${MONGODB_PASS:-${MONGODB_ENV_MONGODB_PASS}}
1713
[[ ( -n "${MONGODB_PASS}" ) ]] && PASS_STR=" --password ${MONGODB_PASS}"
1814
[[ ( -n "${MONGODB_DB}" ) ]] && USER_STR=" --db ${MONGODB_DB}"
1915

20-
BACKUP_CMD="mongodump --out /backup/"'$(date +\%Y.\%m.\%d.\%H\%M\%S)'" --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR}${DB_STR} ${EXTRA_OPTS}"
21-
16+
BACKUP_CMD="mongodump --out /backup/"'${BACKUP_NAME}'" --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR}${DB_STR} ${EXTRA_OPTS}"
2217

2318
echo "=> Creating backup script"
2419
rm -f /backup.sh
2520
cat <<EOF >> /backup.sh
2621
#!/bin/bash
2722
MAX_BACKUPS=${MAX_BACKUPS}
23+
BACKUP_NAME=\$(date +\%Y.\%m.\%d.\%H\%M\%S)
2824
2925
echo "=> Backup started"
26+
if ${BACKUP_CMD} ;then
27+
echo " Backup succeeded"
28+
else
29+
echo " Backup failed"
30+
rm -rf /backup/\${BACKUP_NAME}
31+
fi
32+
3033
if [ -n "\${MAX_BACKUPS}" ]; then
31-
while [ \$(ls /backup -N1 | wc -l) -ge \${MAX_BACKUPS} ];
34+
while [ \$(ls /backup -N1 | wc -l) -gt \${MAX_BACKUPS} ];
3235
do
3336
BACKUP_TO_BE_DELETED=\$(ls /backup -N1 | sort | head -n 1)
34-
echo "Deleting backup \${BACKUP_TO_BE_DELETED}"
37+
echo " Deleting backup \${BACKUP_TO_BE_DELETED}"
3538
rm -rf /backup/\${BACKUP_TO_BE_DELETED}
3639
done
3740
fi
38-
${BACKUP_CMD}
3941
echo "=> Backup done"
4042
EOF
4143
chmod +x /backup.sh
@@ -45,7 +47,11 @@ rm -f /restore.sh
4547
cat <<EOF >> /restore.sh
4648
#!/bin/bash
4749
echo "=> Restore database from \$1"
48-
mongorestore --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR} \$1
50+
if mongorestore --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR} \$1; then
51+
echo " Restore succeeded"
52+
else
53+
echo " Restore failed"
54+
fi
4955
echo "=> Done"
5056
EOF
5157
chmod +x /restore.sh
@@ -54,12 +60,11 @@ touch /mongo_backup.log
5460
tail -F /mongo_backup.log &
5561

5662
if [ -n "${INIT_BACKUP}" ]; then
57-
echo "=> Create a backup on start the container"
63+
echo "=> Create a backup on the startup"
5864
/backup.sh
5965
fi
6066

6167
echo "${CRON_TIME} /backup.sh >> /mongo_backup.log 2>&1" > /crontab.conf
6268
crontab /crontab.conf
63-
crontab -l
6469
echo "=> Running cron job"
6570
exec cron -f

0 commit comments

Comments
 (0)