Advertisement
Guest User

Untitled

a guest
Dec 4th, 2017
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.63 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Script to backup Magento2 Codebase + Database
  5. #
  6. # @author Raj KB <magepsycho@gmail.com>
  7. # @website http://www.magepsycho.com
  8. # @version 0.1.0
  9.  
  10. # UnComment it if bash is lower than 4.x version
  11. shopt -s extglob
  12.  
  13. ################################################################################
  14. # CORE FUNCTIONS - Do not edit
  15. ################################################################################
  16. #
  17. # VARIABLES
  18. #
  19. _bold=$(tput bold)
  20. _underline=$(tput sgr 0 1)
  21. _reset=$(tput sgr0)
  22.  
  23. _purple=$(tput setaf 171)
  24. _red=$(tput setaf 1)
  25. _green=$(tput setaf 76)
  26. _tan=$(tput setaf 3)
  27. _blue=$(tput setaf 38)
  28.  
  29. #
  30. # HEADERS & LOGGING
  31. #
  32. function _debug()
  33. {
  34. if [[ "$DEBUG" = 1 ]]; then
  35. "$@"
  36. fi
  37. }
  38.  
  39. function _header()
  40. {
  41. printf 'n%s%s========== %s ==========%sn' "$_bold" "$_purple" "$@" "$_reset"
  42. }
  43.  
  44. function _arrow()
  45. {
  46. printf 'āžœ %sn' "$@"
  47. }
  48.  
  49. function _success()
  50. {
  51. printf '%sāœ” %s%sn' "$_green" "$@" "$_reset"
  52. }
  53.  
  54. function _error() {
  55. printf '%sāœ– %s%sn' "$_red" "$@" "$_reset"
  56. }
  57.  
  58. function _warning()
  59. {
  60. printf '%sāžœ %s%sn' "$_tan" "$@" "$_reset"
  61. }
  62.  
  63. function _underline()
  64. {
  65. printf '%s%s%s%sn' "$_underline" "$_bold" "$@" "$_reset"
  66. }
  67.  
  68. function _bold()
  69. {
  70. printf '%s%s%sn' "$_bold" "$@" "$_reset"
  71. }
  72.  
  73. function _note()
  74. {
  75. printf '%s%s%sNote:%s %s%s%sn' "$_underline" "$_bold" "$_blue" "$_reset" "$_blue" "$@" "$_reset"
  76. }
  77.  
  78. function _die()
  79. {
  80. _error "$@"
  81. exit 1
  82. }
  83.  
  84. function _safeExit()
  85. {
  86. exit 0
  87. }
  88.  
  89. #
  90. # UTILITY HELPER
  91. #
  92. function _seekConfirmation()
  93. {
  94. printf 'n%s%s%s' "$_bold" "$@" "$_reset"
  95. read -p " (y/n) " -n 1
  96. printf 'n'
  97. }
  98.  
  99. # Test whether the result of an 'ask' is a confirmation
  100. function _isConfirmed()
  101. {
  102. if [[ "$REPLY" =~ ^[Yy]$ ]]; then
  103. return 0
  104. fi
  105. return 1
  106. }
  107.  
  108.  
  109. function _typeExists()
  110. {
  111. if type "$1" >/dev/null; then
  112. return 0
  113. fi
  114. return 1
  115. }
  116.  
  117. function _isOs()
  118. {
  119. if [[ "${OSTYPE}" == $1* ]]; then
  120. return 0
  121. fi
  122. return 1
  123. }
  124.  
  125. function _checkRootUser()
  126. {
  127. #if [ "$(id -u)" != "0" ]; then
  128. if [ "$(whoami)" != 'root' ]; then
  129. echo "You have no permission to run $0 as non-root user. Use sudo"
  130. exit 1;
  131. fi
  132.  
  133. }
  134.  
  135. function _printPoweredBy()
  136. {
  137. local mp_ascii
  138. mp_ascii='
  139. __ ___ ___ __
  140. / |/ /__ ____ ____ / _ ___ __ ______/ / ___
  141. / /|_/ / _ `/ _ `/ -_) ___(_-</ // / __/ _ / _
  142. /_/ /_/_,_/_, /__/_/ /___/_, /__/_//_/___/
  143. /___/ /___/
  144. '
  145. cat <<EOF
  146. ${_green}
  147. Powered By:
  148. $mp_ascii
  149.  
  150. >> Store: ${_reset}${_underline}${_blue}http://www.magepsycho.com${_reset}${_reset}${_green}
  151. >> Blog: ${_reset}${_underline}${_blue}http://www.blog.magepsycho.com${_reset}${_reset}${_green}
  152.  
  153. ################################################################
  154. ${_reset}
  155. EOF
  156. }
  157.  
  158. ################################################################################
  159. # SCRIPT FUNCTIONS
  160. ################################################################################
  161. function _printUsage()
  162. {
  163. echo -n "$(basename "$0") [OPTION]...
  164.  
  165. Backup Magento2 Codebase + Database.
  166. Version $VERSION
  167.  
  168. Options:
  169. -sd, --src-dir Source directory (from where backup file will be created)
  170. -dd, --dest-dir Destination directory (to where the backup file will be moved)
  171. -bt, --type Backup Type. Default: all
  172. Options:
  173. 1. db (for database only)
  174. 2. code (for codebase only)
  175. 3. all (for database + codebase)
  176. -sm, --skip-media Skip media folder from code backup.
  177. Default: 1
  178. -h, --help Display this help and exit
  179. -v, --version Output version information and exit
  180.  
  181. Examples:
  182. $(basename "$0") --type=all --skip-media=1 --src-dir=... --dest-dir=...
  183.  
  184. "
  185. _printPoweredBy
  186. exit 1
  187. }
  188.  
  189. function processArgs()
  190. {
  191. # Parse Arguments
  192. for arg in "$@"
  193. do
  194. case $arg in
  195. -bt=*|--type=*)
  196. M2_BACKUP_TYPE="${arg#*=}"
  197. ;;
  198. -sd=*|--src-dir=*)
  199. M2_SRC_DIR="${arg#*=}"
  200. ;;
  201. -dd=*|--dest-dir=*)
  202. M2_DEST_DIR="${arg#*=}"
  203. ;;
  204. -sm=*|--skip-media=*)
  205. M2_SKIP_MEDIA="${arg#*=}"
  206. ;;
  207. -bn=*|--backup-name=*)
  208. M2_BACKUP_NAME="${arg#*=}"
  209. ;;
  210. --debug)
  211. DEBUG=1
  212. ;;
  213. -h|--help)
  214. _printUsage
  215. ;;
  216. *)
  217. _printUsage
  218. ;;
  219. esac
  220. done
  221.  
  222. validateArgs
  223. sanitizeArgs
  224. }
  225.  
  226. function validateArgs()
  227. {
  228. ERROR_COUNT=0
  229. if [[ -z "$M2_BACKUP_TYPE" ]]; then
  230. _error "Backup type parameter missing."
  231. ERROR_COUNT=$((ERROR_COUNT + 1))
  232. fi
  233.  
  234. if [[ ! -z "$M2_BACKUP_TYPE" && "$M2_BACKUP_TYPE" != @(db|code|all) ]]; then
  235. _error "Backup type must be one of db|code|all."
  236. ERROR_COUNT=$((ERROR_COUNT + 1))
  237. fi
  238.  
  239. if [[ -z "$M2_SRC_DIR" ]]; then
  240. _error "Source directory parameter missing."
  241. ERROR_COUNT=$((ERROR_COUNT + 1))
  242. fi
  243.  
  244. if [[ ! -z "$M2_SRC_DIR" && ! -f "$M2_SRC_DIR/app/etc/env.php" ]]; then
  245. _error "Source directory must be Magento 2 root folder."
  246. ERROR_COUNT=$((ERROR_COUNT + 1))
  247. fi
  248.  
  249. if [[ -z "$M2_DEST_DIR" ]]; then
  250. _error "Destination directory parameter missing."
  251. ERROR_COUNT=$((ERROR_COUNT + 1))
  252. fi
  253.  
  254. if [[ ! -z "$M2_DEST_DIR" ]] && ! mkdir -p "$M2_DEST_DIR"; then
  255. _error "Unable to create destination directory."
  256. ERROR_COUNT=$((ERROR_COUNT + 1))
  257. fi
  258.  
  259. echo "$ERROR_COUNT"
  260. [[ "$ERROR_COUNT" -gt 0 ]] && exit 1
  261. }
  262.  
  263. function sanitizeArgs()
  264. {
  265. # remove trailing /
  266. if [[ ! -z "$M2_SRC_DIR" ]]; then
  267. M2_SRC_DIR="${M2_SRC_DIR%/}"
  268. fi
  269.  
  270. if [[ ! -z "$M2_DEST_DIR" ]]; then
  271. M2_DEST_DIR="${M2_DEST_DIR%/}"
  272. fi
  273. }
  274.  
  275. function prepareBackupName()
  276. {
  277. if [[ -z "$M2_BACKUP_NAME" ]]; then
  278. #MD5=`echo `date` $RANDOM | md5sum | cut -d ' ' -f 1`
  279. DATETIME=$(date + "%Y-%m-%d-%H%-M%-%S")
  280. M2_BACKUP_NAME="mage2-backup.$DATETIME"
  281. fi
  282. }
  283.  
  284. function prepareCodebaseFilename()
  285. {
  286. M2_CODE_BACKUP_FILE="${M2_DEST_DIR}/${M2_BACKUP_NAME}.tar.gz"
  287. }
  288.  
  289. function prepareDatabaseFilename()
  290. {
  291. M2_DB_BACKUP_FILE="${M2_DEST_DIR}/${M2_BACKUP_NAME}.sql.gz"
  292. }
  293.  
  294. function createDbBackup()
  295. {
  296. _success "Dumping MySQL..."
  297. local host username password dbName
  298. host=$(grep host "${M2_SRC_DIR}/app/etc/env.php" | cut -d "'" -f 4)
  299. username=$(grep username "${M2_SRC_DIR}/app/etc/env.php" | cut -d "'" -f 4)
  300. password=$(grep password "${M2_SRC_DIR}/app/etc/env.php" | cut -d "'" -f 4)
  301. dbName=$(grep dbname "${M2_SRC_DIR}/app/etc/env.php" |cut -d "'" -f 4)
  302.  
  303. # @todo option to skip log tables
  304. mysqldump -h "$host" -u "$username" -p"$password" "$dbName" | gzip > "$M2_DB_BACKUP_FILE"
  305. _success "Done!"
  306. }
  307.  
  308. function createCodeBackup()
  309. {
  310. _success "Archiving Codebase..."
  311. if [[ "$M2_SKIP_MEDIA" == 1 ]]; then
  312. tar -zcf "$M2_CODE_BACKUP_FILE" --exclude="./var" --exclude="./pub/media" --exclude="./pub/static" -C "${M2_SRC_DIR}" .
  313. else
  314. tar -zcf "$M2_CODE_BACKUP_FILE" --exclude="./var" --exclude="./pub/static" -C "${M2_SRC_DIR}" .
  315. fi
  316. _success "Done!"
  317. }
  318.  
  319. function printSuccessMessage()
  320. {
  321. _success "Magento2 Backup Completed!"
  322.  
  323. echo "################################################################"
  324. echo ""
  325. echo " >> Backup Type : ${M2_BACKUP_TYPE}"
  326. echo " >> Backup Source : ${M2_SRC_DIR}"
  327. if [[ $M2_BACKUP_TYPE = @(db|database|all) ]]; then
  328. echo " >> Database Dump File : ${M2_DB_BACKUP_FILE}"
  329. fi
  330.  
  331. if [[ $M2_BACKUP_TYPE = @(codebase|code|all) ]]; then
  332. echo " >> Codebase Archive File : ${M2_CODE_BACKUP_FILE}"
  333. fi
  334.  
  335. echo ""
  336. echo "################################################################"
  337. _printPoweredBy
  338.  
  339. }
  340.  
  341. ################################################################################
  342. # Main
  343. ################################################################################
  344. export LC_CTYPE=C
  345. export LANG=C
  346.  
  347. DEBUG=0
  348. _debug set -x
  349. VERSION="0.1.0"
  350.  
  351. M2_SRC_DIR=
  352. M2_DEST_DIR=
  353. M2_BACKUP_TYPE=all
  354. M2_SKIP_MEDIA=1
  355. M2_BACKUP_NAME=
  356. M2_DB_BACKUP_FILE=
  357. M2_CODE_BACKUP_FILE=
  358.  
  359. function main()
  360. {
  361. [[ $# -lt 1 ]] && _printUsage
  362.  
  363. processArgs "$@"
  364.  
  365. prepareBackupName
  366. prepareCodebaseFilename
  367. prepareDatabaseFilename
  368.  
  369. if [[ "$M2_BACKUP_TYPE" = @(database|db|all) ]]; then
  370. createDbBackup
  371. fi
  372.  
  373. if [[ "$M2_BACKUP_TYPE" = @(codebase|code|all) ]]; then
  374. createCodeBackup
  375. fi
  376.  
  377. printSuccessMessage
  378.  
  379. exit 0
  380. }
  381.  
  382. main "$@"
  383.  
  384. _debug set +x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement