Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #!/bin/bash
  2. ###########
  3. #
  4. # An import file to bring the latest database backup to the local system for development
  5. #
  6. ###########
  7.  
  8. BACKUP_DIRECTORY=/home/pbaproduction/db_backups
  9. REMOTE_USER=readonly
  10. REMOTE_PASS="zRsVJvG&wP8.XK"
  11. DOMAIN=performancebeef.com
  12.  
  13. # Local Credentials for accessing database
  14. LOCAL_USER=nishatech
  15. LOCAL_PASS=nishatech
  16. LOCAL_DATABASE=feedlot
  17.  
  18. # Clears out the local database so the newer version can be imported
  19. function clearDatabase() {
  20. mysql --user=${LOCAL_USER} --password=${LOCAL_PASS} 2>&1 << EOF
  21. DROP DATABASE ${LOCAL_DATABASE};
  22. CREATE DATABASE ${LOCAL_DATABASE};
  23. EOF
  24. }
  25.  
  26. # Converts a passed-in timestamp to a more readable format
  27. function prettifyDate() {
  28. local months=( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )
  29.  
  30. local YEAR=${1:0:4}
  31. local MONTH=${months[${1:5:2}]}
  32. local DAY=${1:7:2}
  33. DAY=`echo $DAY | sed 's/^0*//'`
  34. local HOUR=${1:10:2}
  35. HOUR=`echo $HOUR | sed 's/^0*//'` # Remove leading zeroes
  36. local MIN=${1:12:2}
  37. STAMP="${MONTH} ${DAY}"
  38. local AMPM="AM"
  39. if [[ "$HOUR" -ge 12 ]]
  40. then
  41. AMPM="PM"
  42. fi
  43.  
  44. if [[ "$DAY" == 1* && "$DAY" -ne 1 ]]
  45. then
  46. STAMP="${STAMP}th"
  47. elif [[ "$DAY" == *1 ]]
  48. then
  49. STAMP="${STAMP}st"
  50. elif [[ "$DAY" == *2 ]]
  51. then
  52. STAMP="${STAMP}nd"
  53. elif [[ "$DAY" == *3 || "$DAY" -eq 3 ]]
  54. then
  55. STAMP="${STAMP}rd"
  56. else
  57. STAMP="${STAMP}th"
  58. fi
  59. STAMP="${STAMP}, ${YEAR} at $(( (HOUR + 11) % 12 + 1 )):${MIN} ${AMPM}"
  60. }
  61.  
  62. # Get the latest backup created
  63. printf "Finding latest backup created on the server...\n"
  64. BACKUP_FILE=$(ssh ${REMOTE_USER}@${DOMAIN} "ls -1tr ${BACKUP_DIRECTORY}" | tail -n1)
  65. printf "Done.\n\n"
  66.  
  67. DOWNLOAD_FILE="${BACKUP_DIRECTORY}/${BACKUP_FILE}"
  68. printf "Downloading ${BACKUP_FILE}...\n";
  69. sftp ${REMOTE_USER}@${DOMAIN}:${DOWNLOAD_FILE} ${BACKUP_FILE}
  70. printf "Done.\n\n"
  71.  
  72. prettifyDate ${BACKUP_FILE:0:14}
  73. printf "Backup file was made on ${STAMP}.\n\n"
  74.  
  75. printf "Clearing current database..."
  76. clearDatabase
  77. printf " Done.\n\n"
  78.  
  79. printf "Downloading to local database... ";
  80. mysql --user=${LOCAL_USER} --password=${LOCAL_PASS} ${LOCAL_DATABASE} < ${BACKUP_FILE} 2>&1 | grep -v "Warning: Using a password"
  81. printf "Done.\n\n"
  82.  
  83. printf "Cleaning up local files... "
  84. /bin/rm ${BACKUP_FILE}
  85. printf "Done.\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement