You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.4 KiB

  1. #! /bin/sh
  2. set -e
  3. if [ "${DATA_PATH}" == "**None**" ]; then
  4. echo "Warning: You did not set the DATA_PATH environment variable."
  5. fi
  6. if [ "${S3_ACCESS_KEY_ID}" == "**None**" ]; then
  7. echo "Warning: You did not set the S3_ACCESS_KEY_ID environment variable."
  8. fi
  9. if [ "${S3_SECRET_ACCESS_KEY}" == "**None**" ]; then
  10. echo "Warning: You did not set the S3_SECRET_ACCESS_KEY environment variable."
  11. fi
  12. if [ "${S3_BUCKET}" == "**None**" ]; then
  13. echo "You need to set the S3_BUCKET environment variable."
  14. exit 1
  15. fi
  16. if [ "${S3_IAMROLE}" != "true" ]; then
  17. # env vars needed for aws tools - only if an IAM role is not used
  18. export AWS_ACCESS_KEY_ID=$S3_ACCESS_KEY_ID
  19. export AWS_SECRET_ACCESS_KEY=$S3_SECRET_ACCESS_KEY
  20. export AWS_DEFAULT_REGION=$S3_REGION
  21. fi
  22. DUMP_START_TIME=$(date +"%Y-%m-%dT%H%M%SZ")
  23. copy_s3 () {
  24. SRC_FILE=$1
  25. DEST_FILE=$2
  26. if [ "${S3_ENDPOINT}" == "**None**" ]; then
  27. AWS_ARGS=""
  28. else
  29. AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
  30. fi
  31. echo "Uploading ${DEST_FILE} on S3..."
  32. cat $SRC_FILE | aws $AWS_ARGS s3 cp - s3://$S3_BUCKET/$S3_PREFIX/$DEST_FILE
  33. if [ $? != 0 ]; then
  34. >&2 echo "Error uploading ${DEST_FILE} on S3"
  35. fi
  36. rm $SRC_FILE
  37. }
  38. deleteOld_s3 () {
  39. if [ "${S3_ENDPOINT}" == "**None**" ]; then
  40. AWS_ARGS=""
  41. else
  42. AWS_ARGS="--endpoint-url ${S3_ENDPOINT}"
  43. fi
  44. aws $AWS_ARGS s3 ls s3://$S3_BUCKET/$S3_PREFIX/ | while read -r line;
  45. do
  46. createDate=`echo $line|awk {'print $1" "$2'}`
  47. createDate=`date -d"$createDate" +%s`
  48. olderThan=`date -d"-${DELETE_OLDER_THAN}" +%s`
  49. if [[ $createDate -lt $olderThan ]]
  50. then
  51. fileName=`echo $line|awk {'print $4'}`
  52. echo $fileName
  53. if [[ $fileName != "" ]]
  54. then
  55. aws $AWS_ARGS s3 rm s3://$S3_BUCKET/$S3_PREFIX/$fileName
  56. echo "Deleted ${fileName} on S3"
  57. fi
  58. fi
  59. done;
  60. }
  61. shouldDeleteOld_s3() {
  62. if [ $? == 0 ]; then
  63. if [ "${DELETE_OLDER_THAN}" != "**None**" ]; then
  64. deleteOld_s3
  65. fi
  66. else
  67. >&2 echo "Error deleting s3 files older than ${DELETE_OLDER_THAN}"
  68. fi
  69. }
  70. echo "Creating folder backup for ${DATA_PATH}..."
  71. DUMP_FILE=/tmp/${DUMP_START_TIME}.backup.tar.gz
  72. rm -rf /tmp/*.backup.tar.gz
  73. tar -zcf $DUMP_FILE -C $DATA_PATH .
  74. if [ $? == 0 ]; then
  75. S3_FILE="${DUMP_START_TIME}.backup.tar.gz"
  76. copy_s3 $DUMP_FILE $S3_FILE
  77. else
  78. >&2 echo "Error creating dump of folder ${DATA_PATH}"
  79. fi
  80. shouldDeleteOld_s3
  81. echo "Folder backup finished"