mng_bckp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/bin/bash
  2. #set -x
  3. clear
  4. ###
  5. #
  6. # Author: Stéphane HUC
  7. # mail: devs@stephane-huc.net
  8. #
  9. # License: GNU/GPL 3
  10. #
  11. # Github: https://git.framasoft.org/hucste/tools
  12. #
  13. # Date: 2016/08/21
  14. #
  15. ###
  16. ###
  17. #
  18. # Manage backups for databases
  19. #
  20. ###
  21. age=15
  22. ARG="$1"
  23. day="$(date +'%Y-%m-%d_%H-%M-%S')"
  24. dir_svg="" # folder to save
  25. ext=".tbz2"
  26. file_arch=""
  27. file_chattr="/home/zou/Administration/tools/chattr_sys"
  28. file_log=""
  29. passwd=""
  30. user=""
  31. RSYNC_OPTIONS="-av --cvs-exclude --delete --delete-during --exclude=\".git/\" --ignore-errors --force --progress --stats"
  32. function compress() {
  33. if tar -jcvf "${file_arch}${FILE_SQL}${ext}" ${FILE_SQL}; then
  34. echo -e "File archive: ${file_arch}${FILE_SQL}${ext} \nSize: $(get_size "${file_arch}${FILE_SQL}${ext}")" >> "${file_log}"
  35. fi
  36. }
  37. function cp_svg() {
  38. rsync ${RSYNC_OPTIONS} --rsync-path="nice 19 ionice -c 3 -p $$" "/bckp/local/" "/bckp/usb/"
  39. }
  40. function delete_archive() {
  41. find ${dir_svg} -type f -name "${ext}$" -mtime +${age} -delete
  42. }
  43. function get_size() {
  44. file="$1"
  45. size=$(stat -c "%s" "${file}")
  46. numfmt --to=iec-i --suffix=B --format="%3f" ${size}
  47. unset file size
  48. }
  49. function in_array() {
  50. # equivalent to PHP in_array
  51. # call: in_array needle array
  52. local i=0 needle="$1" IFS=" "; shift; read -a array <<< "$@"
  53. while [ $i -le ${#array[@]} ]; do
  54. if [[ "${array[$i]}" == "${needle}" ]]; then return 0; fi # true
  55. let i=i+1
  56. done
  57. return 1
  58. unset i needle IFS array
  59. }
  60. function last_log() {
  61. [ -L "/logs/last.log" ] && rm /logs/last.log
  62. ln -s "${file_log}" /logs/last.log
  63. }
  64. function uncompress() {
  65. tar -jxvf $archive.tgz /tmp/$archive
  66. }
  67. function verif_archive() {
  68. tar -tfw $archive.tar
  69. }
  70. function mng_db() {
  71. local action="$1"
  72. local -a DB_XCLUD=('Database' 'information_schema' 'mysql' 'performance_schema' 'phpmyadmin')
  73. DATABASES="$(echo "SHOW DATABASES;" | mysql --skip-column-names --user=$user --password=$passwd)"
  74. for DB in $DATABASES; do
  75. case "${action}" in
  76. "check")
  77. mysqlcheck --user=${user} --password=${passwd} -o ${DB}
  78. ;;
  79. "flush")
  80. echo "Flush database $DB"
  81. SQL_CMD="FLUSH TABLES WITH READ LOCK;"
  82. ;;
  83. #"optimize")
  84. #;;
  85. "restore")
  86. mysqladmin -u $user -p create ${NEW_DB}
  87. mysql -u $user -p ${NEW_DB} < ${FILE_SQL}
  88. ;;
  89. "save")
  90. if ! in_array "${DB}" "${DB_XCLUD[@]}"; then
  91. echo "DB: ${DB}" >> "${file_log}"
  92. FILE_SQL="${DB}.${day}.sql"; echo "File SQL: ${FILE_SQL}" >> "${file_log}"
  93. mysqldump --opt --user=${user} --password=${passwd} "${DB}" > "${FILE_SQL}"
  94. if [ -f "${FILE_SQL}" ]; then
  95. echo "Size: $(get_size "${FILE_SQL}")" >> "${file_log}"
  96. compress
  97. rm -f "${FILE_SQL}"
  98. fi
  99. fi
  100. ;;
  101. "unlock")
  102. echo "Unlock database $DB" >> "${file_log}"
  103. SQL_CMD="UNLOCK TABLES;"
  104. ;;
  105. esac
  106. if [[ -n "${SQL_CMD}" ]]; then
  107. echo "${SQL_CMD}" | mysql $DB -u $user --password=$passwd
  108. fi
  109. done
  110. }
  111. function verify_uid() {
  112. if [[ $(id -u) -ne 0 ]]; then
  113. printf "[ \\33[1;31m%s\\33[0;39m ] %s \n" "KO" "Need to get rights admins!"
  114. exit 1
  115. fi
  116. }
  117. verify_uid
  118. [ -f "${file_chattr}" ] && "${file_chattr}" false
  119. case "$ARG" in
  120. "save")
  121. mng_db "save"
  122. #compress
  123. last_log
  124. delete_archive
  125. cp_svg
  126. ;;
  127. *) mng_db "$ARG" ;;
  128. esac
  129. [ -f "${file_chattr}" ] && "${file_chattr}" true