Guest User

Untitled

a guest
Jun 18th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #Local directory and file info
  4. DUMP_DIR=/**YourDirectory**
  5. DUMP_SUBDIR=`date +%m`
  6. ARCHIVE_NAME=`date +%d`
  7.  
  8. #Remote directory for upload
  9. REMOTE_USER=**YourRemoteUsername**
  10. REMOTE_IP=**YourRemoteIP**
  11. REMOTE_DIR=/**YourRemoteDirectory**
  12.  
  13. #MySQL
  14. mUSER=**YourMySQLUser**
  15. mPASS=**YourPassword**
  16.  
  17. #Format Output - to make things perty
  18. fout()
  19. {
  20. SP='.'
  21. S1=${#1}
  22. S2=${#2}
  23. C_SIZE=72
  24. SP_SIZE=$((C_SIZE-S1))
  25. while [ $SP_SIZE -gt 0 ]
  26. do
  27. SP+='.'
  28. SP_SIZE=$((SP_SIZE-1))
  29. done
  30. echo -e "$1" "$SP" "$2"
  31. }
  32.  
  33. #Other Globals
  34. ROOT_UID=0 # Only users with $UID 0 have root privileges.
  35. LINES=50 # Default number of lines saved.
  36. E_XCD=86 # Can't change directory?
  37. E_NOTROOT=87 # Non-root exit error.
  38.  
  39. #Colors
  40. COFF="\033[0m" #color off
  41. BLACK="\033[30m"
  42. RED="\033[31m"
  43. GREEN="\033[32m"
  44. BROWN="\033[33m"
  45. BLUE="\033[34m"
  46. MAGE="\033[35m"
  47. CYAN="\033[36m"
  48. GRAY="\033[37m"
  49.  
  50. # Run as root, of course.
  51. if [ "$UID" -ne "$ROOT_UID" ]
  52. then
  53. echo "Must be root to run this script."
  54. exit $E_NOTROOT
  55. fi
  56.  
  57. #check directory exsistance
  58. if [ ! -d "${DUMP_DIR}/${DUMP_SUBDIR}" ]
  59. then
  60. echo -e "Making directory... [${CYAN}${DUMP_DIR}/${DUMP_SUBDIR}${COFF}]"
  61. sleep 1
  62. if MakeDir=`mkdir "${DUMP_DIR}/${DUMP_SUBDIR}"`
  63. then
  64. fout "Directory creation" "[${GREEN}ok${COFF}]"
  65. else
  66. fout "Directory creation" "[${RED}fail${COFF}]"
  67. fi
  68. else
  69. echo -e "Skipping directory creation for ${DUMP_DIR}/${DUMP_SUBDIR}"
  70. fi
  71.  
  72. sleep 1
  73.  
  74. #Get database names
  75. if DBS=`mysql --user="$mUSER" --password="$mPASS" -Bse 'show databases'`
  76. then
  77. fout "Getting database names" "[${GREEN}ok${COFF}]"
  78. else
  79. fout "Getting database names" "[${RED}fail${COFF}]"
  80. fi
  81.  
  82. sleep 1
  83.  
  84. #loop each database name, dumping database into .sql file
  85. for DB in $DBS
  86. do
  87. FILENAME="${DUMP_DIR}/${DUMP_SUBDIR}/${DB}-`date +%m-%d-%y`.sql"
  88. if DUMP=`mysqldump --user="$mUSER" --password="$mPASS" "$DB" > "$FILENAME"`
  89. then
  90. fout "Saving ${FILENAME}" "[${GREEN}ok${COFF}]"
  91. else
  92. fout "Saving ${FILENAME}" "[${RED}fail${COFF}]"
  93. fi
  94. sleep 0.25
  95. done
  96.  
  97. #Add to archive.
  98. if ADD=`tar czPf "$DUMP_DIR"/"$DUMP_SUBDIR"/"$ARCHIVE_NAME".tar.gz "$DUMP_DIR"/"$DUMP_SUBDIR"/*.sql`
  99. then
  100. fout "Gathering sql files" "[${GREEN}ok${COFF}]"
  101. sleep 1
  102. fout "Compressing sql files" "[${GREEN}ok${COFF}]"
  103. else
  104. fout "Gathering sql files" "[${RED}fail${COFF}]"
  105. fi
  106.  
  107. sleep 1
  108.  
  109. #Clean up .sql files
  110. if RM=`rm "$DUMP_DIR"/"$DUMP_SUBDIR"/*.sql`
  111. then
  112. fout "Cleaning up sql files" "[${GREEN}ok${COFF}]"
  113. else
  114. fout "Cleaning up sql files" "[${RED}fail${COFF}]"
  115. fi
  116.  
  117. sleep 1
  118.  
  119. #Copy to remote server using passphraseless/passwordless ssh keys
  120. if SCP=`scp -P 22 -i /root/.ssh/id2_rsa "$DUMP_DIR"/"$DUMP_SUBDIR"/"$ARCHIVE_NAME".tar.gz "$REMOTE_USER"@"$REMOTE_IP":"$REMOTE_DIR"/"$DUMP_SUBDIR"_"$ARCHIVE_NAME".tar.gz`
  121. then
  122. fout "Copying to remote server" "[${GREEN}ok${COFF}]"
  123. else
  124. fout "Copying to remote server" "[${RED}fail${COFF}]"
  125. fi
  126.  
  127. sleep 0.75
  128.  
  129. echo -e "[${GREEN}All Done${COFF}]"
  130. exit
Add Comment
Please, Sign In to add comment