Advertisement
OldManRiver

MySQL Backup/Restore

May 12th, 2016
124
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /bin/bash
  2. # Author: Nyle Davis Created 14-07-17
  3. # Purpose: Backup all MySQL DBs and tables as dump
  4. # File: mysql_dump.sh
  5. # Modified: 16-05-12 By: NED
  6. # Fixed errors and moved rm code inside if statement
  7. # Run this with command:
  8. # bash ../Scripts/Backups/mysql_dump.sh (options)
  9. # Options:
  10. # 1=mode: backup, restore
  11. # Script is CRON capable
  12.  
  13. if [[ "${1}" != "" ]]; then
  14. run_type=${1};
  15. else
  16. run_type="backup";
  17. fi
  18. defdir='/home/files/Dropbox/backups/';
  19. HSTNM=$(hostname -f);
  20. if [[ ${2} != "" ]]; then
  21. bkpath=$2;
  22. else
  23. mkdir /backups/mysql/;
  24. bkpath="/backups/mysql/";
  25. chown -R ndavis:users ${bkpath};
  26. chmod -R 775 ${bkpath};
  27. # bkpath="${defdir}MySQL-DB/";
  28. fi
  29. #flname="mysql_dump_${HSTNM}_("`eval date +%Y-%m-%d`").tgz";
  30. flname="mysql_dump_Aspire-LT-2016-05-11.tar.gz";
  31. #echo "RT=> ${run_type} HST=> ${HSTNM} BP=> ${bkpath} FL=> ${flname}";
  32.  
  33. dbuser='ndavis';
  34. dbpass='nomened1497';
  35. dbpath='/usr/bin';
  36. GZIP="$(which gzip)";
  37. infile=${bkpath}${flname};
  38. if [[ ${run_type} == "restore" ]]; then
  39. # Restore DBs
  40. echo "Running MySQL compressed file restore!";
  41. echo "InFile=> ${infile}";
  42. $GZIP -9 < ${infile} | ${dbpath}/mysql -u ${dbuser} -p${dbpass};
  43. # ${dbpath}/mysql --user=${dbuser} --password=${dbpass} < ${infile};
  44. # tar -xzOf ${infile} | ${dbpath}/mysql --user=${dbuser} --password=${dbpass};
  45. fi
  46.  
  47. if [[ ${run_type} == "backup" ]]; then
  48. rm -f "${bkpath}${flname}";
  49. # dbopts='--add-drop-database --add-drop-table --events -A -C -f';
  50. dbopts='-A -E -C -f';
  51.  
  52. # Backup DBs
  53. echo "Running MySQL compressed file backup!";
  54. echo "OutFile=> ${infile}";
  55. ${dbpath}/mysqldump -u ${dbuser} -p${dbpass} ${dbopts} | $GZIP -9 > ${infile};
  56. fi
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement