Guest User

Untitled

a guest
Oct 10th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #Show instructions
  4. function show_usage {
  5. echo "Backup account and database, assuming /home/USER filesystem setup."
  6. echo "Usage:"
  7. echo " -h Help menu - See this information"
  8. echo " Files: "
  9. echo " -a Account user name, for use in /home/USER backup. If no path is set via -p flag, the /home/USER path will be backed up. "
  10. echo " -f Filepath to Backup "
  11. echo " Database: "
  12. echo " -d Name of Database to backup "
  13. echo " -h Host of database, if not localhost "
  14. echo " -p Database Password to use for backing up "
  15. echo " -u Database User to use for backup "
  16. echo " Backup File "
  17. echo " -b Name and path of backup file to create. '.tar.bz2' will be appended to the filename "
  18.  
  19. exit 1
  20. }
  21.  
  22. #Sanity Check - are there no arguments?
  23. if [ $# -eq 0 ]; then
  24. show_usage
  25. fi
  26.  
  27. #Set variables to be used
  28. user=''
  29. filepath=''
  30. database=''
  31. dbfile=''
  32. dbuser=''
  33. dbpass=''
  34. dbhost='127.0.0.1'
  35. bkpath=''
  36. finalbkpath=''
  37. finalbkfile=''
  38.  
  39. #Parse flags
  40. while getopts “ha:f:d:p:u:b:” OPTION; do
  41. case $OPTION in
  42. h)
  43. show_usage
  44. ;;
  45. a)
  46. user=$OPTARG
  47. ;;
  48. f)
  49. filepath=$OPTARG
  50. ;;
  51. d)
  52. database=$OPTARG
  53. dbfile="$database-`date +%Y%m%d`.sql"
  54. ;;
  55. h)
  56. dbhost=$OPTARG
  57. ;;
  58. p)
  59. dbpass=$OPTARG
  60. ;;
  61. u)
  62. dbuser=$OPTARG
  63. ;;
  64. b)
  65. bkpath=$OPTARG
  66. ;;
  67. *)
  68. show_usage
  69. ;;
  70. esac
  71. done
  72.  
  73. #Backup based on filepath instead of /home/USER if used
  74. if [ "$filepath" != '' ] && [ -d $filepath ]; then
  75. finalbkpath="$filepath"
  76. elif [ -d /home/$user ]; then
  77. finalbkpath="/home/$user"
  78. else
  79. echo 'Invalid file path or no user specified'
  80. show_usage
  81. fi
  82.  
  83. #Get database dump, if database is defined
  84. if [ "$dbuser" != "" ] && [ "$database" != "" ]; then
  85. mysqldump -u ${dbuser} -p${dbpass} -h ${dbhost} ${database} > ./${dbfile}
  86. elif [ "$database" != "" ]; then
  87. mysqldump -h ${dbhost} ${database} > ./${dbfile}
  88. fi
  89.  
  90. #Create backup
  91. if [ -f ./${dbfile} ]; then
  92. tar -cjf backup-`date +%Y%m%d`.tar.bz2 ${finalbkpath} ./${dbfile}
  93. else
  94. tar -cjf backup-`date +%Y%m%d`.tar.bz2 ${finalbkpath}
  95. fi
Add Comment
Please, Sign In to add comment