Skip to content

Instantly share code, notes, and snippets.

@pulpfree
Forked from eladnava/mongodb-s3-backup.sh
Created July 26, 2017 03:26
Show Gist options
  • Select an option

  • Save pulpfree/c942376ab232abb2f6a1ce6b0d1a1e9a to your computer and use it in GitHub Desktop.

Select an option

Save pulpfree/c942376ab232abb2f6a1ce6b0d1a1e9a to your computer and use it in GitHub Desktop.

Revisions

  1. @eladnava eladnava revised this gist Jun 10, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongodb-s3-backup.sh
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    # 8) Set up a daily backup at midnight via `crontab -e`:
    # 0 0 * * * /home/ubuntu/backup.sh > /home/ubuntu/backup.log

    # DB host (secondary)
    # DB host (secondary preferred as to avoid impacting primary performance)
    HOST=db.example.com

    # DB name
  2. @eladnava eladnava revised this gist Jun 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongodb-s3-backup.sh
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ echo "Backing up $HOST/$DBNAME to s3://$BUCKET/ on $TIME";
    /usr/bin/mongodump -h $HOST -d $DBNAME -o $DEST

    # Create tar of backup directory
    /bin/tar cvf $TAR $DEST
    /bin/tar cvf $TAR -C $DEST .

    # Upload tar to s3
    /usr/bin/aws s3 cp $TAR s3://$BUCKET/
  3. @eladnava eladnava revised this gist Jun 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mongodb-s3-backup.sh
    Original file line number Diff line number Diff line change
    @@ -54,4 +54,4 @@ echo "Backing up $HOST/$DBNAME to s3://$BUCKET/ on $TIME";
    /bin/rm -rf $DEST

    # All done
    echo "Backup available at https://s3.amazonaws.com/$BUCKET/$TAR"
    echo "Backup available at https://s3.amazonaws.com/$BUCKET/$TIME.tar"
  4. @eladnava eladnava revised this gist Jun 7, 2016. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions mongodb-s3-backup.sh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    # 2) Run sudo apt-get install awscli to install the AWSCLI
    # 3) Run aws configure (enter s3-authorized IAM user and specify region)
    # 4) Fill in DB host + name
    # 5) Create S3 bucket and fill it in below
    # 5) Create S3 bucket for the backups and fill it in below (set a lifecycle rule to expire files older than X days in the bucket)
    # 6) Run chmod +x backup.sh
    # 7) Test it out via ./backup.sh
    # 8) Set up a daily backup at midnight via `crontab -e`:
    @@ -35,6 +35,9 @@ TAR=$DEST/../$TIME.tar
    # Create backup dir (-p to avoid warning if already exists)
    /bin/mkdir -p $DEST

    # Log
    echo "Backing up $HOST/$DBNAME to s3://$BUCKET/ on $TIME";

    # Dump from mongodb host into backup directory
    /usr/bin/mongodump -h $HOST -d $DBNAME -o $DEST

    @@ -48,4 +51,7 @@ TAR=$DEST/../$TIME.tar
    /bin/rm -f $TAR

    # Remove backup directory
    /bin/rm -rf $DEST
    /bin/rm -rf $DEST

    # All done
    echo "Backup available at https://s3.amazonaws.com/$BUCKET/$TAR"
  5. @eladnava eladnava revised this gist Jun 7, 2016. 1 changed file with 8 additions and 7 deletions.
    15 changes: 8 additions & 7 deletions mongodb-s3-backup.sh
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,14 @@
    #!/bin/sh

    # Make sure to:
    # 1) Run sudo apt-get install awscli to install the AWSCLI
    # 2) Run aws configure (enter s3-authorized IAM user and specify region)
    # 3) Fill in DB host + name
    # 4) Create S3 bucket and fill it in below
    # 5) Run chmod +x backup.sh
    # 6) Test it out via ./backup.sh
    # 7) Set up a daily backup at midnight via `crontab -e`:
    # 1) Name this file `backup.sh` and place it in /home/ubuntu
    # 2) Run sudo apt-get install awscli to install the AWSCLI
    # 3) Run aws configure (enter s3-authorized IAM user and specify region)
    # 4) Fill in DB host + name
    # 5) Create S3 bucket and fill it in below
    # 6) Run chmod +x backup.sh
    # 7) Test it out via ./backup.sh
    # 8) Set up a daily backup at midnight via `crontab -e`:
    # 0 0 * * * /home/ubuntu/backup.sh > /home/ubuntu/backup.log

    # DB host (secondary)
  6. @eladnava eladnava revised this gist Jun 7, 2016. No changes.
  7. @eladnava eladnava created this gist Jun 7, 2016.
    50 changes: 50 additions & 0 deletions mongodb-s3-backup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    #!/bin/sh

    # Make sure to:
    # 1) Run sudo apt-get install awscli to install the AWSCLI
    # 2) Run aws configure (enter s3-authorized IAM user and specify region)
    # 3) Fill in DB host + name
    # 4) Create S3 bucket and fill it in below
    # 5) Run chmod +x backup.sh
    # 6) Test it out via ./backup.sh
    # 7) Set up a daily backup at midnight via `crontab -e`:
    # 0 0 * * * /home/ubuntu/backup.sh > /home/ubuntu/backup.log

    # DB host (secondary)
    HOST=db.example.com

    # DB name
    DBNAME=my-db

    # S3 bucket name
    BUCKET=s3-bucket-name

    # Linux user account
    USER=ubuntu

    # Current time
    TIME=`/bin/date +%d-%m-%Y-%T`

    # Backup directory
    DEST=/home/$USER/tmp

    # Tar file of backup directory
    TAR=$DEST/../$TIME.tar

    # Create backup dir (-p to avoid warning if already exists)
    /bin/mkdir -p $DEST

    # Dump from mongodb host into backup directory
    /usr/bin/mongodump -h $HOST -d $DBNAME -o $DEST

    # Create tar of backup directory
    /bin/tar cvf $TAR $DEST

    # Upload tar to s3
    /usr/bin/aws s3 cp $TAR s3://$BUCKET/

    # Remove tar file locally
    /bin/rm -f $TAR

    # Remove backup directory
    /bin/rm -rf $DEST