daily pastebin goal
61%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. mysqldump=1  #####создавать дамп всех бд 1 — да; 0 — нет
  4. filedump=1 #####создавать бекап всех файлов 1 — да; 0 — нет
  5. upload=1 #### загружать на удалённый сервер? 1 — да; 0 — нет
  6. user="bortnik"
  7. remotehost="emobort.loseyourip.com" #куда загружать бекапы, работает только если upload = 1
  8. dir="/home/bortnik/backups/version3/`date +%d.%m.%Y`" #директория куда загружать бэкапы
  9.  
  10.  
  11. ####создание дампа всех БД
  12. USER="root"  #юзер
  13. PASSWORD="Galaxys7edge" #пароль
  14. OUTPUT="/backup/`date +%d.%m.%Y`"   #путь куда создаются дампы.
  15.  
  16.  
  17.  
  18.  
  19.  
  20. #####################do not editcode bellow#####################
  21.  
  22. # check root permissions
  23. if [ "x$(id -u)" != 'x0' ]; then
  24.     echo 'Error: this script can be executed only by root'
  25.     exit 1
  26. fi
  27.  
  28. echo -e "\n" >> /var/log/$0.log
  29. echo -e "\n" >> /var/log/$0.log
  30. echo -e "\n" >> /var/log/$0.log
  31.  
  32. echo  >> /var/log/$0.log
  33. echo -e "\e[42m============================$(date +'%d-%b-%Y %R')=========================\e[0m"  >> /var/log/$0.log
  34. echo -e "\e[42m==================================START===============================\e[0m"  >> /var/log/$0.log
  35.  
  36. sleep 3
  37.  
  38. # Create directory if not exist
  39. if [[ ! -d $OUTPUT && ! -L $OUTPUT ]]; then
  40. mkdir -p $OUTPUT && cd $OUTPUT
  41. fi
  42.  
  43.  
  44.  
  45. if [ $mysqldump = "1" ]; then
  46.  
  47. databases=`mysql --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
  48.  
  49. for db in $databases; do
  50.     if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != _* ]] ; then
  51.        
  52.        mysqldump --routines --skip-add-locks --user=$USER --password=$PASSWORD --databases $db  > $OUTPUT/$db.sql;
  53.        echo -e "Dump \e[34m$db\e[0m was successfully created and located here: \033[1m\e[34m $OUTPUT/$db.sql\e[0m\033[0m"   >> /var/log/$0.log
  54.        
  55.  
  56. fi
  57.       done
  58. fi
  59.  
  60. sleep 4
  61.  
  62. #create filedump
  63. if [ $filedump = "1" ]; then
  64. echo -e "Creating \e[34mserver_backup.tar.gz\e[0m has started. Log of this operation is not included." >> /var/log/$0.log
  65. sleep 1
  66. echo -e "\n"  >> /var/log/$0.log
  67. cd $OUTPUT && tar cvpzf server_backup.tar.gz --exclude=/ztmp --exclude=/proc --exclude=/ztemp --exclude=/media --exclude=/mnt --exclude=/opt --exclude=/srv --exclude=/tmp --exclude=$OUTPUT/server_backup*.tar.gz --exclude=/backup --one-file-system /
  68. echo -e "Dump \e[34mserver_backup.tar.gz\e[0m was successfully created and located here: \033[1m\e[34m $OUTPUT/$db.sql\e[0m\033[0m"  >> /var/log/$0.log
  69. else
  70. echo -e "\n"  >> /var/log/$0.log
  71. echo -e "Сreating \e[34mserver_backup.tar.gz\e[0m is not enabled"  >> /var/log/$0.log
  72. fi
  73.  
  74.  
  75. sleep 3
  76.  
  77.  
  78. # Create directory if not exist in remote host and upload backup
  79. if [ $upload = "1" ]
  80. then
  81. ssh $user@$remotehost 'dir="'"$dir"'"; test -d "$dir" || mkdir -p "$dir"'
  82.  rsync -avzh --progress --remove-source-files -W $OUTPUT/*.tar.gz $user@$remotehost:$dir
  83.  sleep 1
  84.  echo -e "\n"  >> /var/log/$0.log
  85.  echo -e "\e[34mserver_backup.tar.gz\e[0m was successfully upload to your remote host."  >> /var/log/$0.log
  86.  sleep 2
  87.  rsync -avzh --progress --remove-source-files -W $OUTPUT/*.sql $user@$remotehost:$dir
  88.  echo -e "\e[34msql-files\e[0m was successfully upload to your remote host."  >> /var/log/$0.log
  89. else
  90. echo -e "Uploading to remote is not enabled"  >> /var/log/$0.log
  91. fi
  92.  
  93. echo  >> /var/log/$0.log
  94. echo -e "\e[42m============================$(date +'%d-%b-%Y %R')=========================\e[0m"  >> /var/log/$0.log
  95. echo -e "\e[42m===================================END===============================\e[0m"  >> /var/log/$0.log
RAW Paste Data
Top