Advertisement
pakdoz

mysqltos3

Oct 19th, 2012
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.76 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Updates etc at: https://github.com/woxxy/MySQL-backup-to-Amazon-S3
  4. # Under a MIT license
  5.  
  6. # change these variables to what you need
  7. MYSQLROOT=root
  8. MYSQLPASS=password
  9. S3BUCKET=bucketname
  10. FILENAME=filename
  11. DATABASE='--all-databases'
  12. # the following line prefixes the backups with the defined directory. it must be blank or end with a /
  13. S3PATH=mysql_backup/
  14. # when running via cron, the PATHs MIGHT be different. If you have a custom/manual MYSQL install, you should set this manually like MYSQLDUMPPATH=/usr/local/mysql/bin/
  15. MYSQLDUMPPATH=
  16. #tmp path.
  17. TMP_PATH=~/
  18.  
  19. PERIOD=${1-day}
  20.  
  21. echo "Selected period: $PERIOD."
  22.  
  23. echo "Starting backing up the database to a file..."
  24.  
  25. # dump all databases
  26. ${MYSQLDUMPPATH}mysqldump --quick --user=${MYSQLROOT} --password=${MYSQLPASS} ${DATABASE} > ${TMP_PATH}${FILENAME}.sql
  27.  
  28. echo "Done backing up the database to a file."
  29. echo "Starting compression..."
  30.  
  31. tar czf ${TMP_PATH}${FILENAME}.tar.gz ${TMP_PATH}${FILENAME}.sql
  32.  
  33. echo "Done compressing the backup file."
  34.  
  35. # we want at least two backups, two months, two weeks, and two days
  36. echo "Removing old backup (2 ${PERIOD}s ago)..."
  37. s3cmd del --recursive s3://${S3BUCKET}/${S3PATH}previous_${PERIOD}/
  38. echo "Old backup removed."
  39.  
  40. echo "Moving the backup from past $PERIOD to another folder..."
  41. s3cmd mv --recursive s3://${S3BUCKET}/${S3PATH}${PERIOD}/ s3://${S3BUCKET}/${S3PATH}previous_${PERIOD}/
  42. echo "Past backup moved."
  43.  
  44. # upload all databases
  45. echo "Uploading the new backup..."
  46. s3cmd put -f ${TMP_PATH}${FILENAME}.tar.gz s3://${S3BUCKET}/${S3PATH}${PERIOD}/
  47. echo "New backup uploaded."
  48.  
  49. echo "Removing the cache files..."
  50. # remove databases dump
  51. rm ${TMP_PATH}${FILENAME}.sql
  52. rm ${TMP_PATH}${FILENAME}.tar.gz
  53. echo "Files removed."
  54. echo "All done."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement