Skip to content

Instantly share code, notes, and snippets.

@ryanjohnston
Last active September 25, 2018 23:13
Show Gist options
  • Save ryanjohnston/961c2eabace30985ac700a1c84adbb53 to your computer and use it in GitHub Desktop.
Save ryanjohnston/961c2eabace30985ac700a1c84adbb53 to your computer and use it in GitHub Desktop.
[BASH - Scripts] General Bash Scripts, Snippets and Gists #cli

Collected hacks and tips for the command line.

#!/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}}
[[ ( -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}"
BACKUP_CMD="mongodump --out /backup/"'${BACKUP_NAME}'" --host ${MONGODB_HOST} --port ${MONGODB_PORT} ${USER_STR}${PASS_STR}${DB_STR} ${EXTRA_OPTS}"
echo "=> Creating backup script"
rm -f /backup.sh
cat <<EOF >> /backup.sh
#!/bin/bash
MAX_BACKUPS=${MAX_BACKUPS}
BACKUP_NAME=\$(date +\%Y.\%m.\%d.\%H\%M\%S)
echo "=> Backup started"
if ${BACKUP_CMD} ;then
if zip -r /backup/\${MONGODB_DB}_\${BACKUP_NAME}.zip /backup/\${BACKUP_NAME} ;then
rm -rf /backup/\${BACKUP_NAME}
echo " Backup succeeded"
fi
else
echo " Backup failed"
rm -rf /backup/\${BACKUP_NAME}
fi
if [ -n "\${MAX_BACKUPS}" ]; then
while [ \$(ls /backup -N1 | wc -l) -gt \${MAX_BACKUPS} ];
do
BACKUP_TO_BE_DELETED=\$(ls /backup -N1 | sort | head -n 1)
echo " Deleting backup \${BACKUP_TO_BE_DELETED}"
rm -rf /backup/\${BACKUP_TO_BE_DELETED}
done
fi
echo "=> Backup done"
EOF
chmod +x /backup.sh
touch /mongo_backup.log
tail -F /mongo_backup.log &
if [ -n "${INIT_BACKUP}" ]; then
echo "=> Create a backup on the startup"
/backup.sh
fi
echo "${CRON_TIME} /backup.sh >> /mongo_backup.log 2>&1" > /crontab.conf
crontab /crontab.conf
echo "=> Running cron job"
exec cron -f
IP=$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}')
#!/usr/bin/env bash
#
# Requires npm package, 'http-server'.
# npm install -g http-server
#
http-server -o --cors -c-1 -P .
#!/usr/bin/env bash
#
# pipe tree command via stdout to vim.
#
# Requires tree (brew install tree)
#
tree -dL 2 | vim -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment