Created
May 29, 2023 06:27
-
-
Save dynax60/98a8adbc9966b99409125da3a60d6234 to your computer and use it in GitHub Desktop.
Simple site change tracker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PATH=/usr/bin:/usr/sbin | |
# Установка переменных | |
SITE_DIRECTORY="/var/www/mysite.ru" # Путь к директории сайта | |
BACKUP_DIRECTORY="/opt/git/site" # Путь к директории для сохранения бэкапов | |
EMAIL_RECIPIENT="mysite@mysite.ru" # Адрес получателя электронной почты | |
MAX_DIFF_SIZE=$((10 * 1024)) # 10 Кб в байтах | |
# Создание временной директории для выполнения операций | |
TEMP_DIRECTORY=$(mktemp -d) | |
# Выполнение резервного копирования с использованием rsync | |
rsync -av --exclude-from=$(dirname $0)/rsync.exclude --delete "$SITE_DIRECTORY" "$BACKUP_DIRECTORY" | |
# Проверка изменений с помощью Git | |
cd "$BACKUP_DIRECTORY" | |
git init | |
git config user.name "Backup script" | |
git config user.email $EMAIL_RECIPIENT | |
git add -A | |
git diff --quiet HEAD . # Проверка изменений между текущим состоянием и бэкапом | |
DIFF_RESULT=$? | |
# Если есть различия, создание и отправка diff-файла по почте | |
if [ $DIFF_RESULT -ne 0 ]; then | |
DIFF_FILE="$TEMP_DIRECTORY/backup-diff-$(date +%Y%m%d%H%M%S).diff" | |
git diff HEAD . > "$DIFF_FILE" | |
file_size=$(stat -c "%s" "$DIFF_FILE") | |
if [ "$file_size" -gt "$MAX_DIFF_SIZE" ]; then | |
# Если размер файла больше $MAX_DIFF_SIZE, обрезаем его до $MAX_DIFF_SIZE | |
truncate -s "$MAX_DIFF_SIZE" "$DIFF_FILE" | |
echo "..." >> $DIFF_FILE | |
fi | |
echo -e "To: $EMAIL_RECIPIENT\nSubject: Изменения сайта mysite.ru\n\n$(cat $DIFF_FILE)" | sendmail -t | |
#echo "Различия между текущим состоянием и бэкапом: " | sendmail -s "Изменения сайта" -a "$DIFF_FILE" "$EMAIL_RECIPIENT" | |
fi | |
git commit -m "Daily backup $(date +%Y-%m-%d)" | |
# Очистка временной директории | |
rm -rf "$TEMP_DIRECTORY" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.git | |
bitrix/backup | |
bitrix/cache | |
bitrix/managed_cache | |
bitrix/managed_flags | |
bitrix/modules/*.log | |
bitrix/php_interface/crontab | |
bitrix/php_interface/dbconn.php | |
bitrix/.settings.php | |
bitrix/stack_cache | |
bitrix/tmp | |
logs | |
.idea/ | |
.vscode/ | |
.sass-cache/ | |
.cache/ | |
.local/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment