Advertisement
OldManRiver

MySQL Backup BASH Script

Sep 1st, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. All,
  2.  
  3. Trying to learn BASH functions, don't make sense to me yet, being conceptual and concepts not clear to me yet.
  4.  
  5. I'm down to this. When I assign the "read_db_login" routine, in line 86 (commenting out line # 87) of the following code, I get blanks returned. When I comment out #86 and uncomment 87 then I see all the vars/arrays correctly.
  6.  
  7. Not sure why I'm not getting the array to set right, so all help appreciated.
  8.  
  9. Cheers
  10.  
  11. OMR
  12.  
  13. #! /bin/bash
  14. ################################################################################
  15. # Author: Nyle Davis Created 12-04-20
  16. # Purpose: Backup all MySQL DBs and tables
  17. # File: mysql_backup.sh
  18. # Run this with command:
  19. # bash ../Scripts/Backups/mysql_backup.sh (options)
  20. # Options:
  21. # 1=mode: daily, system, dump, restore
  22. # 2=fext: .sql, .zip .tar.gz
  23. # 3=path: alternate backup path, such as flash or network
  24. # 4=Read File: Read the file containing DB login info
  25. # Script is CRON capable
  26. ################################################################################
  27. # Modified: 16-08-27 By: NED
  28. # Added the dump & restore options
  29. ################################################################################
  30.  
  31. chk_dirs(){
  32. # Check directories, create missing ones
  33. hstname=$(hostname -s);
  34. dest="/backups/$hstname";
  35. myuid=${user};
  36. if [[ ${myuid} = "root" ]] || [[ -z ${myuid} ]]; then
  37. myuid=${SUDO_USER};
  38. fi
  39. drlist="/backups /Blogs /Config_files /data /dpkg-repack /Repos_DPKG";
  40. drlist="${drlist} /Scripts /var/www $dest $dest/daily $dest/system";
  41. drlist="${drlist} $dest/dump $dest/individual";
  42. for word in $drlist; do
  43. # echo "W=> $word";
  44. if [ ! -d "$word" ]; then
  45. echo "Creating Missing Dir=> $word";
  46. mkdir -p $word;
  47. chown -R $user:users $word;
  48. chmod -R 775 $word;
  49. fi
  50. done;
  51. } # end function chk_dirs
  52.  
  53. prm_qual(){
  54. # Qualify parameters
  55. # 1=action type: daily, system, individual, dump, restore
  56. # 2=file ext: .sql, .zip .tar.gz
  57. # 3=Path: Backup or Restore path
  58. # 4=Read File: Read the file containing DB login info
  59. if [ $# -lt 4 ]; then
  60. if [[ $# -lt 1 ]]; then
  61. echo "You entered no parameters!";
  62. else
  63. echo "You are missing one or more parameters!";
  64. fi
  65. echo "The correct parameters are:";
  66. echo " 1=action type: daily, system, individual, dump, restore";
  67. echo " 2=file ext: .sql, .zip .tar.gz";
  68. echo " 3=Path: Backup or Restore path";
  69. echo " 4=Read File: Read the file containing DB login info";
  70. echo "Try again!";
  71. exit;
  72. fi
  73. echo "$1, $2, $3, $4";
  74. } # end function prm_qual
  75.  
  76. read_db_login(){
  77. # Find the DB login data from the passed file.
  78. while IFS='' read -r line || [[ -n "$line" ]]; do
  79. if [[ ${line:0:7} = "dbuser=" ]]; then
  80. dbuser=${line:7};
  81. fi
  82. if [[ ${line:0:7} = "dbpass=" ]]; then
  83. dbpass=${line:7};
  84. fi
  85. if [[ ${line:0:7} = "dbpath=" ]]; then
  86. dbpath=${line:7};
  87. fi
  88. if [[ ${line:0:7} = "dbopts=" ]]; then
  89. dbopts=${line:7};
  90. fi
  91. done < $1;
  92. echo $dbuser, $dbpass, $dbpath, $dbopts;
  93. } # end function read_db_login
  94.  
  95. db_restore(){
  96. #function db_restore
  97. echo "Reading file => $3";
  98. # declare -a log_ray=$(read_db_login $3);
  99. read_db_login $3;
  100. echo "LR=> $log_ray";
  101. echo "Login Info File done";
  102.  
  103. # ${dbpath}/mysql -u ${dbuser} -p${dbpass} ${dbopts} < ${infile}
  104. } # end function db_restore
  105.  
  106. db_backup( ){
  107. # Backup DBs
  108. echo "1=> $1";
  109. echo "2=> $2";
  110. echo "3=> $3";
  111. cd ${bkpath};
  112. rm -f "${flname}";
  113. GZIP="$(which gzip)"
  114. ${dbpath}/mysqldump -u ${dbuser} -p${dbpass} ${dbopts} -A -C -f | $GZIP -9 > ${flname};
  115.  
  116. } # end function db_backup
  117.  
  118.  
  119. # May have to additionally manually backup the phpmyadmin option DB as sometime does not work correctly
  120.  
  121. db_bkupind(){
  122. #function db_bkupind(){
  123. # get all database listing
  124. DBS="$(mysql -u $MUSER -p$MPASS -h $MHOST -P $MPORT -Bse 'show databases')";
  125. # start to dump database one by one
  126. for db in $DBS
  127. do
  128. DUMP="yes";
  129. if [ "$IGNOREDB" != "" ]; then
  130. for i in $IGNOREDB # Store all value of $IGNOREDB ON i
  131. do
  132. if [ "$db" == "$i" ]; then # If result of $DBS(db) is equal to $IGNOREDB(i) then
  133. DUMP="NO"; # SET value of DUMP to "no"
  134. #echo "$i database is being ignored!";
  135. fi
  136. done
  137. fi
  138.  
  139. if [ "$DUMP" == "yes" ]; then # If value of DUMP is "yes" then backup database
  140. FILE="$BACKUPDIR/$NOW-$db.gz";
  141. echo "BACKING UP $db";
  142. $MYSQLDUMP --add-drop-database --opt --lock-all-tables -u $MUSER -p$MPASS -h $MHOST -P $MPORT $db | gzip > $FILE
  143. fi
  144. done
  145. } # end function db_bkupind
  146.  
  147. action_mode(){
  148. if [[ $1 = "daily" ]]; then
  149. flname="("`eval date +%Y-%m-%d`")_mysql_daily.${fil_ext}";
  150. dest="${bakpath}/daily";
  151. db_backup ${"daily", "${dest}/${flname}", "${DBL_file}"};
  152. elif [[ $1 = "system" ]]; then
  153. flname="("`eval date +%Y-%m-%d`")_mysql_system.${fil_ext}";
  154. dest="${bakpath}/system";
  155. db_backup ${"system", "${dest}/${flname}", "${DBL_file}"};
  156. elif [[ $1 = "individual" ]]; then
  157. db_bkupind "${bakpath}/individual";
  158. elif [[ $1 = "dump" ]]; then
  159. flname="("`eval date +%Y-%m-%d`")_mysql_dump.${fil_ext}";
  160. dest="${bakpath}/dump";
  161. db_backup ${"dump", "${dest}/${flname}", "${DBL_file}"};
  162. elif [[ $1 = "restore" ]]; then
  163. echo "Restore Started!";
  164. db_restore $2 $3 $4;
  165. echo "Restore Complete!";
  166. fi
  167. } # end function action_mode
  168.  
  169. echo "Checking directories!";
  170. chk_dirs;
  171. echo "Qualifying parameters!";
  172. declare -a ret_ray=$(prm_qual $1 $2 $3 $4);
  173. echo "Checking Mode!";
  174. action_mode $1 $2 $3 $4;
  175. exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement