Skip to content

Instantly share code, notes, and snippets.

@fatchan
Created March 8, 2019 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fatchan/095aff5a7aac67caa6878e381e53544c to your computer and use it in GitHub Desktop.
Save fatchan/095aff5a7aac67caa6878e381e53544c to your computer and use it in GitHub Desktop.
#!/bin/bash
#mongo configs
APP_NAME="xxxxxx"
MONGO_DATABASE="xxxxxx"
MONGO_HOST="127.0.0.1"
MONGO_PORT="27017"
MONGO_USERNAME="xxxx"
MONGO_PASSWORD="xxxxx"
#backup configs
TIMESTAMP=`date +%F-%H%M`
BACKUPS_DIR="/home/$USER/mongo-backups/$APP_NAME"
BACKUP_NAME="$APP_NAME-$TIMESTAMP.gz"
ARCHIVE_PATH="$BACKUPS_DIR/$BACKUP_NAME"
#ftp configs
FTP_HOST="xxxxxxx"
FTP_USER="xxxxxxxxxx"
FTP_PASSWD="xxxxxxx"
#dump the DB to archive
mkdir -p $BACKUPS_DIR
mongodump --username $MONGO_USERNAME --password $MONGO_PASSWORD --authenticationDatabase $MONGO_DATABASE --db $MONGO_DATABASE --archive=$ARCHIVE_PATH --gzip
#upload to FTP backup
ftp -n $FTP_HOST <<END_SCRIPT
quote USER $FTP_USER
quote PASS $FTP_PASSWD
binary
lcd $BACKUPS_DIR
put $BACKUP_NAME
quit
END_SCRIPT
#delete older than 30 days local backups
find $ARCHIVE_PATH -type f -name '*.gz' -mtime +30 -exec rm -f {} \;
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment