MongoDB backup plan

前端 未结 4 840
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-30 15:10

I want to switch from MySQL to MongoDB but great data losses (more than 1 hour) are not acceptable for me.

I need to have 3 backup plans:

  1. Hourly

4条回答
  •  星月不相逢
    2021-01-30 15:11

    Try this backup script if you want to create a backup from slave mongodb database to S3.

    1. DB host (secondary preferred as to avoid impacting primary performance)

      HOST='SomeHost/mongodbtest-slave'

    2. DB name DBNAME=***

    3. S3 bucket name BUCKET=*-backup

    4. Linux user account USER=ubuntu

    5. Current time TIME=/bin/date +%d-%m-%Y-%T

    6. Password PASSWORD=somePassword#!2*1

    7. Username USERNAME=someUsername

    8. Backup directory DEST=/home/ubuntu/tmp

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

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

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

    12. Dump from mongodb host into backup directory

    mongodump --port 27017 -d DBNAME -u USERNAME -p PASSWORD -o $DEST

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

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

    3. Remove tar file locally /bin/rm -f $TAR

    4. Remove backup directory /bin/rm -rf $DEST

    All done echo "Backup available at https://s3.amazonaws.com/$BUCKET/$TIME.tar

    You can use the steps above put them in a shell executable file and execute this at any interval using crontab commands.

提交回复
热议问题