Created
January 10, 2017 15:59
-
-
Save srokatonie/5a34f144b6e3c15fb79849239cd28820 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# run cron job to backup 'test' database to '[email protected]' at 01:00 every day | |
# 0 01 * * * bash /home/maciek/scripts/backup_email.sh test [email protected] | |
DB=$1 | |
EMAIL_TO=$2 | |
MAX_FILE_SIZE=50000 #Mailgun email size <= 50MB | |
BACKUP_DIR="/var/backups/mongobackups" | |
MAILGUN_API_KEY="key-1234...." | |
MAILGUN_DOMAIN="emaildomain.com" | |
echo "Creating Mongo dump for '$DB' at $BACKUP_DIR" | |
mongodump --db $DB --out $BACKUP_DIR | |
echo "Compressing $BACKUP_DIR/$DB" | |
tar -zcvf $BACKUP_DIR/$DB.tar.gz $BACKUP_DIR/$DB | |
echo "Sending email to $EMAIL_TO" | |
FILE=$BACKUP_DIR/$DB.tar.gz | |
FILE_SIZE=$(wc -c <"$FILE") | |
# FILE is larger than MAX_FILE_SIZE | |
if [ $FILE_SIZE -gt $MAX_FILE_SIZE ]; then | |
# Copy to a filename that has today's date | |
DATE=`date +%Y-%m-%d` | |
cp $BACKUP_DIR/$DB.tar.gz $BACKUP_DIR/$DB-$DATE.tar.gz | |
SUBJECT="File to large - $DB backup" | |
MESSAGE="File size to large to attach. Check $BACKUP_DIR on server for a backup file" | |
curl -s --user "api:$MAILGUN_API_KEY" \ | |
https://api.mailgun.net/v3/$MAILGUN_DOMAIN/messages \ | |
-F from="Backups <backups@$MAILGUN_DOMAIN>" \ | |
-F to="$EMAIL_TO" \ | |
-F subject="$SUBJECT" \ | |
-F text="$MESSAGE" | |
else | |
SUBJECT="$DB backup" | |
MESSAGE="Backup of $DB" | |
curl -s --user "api:$MAILGUN_API_KEY" \ | |
https://api.mailgun.net/v3/$MAILGUN_DOMAIN/messages \ | |
-F from="Backups <backups@$MAILGUN_DOMAIN>" \ | |
-F to="$EMAIL_TO" \ | |
-F subject="$SUBJECT" \ | |
-F text="$MESSAGE" \ | |
-F attachment=@$FILE | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment