backup.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/bash
  2. # https://github.com/nixscript/backup.sh
  3. # shellcheck source=/dev/null
  4. source <(grep "=" /usr/local/etc/backup.sh/backup.cfg)
  5. if [[ $1 == "-c" || $1 == "--config" ]]; then
  6. if [[ $LANG == "ru_RU.UTF-8" ]]; then
  7. echo "Укажите логин удалённой машины:"
  8. else
  9. echo "Type login for the remote computer:"
  10. fi
  11. read -r REMOTEUSER
  12. if [[ -z "$REMOTEUSER" ]]; then
  13. echo -e "\\e[33;1mWRONG! Run script again and type login correct!\\[0m"
  14. exit
  15. fi
  16. if [[ $LANG == "ru_RU.UTF-8" ]]; then
  17. echo "Укажите имя удалённой машины или IP-адрес:"
  18. else
  19. echo "Type remote host name or IP for the remote computer:"
  20. fi
  21. read -r REMOTEHOST
  22. if [[ -z "$REMOTEHOST" ]]; then
  23. echo -e "\\e[33;1mWRONG! Run script again and type remote host correct!\\[0m"
  24. exit
  25. fi
  26. if [[ $LANG == "ru_RU.UTF-8" ]]; then
  27. echo "Укажите путь для бекапов на удалённой машине [/home/$REMOTEUSER/backups/]:"
  28. else
  29. echo "Type remote path for backups on the remote computer [/home/$REMOTEUSER/backups/]:"
  30. fi
  31. read -r REMOTEPATH
  32. if [[ -z "$REMOTEPATH" ]]; then
  33. REMOTEPATH="/home/$REMOTEUSER/backups/"
  34. fi
  35. if [[ $LANG == "ru_RU.UTF-8" ]]; then
  36. echo "Укажите абсолютные пути к файлам и директориям, которые нужно бекапить, через пробел [Пример: /var/www/ /etc/apache2/sites-avialable]:"
  37. else
  38. echo "Type absolute paths files/dirs to backaup over space [example: /var/www /etc/apache2/sites-avialable]:"
  39. fi
  40. read -r TARGETS
  41. if [[ -z "$TARGETS" ]]; then
  42. echo -e "\\e[33;1mWRONG! You must type some paths for backup! Run again.\\e[0m"
  43. exit
  44. fi
  45. if [[ $LANG == "ru_RU.UTF-8" ]]; then
  46. echo "Как вы хотите создавать бекапы?
  47. 1. Полный архив
  48. 2. Только изменения за последние сутки
  49. Введите порядковый номер [default: 1]:"
  50. else
  51. echo "Choice method for creating backup
  52. 1. Full archive
  53. 2. Only updates
  54. Type the number [default: 1]:"
  55. fi
  56. read -rn 1 TARCHS
  57. if [[ -z $TARCHS ]]; then
  58. TARCHS=1
  59. elif [[ $TARCHS != "1" && $TARCHS != "2" ]]; then
  60. echo -e "\\e[31;1mWRONG! You must specify the number 1 or 2! Run again.\\e[0m"
  61. exit
  62. fi
  63. if [[ $TARCHS == "1" ]]; then
  64. if [[ $LANG == "ru_RU.UTF-8" ]]; then
  65. echo "Укажите существующий путь для скрипта clearbckp.sh на удалённой машине [/home/$REMOTEUSER/bin/]:"
  66. else
  67. echo "Type exists path for clearbckp.sh on the remote computer [/home/$REMOTEUSER/bin/]:"
  68. fi
  69. read -r RPATH
  70. if [[ -z "$RPATH" ]]; then
  71. RPATH="/home/$REMOTEUSER/bin"
  72. fi
  73. fi
  74. if [[ ! -f "$HOME/.ssh/id_rsa.pub" ]]; then
  75. # Generate ssh-key for connect to remote computer
  76. ssh-keygen -t rsa
  77. fi
  78. # Copy public ssh-key to remote computer (need remote password)
  79. scp "$HOME/.ssh/id_rsa.pub" "$REMOTEUSER@$REMOTEHOST:/home/$REMOTEUSER/.ssh/authorized_keys"
  80. # Change params into scripts
  81. sed -i "s%REMOTEUSER=\"user\"%REMOTEUSER=\"$REMOTEUSER\"%; s%REMOTEHOST=\"example.com\"%REMOTEHOST=\"$REMOTEHOST\"%; s%REMOTEPATH=\"/home/backupsdir/\"%REMOTEPATH=\"$REMOTEPATH\"%; s%TARGETS=\"/var/www\"%TARGETS=\"$TARGETS\"%; s%TARCHS=\"1\"%TARCHS=\"$TARCHS\"%" /usr/local/etc/backup.sh/backup.cfg
  82. if [[ $TARCHS == "1" ]]; then
  83. {
  84. echo '#!/bin/bash'
  85. echo "find ${REMOTEPATH}* -mtime +7 -exec rm {} \\;"
  86. } >> clearbckp.sh
  87. chmod +x clearbckp.sh
  88. scp -B clearbckp.sh "$REMOTEUSER@$REMOTEHOST:$RPATH"
  89. rm -f clearbckp.sh
  90. echo -e "\\e[32;1mScript clearbckp.sh copyed on the remote computer to $RPATH\\e[0m"
  91. fi
  92. echo '#!/bin/bash' > /etc/cron.daily/backup_sh
  93. {
  94. echo "# shellcheck source=/dev/null"
  95. echo "source <(grep \"=\" /usr/local/etc/backup.sh/backup.cfg)"
  96. echo "d=\$(date +%F)"
  97. echo "if [[ \$TARCHS == \"1\" ]]; then"
  98. echo " CMD=\"tar -cvf - \$TARGETS\""
  99. echo " \$CMD | xz -9 --threads=0 - > \"\$TMPDIR/\${d}backup.tar.xz\""
  100. echo "else"
  101. echo " LIST=\"\""
  102. echo " for i in \$TARGETS"
  103. echo " do"
  104. echo " L=\$(find \"\$i\" -type f -mtime -1)"
  105. echo " LIST=\"\$LIST"
  106. echo "\$L\""
  107. echo " done"
  108. printf " LIST=\$(echo \"\$LIST\" | tr \"\\\n\" \" \")\n"
  109. echo " CMD=\"tar -cvf - \${LIST:1:-1}\""
  110. echo " \$CMD | xz -9 --threads=0 - > \"\$TMPDIR/\${d}backup.tar.xz\""
  111. echo "fi"
  112. echo "scp -B \"\$TMPDIR/\${d}backup.tar.xz\" \"\$REMOTEUSER@\$REMOTEHOST:\$REMOTEPATH\""
  113. echo "rm -f \"\$TMPDIR/\${d}backup.tar.xz\""
  114. } >> /etc/cron.daily/backup_sh
  115. chmod +x /etc/cron.daily/backup_sh
  116. if [[ -f /lib/systemd/system/cron.service ]]; then
  117. systemctl restart cron
  118. elif [[ -f /lib/systemd/system/crond.service ]]; then
  119. systemctl restart crond
  120. else
  121. echo -e "\\e[31;1mWarning! You must restart cron!\n\\e[0m"
  122. fi
  123. if [[ $TARCHS == "1" ]]; then
  124. echo -e "\\e[32;1mAdd crontab task on the remote computer like that:\n\\e[31;1m 0 1 * * * find $REMOTEPATH -mtime +7 -exec rm {} \\;\n\\e[32;1mOr copy file ./clearbckp.sh on the remote computer and add to crontab tasks.\\e[0m\n\n"
  125. else
  126. echo "On the remote computer you not need delete any archives. You should not adding task for cron."
  127. fi
  128. exit
  129. elif [[ $1 == "-f" || $1 == "--first" ]]; then
  130. tar -cvf - "$TARGETS" | xz -9 --threads=0 - > "first-backup.tar.xz"
  131. elif [[ ! -z $1 ]]; then
  132. echo "Usage: backup.sh -c
  133. backup.sh --config
  134. "
  135. exit
  136. else
  137. # Загоняем текущую дату в переменную
  138. d=$(date +%F)
  139. if [[ $TARCHS == "1" ]]; then
  140. # Упаковываем файлы и прочее в TAR и XZ с максимальным сжатием
  141. tar -cvf - "$TARGETS" | xz -9 --threads=0 - > "$TMPDIR/${d}backup.tar.xz"
  142. # Отправляем на удалённую машину
  143. elif [[ $TARCHS == "2" ]];then
  144. LIST=""
  145. for i in $TARGETS
  146. do
  147. L=$(find "$i" -type f -mtime -1)
  148. LIST="$LIST
  149. $L"
  150. done
  151. LIST=$(echo "$LIST" | tr "\n" " ")
  152. CMD="tar -cvf - ${LIST:1:-1}"
  153. $CMD | xz -9 --threads=0 - > "$TMPDIR/${d}backup.tar.xz"
  154. else
  155. exit
  156. fi
  157. # Отправляем на удалённую машину
  158. scp -B "$TMPDIR/${d}backup.tar.xz" "$REMOTEUSER@$REMOTEHOST:$REMOTEPATH"
  159. # Удаляем архив, чтобы не занимать пространство на диске без пользы
  160. rm -f "$TMPDIR/${d}backup.tar.xz"
  161. fi