Advertisement
Guest User

Untitled

a guest
Jul 15th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #!/bin/bash
  2. # Run s3cmd before this script by running "s3cmd --configure"
  3. # Replace <USER> with mysql user, <PASS> with mysql password, <PATH OF APPLICATION CODE> Path of application code , <DBNAME> Name of the database to be backed up ,
  4. # <BACKUPFILENAME> with Name of the backup file for code , <S3BUCKETNAME> S3 Bucket Name
  5. # This script creates bucket if does not exist already
  6. # The filenames will be <DBNAME>20170709.sql.gz and <BACKUPFILENAME20170708.tar.gz
  7. # This script rotates the backups every 7 days
  8.  
  9. today=`date --date=today "+%Y%m%d"`
  10.  
  11. #MySql credentials
  12.  
  13. MUSER=<USER>
  14. MPASS=<PASS>
  15.  
  16. #Backup Paths
  17.  
  18. [ -d /backups/ ] || /bin/mkdir /backups/
  19. BPATH="/backups/"
  20. CODEPATH="<PATH OF APPLICATION CODE>" #example /var/www/myapp
  21.  
  22. #Tool
  23.  
  24. MYSQL="$(which mysql)"
  25. MYSQLDUMP="$(which mysqldump)"
  26. TAR="$(which tar)"
  27. GZIP="$(which gzip)"
  28.  
  29. #Mysql backups
  30.  
  31. $MYSQLDUMP -u $MUSER -p$MPASS <DBNAME> | $GZIP -9 > $BPATH/<DBNAME>$today.sql.gz
  32.  
  33. #Code backups
  34.  
  35. $TAR -zcf $BPATH/<BACKUPFILENAME>$today.tar.gz $CODEPATH
  36.  
  37. #Create S3 Backup if does not exist
  38.  
  39. [ -d s3://<S3BUCKETNAME> ] || s3cmd mb s3://<S3BUCKETNAME>
  40. /usr/bin/s3cmd sync $BPATH s3://<S3BUCKETNAME>
  41.  
  42. #Remove backups from disk after syncing to S3
  43.  
  44. rm -rf $BPATH
  45.  
  46. #Back Rotation
  47.  
  48. s3cmd ls s3://<S3BUCKETNAME> | while read -r line;
  49. do
  50. createDate=`echo $line|awk {'print $1" "$2'}`
  51. createDate=`date -d"$createDate" +%s`
  52. olderThan=`date -d"- 7days" +%s`
  53. if [[ $createDate -lt $olderThan ]]
  54. then
  55. fileName=`echo $line|awk {'print $4'}`
  56. echo $fileName
  57. if [[ $fileName != "" ]]
  58. then
  59. s3cmd del "$fileName"
  60. fi
  61. fi
  62. done;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement