backup.sh

From rtmf@nic.xh, 1 Year ago, written in Plain Text, viewed 167 times. This paste will go to its last resting place in 1 Second.
URL http://paste.beautifulsunrise.org/view/86a71bec Embed
Download Paste or View Raw
  1. '#!/bin/bash
  2. SRCDIR="/home/rtmf/workspace/django/sunrise"
  3. PGUSER="rtmf"
  4. PGPASSWORD="testpass"
  5. PGDATABASE="test"
  6. STAGEPATH="$(mktemp -d --tmpdir backup-XXXXXXX)"
  7. DBDUMPPATH="${PGDATABASE}.psql"
  8. TARBALL="${STAGEPATH}/backup-$(date -Iseconds)-$(uuidgen).tar.gz"
  9. GPGKEYID="backups@foo.bar"
  10. S3BUCKET="show1-test"
  11. # XXX - on many systems /tmp is tmpfs, may wish to explicitly set a tmpdir for these to avoid consuming all ram
  12. pg_dump -Fc > "${STAGEPATH}/${DBDUMPPATH}"
  13. tar -czpf "${TARBALL}" -C "${STAGEPATH}" "${SRCDIR}" "${DBDUMPPATH}" 2>/dev/null
  14. rm "${STAGEPATH}/${DBDUMPPATH}"
  15. # TODO - move finished product out of STAGEPATH
  16. gpg --encrypt -r "${GPGKEYID}" < "${TARBALL}" > "${TARBALL}.gpg"
  17. rm "${TARBALL}"
  18. ( python -c "\
  19. import boto,os,math
  20. from filechunkio import FileChunkIO
  21. s3_connection = boto.connect_s3()
  22. bucket = s3_connection.get_bucket('${S3BUCKET}')
  23. source_path = '${TARBALL}.gpg'
  24. source_size = os.stat(source_path).st_size
  25. chunk_size = 52428800
  26. #50MiB
  27. chunk_count = int(math.ceil(source_size/float(chunk_size)))
  28. upload = bucket.initiate_multipart_upload(os.path.basename(source_path))
  29. for i in range(chunk_count):
  30.         offset = chunk_size*i
  31.         bytes = min(chunk_size, source_size - offset)
  32.         with FileChunkIO(source_path, 'r', offset=offset, bytes=bytes) as file_part:
  33.                 upload.upload_part_from_file(file_part,part_num=i 1)
  34. upload.complete_upload()
  35. "

Reply to "backup.sh"

Here you can reply to the paste above