Advertisement
OldManRiver

MySQL Backup BASH Script

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