backup.sh 663 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/bin/bash
  2. set -e
  3. : ${S3_BUCKET:?}
  4. : ${AWS_ACCESS_KEY_ID:?}
  5. : ${AWS_SECRET_ACCESS_KEY:?}
  6. : ${DATE_FORMAT:?}
  7. : ${FILE_PREFIX:?}
  8. : ${BACKUP_SOURCE}
  9. FOLDER=/backup
  10. FILE_NAME=${FILE_PREFIX}$(date -u +${DATE_FORMAT}).tar.gz
  11. echo "Creating backup folder..."
  12. rm -fr ${FOLDER} && mkdir -p ${FOLDER} && cd ${FOLDER}
  13. echo "Starting backup..."
  14. cp -rf /data/${BACKUP_SOURCE} ${BACKUP_SOURCE}
  15. echo "Compressing backup..."
  16. tar -zcvf ${FILE_NAME} ${BACKUP_SOURCE} && rm -fr ${BACKUP_SOURCE}
  17. echo "Uploading to S3..."
  18. aws s3api put-object --bucket ${S3_BUCKET} --key ${FILE_NAME} --body ${FILE_NAME}
  19. echo "Removing backup file..."
  20. rm -f ${FILE_NAME}
  21. echo "Done!"