Guest User

Untitled

a guest
Feb 6th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. #!/bin/bash
  2. # ---------------------------------------------------------------------
  3. # Web hosts backup script
  4. # prochor666@gmail.com
  5. # Full backup few days (7 is default)
  6. # This script is licensed under GNU GPL version 2.0 or above
  7. # ---------------------------------------------------------------------
  8.  
  9. clear
  10.  
  11. # Setup
  12. BACKUPROOT="/var/webserver-backup"
  13. MYSQLBACKUPROOT="$BACKUPROOT/mysql"
  14. WEBBACKUPROOT="$BACKUPROOT/webhosts"
  15. WEBROOT="/var/www"
  16. MUSER="root"
  17. MPASS="mysqlpassword"
  18. MHOST="localhost"
  19. MYSQL="$(which mysql)"
  20. MYSQLDUMP="$(which mysqldump)"
  21. GZIP="$(which gzip)"
  22. TAR="$(which tar)"
  23. LOGFILE="$BACKUPROOT/webserver-backup.log"
  24.  
  25. _cleanup () {
  26. find $MYSQLBACKUPROOT -type f -mtime +$1 -print | xargs -I {} rm {}
  27. find $WEBBACKUPROOT -type f -mtime +$1 -print | xargs -I {} rm {}
  28. }
  29.  
  30. _get_date_suffix () {
  31. V=`date +"%Y-%m-%d"`
  32. echo "$V"
  33. }
  34.  
  35. _get_dir_size () {
  36. echo `du -hs $1 | cut -f1`
  37. }
  38.  
  39. _log_reset () {
  40. echo "" > $LOGFILE
  41. }
  42.  
  43. _log () {
  44. T=`date +"%d.%m.%Y %T"`
  45. echo "$T $1" >> $LOGFILE
  46. }
  47.  
  48. # Check dirs
  49. if [ ! -d $MYSQLBACKUPROOT ]; then
  50. mkdir -p $MYSQLBACKUPROOT
  51. fi
  52.  
  53. if [ ! -d $WEBBACKUPROOT ]; then
  54. mkdir -p $WEBBACKUPROOT
  55. fi
  56.  
  57. # Reset log
  58. _log_reset
  59. _log "Backup job begin"
  60.  
  61. # First, check old files and delete all files older then $1 days
  62. _cleanup 7
  63.  
  64.  
  65. # MySQL backup
  66. DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
  67.  
  68. for DB in $DBS
  69. do
  70. if [ $DB != "information_schema" ] && [ $DB != "performance_schema" ] && [ $DB != "mysql" ]; then
  71. DFILE="$MYSQLBACKUPROOT/$DB-$(_get_date_suffix).gz"
  72. _log "Backup database $DB to $DFILE"
  73. $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $DB | $GZIP -9 > $DFILE
  74. fi
  75. done
  76.  
  77. _log "MySQL backup completed, directory size: $(_get_dir_size $MYSQLBACKUPROOT)"
  78.  
  79.  
  80. # Webhosts ackup
  81. for DIR in $WEBROOT/*
  82. do
  83. if [ -d $DIR ] && [ ${DIR##*/} != "html" ]; then
  84. WFILE="$WEBBACKUPROOT/${DIR##*/}-$(_get_date_suffix).gz"
  85. _log "Backup directory $DIR to $WFILE"
  86. $TAR -zcf $WFILE $DIR
  87. fi
  88. done
  89.  
  90. _log "Webhosts backup completed, directory size: $(_get_dir_size $WEBBACKUPROOT)"
Add Comment
Please, Sign In to add comment