Updated the script so it now it checks if a new version of gitea is available before running and will only run the script if there is.

This commit is contained in:
2026-09-25 22:18:05 +01:00
parent 099fe25504
commit f5c61c051a
2 changed files with 276 additions and 140 deletions
-4
View File
@@ -86,12 +86,8 @@ bash
ENABLE_SLACK="false" ENABLE_SLACK="false"
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/YOUR/WEBHOOK/URL" SLACK_WEBHOOK_URL="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
Notification Setup
Email Notifications
Option 1: Using msmtp (Recommended)
``` ```
### Usage ### Usage
Manual Execution Manual Execution
+260 -120
View File
@@ -1,7 +1,6 @@
#!/bin/bash #!/bin/bash
# Gitea Backup and Update Script with Notifications # Gitea Backup and Update Script with Notifications
# Supports email and Discord/Slack webhook notifications
set -e set -e
@@ -11,30 +10,27 @@ GITEA_USER="git"
GITEA_BIN="/usr/local/bin/gitea" GITEA_BIN="/usr/local/bin/gitea"
BACKUP_DIR="/var/backups/gitea" BACKUP_DIR="/var/backups/gitea"
LOG_FILE="/var/log/gitea-backup-update.log" LOG_FILE="/var/log/gitea-backup-update.log"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S") TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_NAME="gitea_backup_${TIMESTAMP}" BACKUP_NAME="gitea_backup_${TIMESTAMP}"
HOSTNAME=$(hostname) HOSTNAME=$(hostname)
# Notification Settings # Notification Settings
# Email notifications # Email notifications
ENABLE_EMAIL="false" ENABLE_EMAIL="false"
EMAIL_FROM="[email protected]" EMAIL_FROM="[email protected]"
EMAIL_TO="[email protected]" EMAIL_TO="[email protected]"
SMTP_SERVER="smtp.gmail.com"
SMTP_PORT="587"
SMTP_USER="[email protected]"
SMTP_PASS="your-app-password"
# Discord webhook # Discord webhook
ENABLE_DISCORD="false" ENABLE_DISCORD="false"
DISCORD_WEBHOOK_URL="https://discordapp.com/api/webhooks/YOUR_WEBHOOK_ID/YOUR_WEBHOOK_TOKEN" DISCORD_WEBHOOK_URL="https://discordapp.com/api/webhooks/YOUR_WEBHOOK_ID/YOUR_WEBHOOK_TOKEN"
# Slack webhook # Slack webhook
ENABLE_SLACK="false" ENABLE_SLACK="false"
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/YOUR/WEBHOOK/URL" SLACK_WEBHOOK_URL="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
# Colors for output # Colors
RED='\033[0;31m' RED='\033[0;31m'
GREEN='\033[0;32m' GREEN='\033[0;32m'
YELLOW='\033[1;33m' YELLOW='\033[1;33m'
@@ -46,21 +42,18 @@ STATUS="RUNNING"
STATUS_MESSAGE="" STATUS_MESSAGE=""
START_TIME=$(date +%s) START_TIME=$(date +%s)
# Logging function # ------------------------------------------------------------
# Logging
# ------------------------------------------------------------
log() { log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
} }
# Error handler # ------------------------------------------------------------
error_exit() { # Notifications
log "${RED}ERROR: $1${NC}" # ------------------------------------------------------------
STATUS="FAILED"
STATUS_MESSAGE="$1"
send_notifications
exit 1
}
# Send email notification
send_email() { send_email() {
local subject="$1" local subject="$1"
local body="$2" local body="$2"
@@ -71,15 +64,20 @@ send_email() {
log "Sending email notification..." log "Sending email notification..."
# Using msmtp (lightweight SMTP client) if command -v msmtp >/dev/null 2>&1; then
if command -v msmtp &> /dev/null; then {
echo "$body" | msmtp -a default "$EMAIL_TO" echo "To: $EMAIL_TO"
echo "From: $EMAIL_FROM"
echo "Subject: $subject"
echo ""
echo "$body"
} | msmtp "$EMAIL_TO"
log "${GREEN}Email sent successfully${NC}" log "${GREEN}Email sent successfully${NC}"
return return
fi fi
# Fallback to ssmtp if command -v ssmtp >/dev/null 2>&1; then
if command -v ssmtp &> /dev/null; then
{ {
echo "To: $EMAIL_TO" echo "To: $EMAIL_TO"
echo "From: $EMAIL_FROM" echo "From: $EMAIL_FROM"
@@ -87,44 +85,51 @@ send_email() {
echo "" echo ""
echo "$body" echo "$body"
} | ssmtp "$EMAIL_TO" } | ssmtp "$EMAIL_TO"
log "${GREEN}Email sent successfully${NC}" log "${GREEN}Email sent successfully${NC}"
return return
fi fi
# Fallback to mail command if command -v mail >/dev/null 2>&1; then
if command -v mail &> /dev/null; then
echo "$body" | mail -s "$subject" "$EMAIL_TO" echo "$body" | mail -s "$subject" "$EMAIL_TO"
log "${GREEN}Email sent successfully${NC}" log "${GREEN}Email sent successfully${NC}"
return return
fi fi
log "${YELLOW}Warning: No mail client found. Install msmtp, ssmtp, or mailutils${NC}" log "${YELLOW}Warning: No mail client found${NC}"
} }
# Send Discord notification
send_discord() { send_discord() {
local status_color="" if [ "$ENABLE_DISCORD" != "true" ] || [ -z "$DISCORD_WEBHOOK_URL" ]; then
local status_emoji="" return
fi
local status_color
local status_emoji
case "$STATUS" in case "$STATUS" in
"RUNNING") RUNNING)
status_color="3447525" # Blue status_color="3447525"
status_emoji="⏳" status_emoji="⏳"
;; ;;
"SUCCESS") SUCCESS)
status_color="3066993" # Green status_color="3066993"
status_emoji="✅" status_emoji="✅"
;; ;;
"FAILED") FAILED)
status_color="15158332" # Red status_color="15158332"
status_emoji="❌" status_emoji="❌"
;; ;;
esac esac
local duration=$(($(date +%s) - START_TIME)) local duration
local duration_formatted="$((duration / 60))m $((duration % 60))s" local duration_formatted
local payload=$(cat <<EOF duration=$(($(date +%s) - START_TIME))
duration_formatted="$((duration / 60))m $((duration % 60))s"
local payload
payload=$(cat <<EOF
{ {
"embeds": [{ "embeds": [{
"title": "$status_emoji Gitea Backup & Update - $STATUS", "title": "$status_emoji Gitea Backup & Update - $STATUS",
@@ -152,7 +157,7 @@ send_discord() {
}, },
{ {
"name": "Backup Location", "name": "Backup Location",
"value": "\`$BACKUP_DIR\`", "value": "$BACKUP_DIR",
"inline": false "inline": false
} }
], ],
@@ -164,37 +169,45 @@ send_discord() {
EOF EOF
) )
if [ "$ENABLE_DISCORD" = "true" ] && [ -n "$DISCORD_WEBHOOK_URL" ]; then curl --silent --show-error \
curl -s -X POST "$DISCORD_WEBHOOK_URL" \ --request POST "$DISCORD_WEBHOOK_URL" \
-H "Content-Type: application/json" \ --header "Content-Type: application/json" \
-d "$payload" > /dev/null 2>&1 || log "${YELLOW}Warning: Discord notification failed${NC}" --data "$payload" \
fi >/dev/null 2>&1 \
|| log "${YELLOW}Warning: Discord notification failed${NC}"
} }
# Send Slack notification
send_slack() { send_slack() {
local status_color="" if [ "$ENABLE_SLACK" != "true" ] || [ -z "$SLACK_WEBHOOK_URL" ]; then
local status_emoji="" return
fi
local status_color
local status_emoji
case "$STATUS" in case "$STATUS" in
"RUNNING") RUNNING)
status_color="warning" status_color="warning"
status_emoji="⏳" status_emoji="⏳"
;; ;;
"SUCCESS") SUCCESS)
status_color="good" status_color="good"
status_emoji="✅" status_emoji="✅"
;; ;;
"FAILED") FAILED)
status_color="danger" status_color="danger"
status_emoji="❌" status_emoji="❌"
;; ;;
esac esac
local duration=$(($(date +%s) - START_TIME)) local duration
local duration_formatted="$((duration / 60))m $((duration % 60))s" local duration_formatted
local payload=$(cat <<EOF duration=$(($(date +%s) - START_TIME))
duration_formatted="$((duration / 60))m $((duration % 60))s"
local payload
payload=$(cat <<EOF
{ {
"attachments": [{ "attachments": [{
"color": "$status_color", "color": "$status_color",
@@ -231,19 +244,23 @@ send_slack() {
EOF EOF
) )
if [ "$ENABLE_SLACK" = "true" ] && [ -n "$SLACK_WEBHOOK_URL" ]; then curl --silent --show-error \
curl -s -X POST "$SLACK_WEBHOOK_URL" \ --request POST "$SLACK_WEBHOOK_URL" \
-H "Content-Type: application/json" \ --header "Content-Type: application/json" \
-d "$payload" > /dev/null 2>&1 || log "${YELLOW}Warning: Slack notification failed${NC}" --data "$payload" \
fi >/dev/null 2>&1 \
|| log "${YELLOW}Warning: Slack notification failed${NC}"
} }
# Send all notifications
send_notifications() { send_notifications() {
log "Sending notifications..." log "Sending notifications..."
local subject="Gitea Backup & Update - $STATUS" local subject
local body="Gitea Backup and Update Script Results local body
subject="Gitea Backup & Update - $STATUS"
body="Gitea Backup and Update Script Results
Server: $HOSTNAME Server: $HOSTNAME
Status: $STATUS Status: $STATUS
@@ -261,59 +278,99 @@ Backup Location: $BACKUP_DIR"
send_slack send_slack
} }
# Check if running as root error_exit() {
log "${RED}ERROR: $1${NC}"
STATUS="FAILED"
STATUS_MESSAGE="$1"
send_notifications
exit 1
}
# ------------------------------------------------------------
# Initial checks
# ------------------------------------------------------------
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
error_exit "This script must be run as root" STATUS="FAILED"
STATUS_MESSAGE="This script must be run as root"
echo "$STATUS_MESSAGE"
exit 1
fi fi
if [ ! -x "$GITEA_BIN" ]; then
error_exit "Gitea binary not found or is not executable: $GITEA_BIN"
fi
if ! command -v curl >/dev/null 2>&1; then
error_exit "curl is required but was not found"
fi
mkdir -p "$(dirname "$LOG_FILE")"
log "${GREEN}=== Gitea Backup and Update Started ===${NC}" log "${GREEN}=== Gitea Backup and Update Started ===${NC}"
STATUS="RUNNING" STATUS="RUNNING"
send_discord send_discord
# Create backup directory if it doesn't exist # ------------------------------------------------------------
mkdir -p "$BACKUP_DIR" # Check installed and latest versions
# ------------------------------------------------------------
# Step 1: Stop Gitea service log "Checking installed Gitea version..."
log "Stopping Gitea service..."
systemctl stop gitea || error_exit "Failed to stop Gitea service"
sleep 2
# Step 2: Backup Gitea data INSTALLED_VERSION=$(
log "Creating backup of Gitea data..." "$GITEA_BIN" --version 2>/dev/null |
mkdir -p "$BACKUP_DIR/$BACKUP_NAME" grep -oE 'version [0-9]+\.[0-9]+\.[0-9]+' |
awk '{print $2}' |
head -n 1
)
# Backup the main Gitea directory if [ -z "$INSTALLED_VERSION" ]; then
cp -r "$GITEA_HOME" "$BACKUP_DIR/$BACKUP_NAME/gitea_home" || error_exit "Failed to backup Gitea home directory" error_exit "Could not determine installed Gitea version"
fi
# Backup the binary log "Checking latest Gitea release..."
cp "$GITEA_BIN" "$BACKUP_DIR/$BACKUP_NAME/gitea_bin_backup" || error_exit "Failed to backup Gitea binary"
log "${GREEN}Backup created at: $BACKUP_DIR/$BACKUP_NAME${NC}" LATEST_VERSION=$(
STATUS_MESSAGE="Backup created successfully" curl --fail --silent --show-error \
send_discord https://api.github.com/repos/go-gitea/gitea/releases/latest |
grep -oP '"tag_name":\s*"\K[^"]+' |
# Step 3: Compress backup head -n 1
log "Compressing backup..." )
cd "$BACKUP_DIR"
tar -czf "${BACKUP_NAME}.tar.gz" "$BACKUP_NAME" || error_exit "Failed to compress backup"
rm -rf "$BACKUP_DIR/$BACKUP_NAME"
log "${GREEN}Backup compressed: ${BACKUP_NAME}.tar.gz${NC}"
# Step 4: Download latest Gitea release
log "Downloading latest Gitea release..."
LATEST_VERSION=$(curl -s https://api.github.com/repos/go-gitea/gitea/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")')
if [ -z "$LATEST_VERSION" ]; then if [ -z "$LATEST_VERSION" ]; then
error_exit "Failed to fetch latest Gitea version" error_exit "Failed to fetch latest Gitea version"
fi fi
log "Latest version: $LATEST_VERSION" LATEST_VERSION_NUMBER="${LATEST_VERSION#v}"
STATUS_MESSAGE="Fetched latest version: $LATEST_VERSION"
log "Installed version: $INSTALLED_VERSION"
log "Latest version: $LATEST_VERSION_NUMBER"
# Exit before stopping Gitea or creating a backup if already current
if [ "$INSTALLED_VERSION" = "$LATEST_VERSION_NUMBER" ]; then
STATUS="SUCCESS"
STATUS_MESSAGE="Gitea is already up to date at version $INSTALLED_VERSION"
log "${GREEN}$STATUS_MESSAGE${NC}"
send_notifications
exit 0
fi
log "${YELLOW}Update available: $INSTALLED_VERSION -> $LATEST_VERSION_NUMBER${NC}"
STATUS_MESSAGE="Update available: $INSTALLED_VERSION -> $LATEST_VERSION_NUMBER"
send_discord send_discord
# Determine system architecture # ------------------------------------------------------------
# Determine architecture
# ------------------------------------------------------------
ARCH=$(uname -m) ARCH=$(uname -m)
case $ARCH in
case "$ARCH" in
x86_64) x86_64)
DOWNLOAD_ARCH="amd64" DOWNLOAD_ARCH="amd64"
;; ;;
@@ -328,42 +385,125 @@ case $ARCH in
;; ;;
esac esac
DOWNLOAD_URL="https://github.com/go-gitea/gitea/releases/download/${LATEST_VERSION}/gitea-${LATEST_VERSION#v}-linux-${DOWNLOAD_ARCH}" DOWNLOAD_URL="https://github.com/go-gitea/gitea/releases/download/${LATEST_VERSION}/gitea-${LATEST_VERSION_NUMBER}-linux-${DOWNLOAD_ARCH}"
log "Downloading from: $DOWNLOAD_URL" # ------------------------------------------------------------
curl -L -o /tmp/gitea_new "$DOWNLOAD_URL" || error_exit "Failed to download Gitea" # Stop Gitea
# ------------------------------------------------------------
# Step 5: Verify and install new binary log "Stopping Gitea service..."
log "Installing new Gitea binary..."
chmod +x /tmp/gitea_new
cp /tmp/gitea_new "$GITEA_BIN" || error_exit "Failed to install new binary"
chown "$GITEA_USER:$GITEA_USER" "$GITEA_BIN"
rm /tmp/gitea_new
log "${GREEN}Gitea binary updated to $LATEST_VERSION${NC}" systemctl stop gitea ||
STATUS_MESSAGE="Gitea updated to $LATEST_VERSION" error_exit "Failed to stop Gitea service"
sleep 2
# ------------------------------------------------------------
# Create backup
# ------------------------------------------------------------
log "Creating backup of Gitea data..."
mkdir -p "$BACKUP_DIR/$BACKUP_NAME"
cp -r "$GITEA_HOME" "$BACKUP_DIR/$BACKUP_NAME/gitea_home" ||
error_exit "Failed to backup Gitea home directory"
cp "$GITEA_BIN" "$BACKUP_DIR/$BACKUP_NAME/gitea_bin_backup" ||
error_exit "Failed to backup Gitea binary"
log "${GREEN}Backup created at: $BACKUP_DIR/$BACKUP_NAME${NC}"
STATUS_MESSAGE="Backup created successfully"
send_discord send_discord
# Step 6: Start Gitea service # ------------------------------------------------------------
log "Starting Gitea service..." # Compress backup
systemctl start gitea || error_exit "Failed to start Gitea service" # ------------------------------------------------------------
sleep 3
# Step 7: Verify service is running log "Compressing backup..."
if systemctl is-active --quiet gitea; then
log "${GREEN}=== Gitea service is running ===${NC}" cd "$BACKUP_DIR"
else
error_exit "Gitea service failed to start. Check logs for details." tar -czf "${BACKUP_NAME}.tar.gz" "$BACKUP_NAME" ||
error_exit "Failed to compress backup"
rm -rf "$BACKUP_DIR/$BACKUP_NAME"
log "${GREEN}Backup compressed: $BACKUP_DIR/${BACKUP_NAME}.tar.gz${NC}"
# ------------------------------------------------------------
# Download and install Gitea
# ------------------------------------------------------------
log "Downloading Gitea $LATEST_VERSION_NUMBER..."
log "Download URL: $DOWNLOAD_URL"
TMP_BINARY=$(mktemp /tmp/gitea_new.XXXXXX)
curl --fail --location --silent --show-error \
--output "$TMP_BINARY" \
"$DOWNLOAD_URL" || {
rm -f "$TMP_BINARY"
error_exit "Failed to download Gitea"
}
if [ ! -s "$TMP_BINARY" ]; then
rm -f "$TMP_BINARY"
error_exit "Downloaded Gitea binary is empty"
fi fi
# Step 8: Cleanup old backups (keep only last 5) log "Installing new Gitea binary..."
log "Cleaning up old backups (keeping last 5)..."
chmod 755 "$TMP_BINARY"
chown "$GITEA_USER:$GITEA_USER" "$TMP_BINARY"
mv "$TMP_BINARY" "$GITEA_BIN" ||
error_exit "Failed to install new Gitea binary"
log "${GREEN}Gitea binary updated to $LATEST_VERSION_NUMBER${NC}"
STATUS_MESSAGE="Gitea updated from $INSTALLED_VERSION to $LATEST_VERSION_NUMBER"
send_discord
# ------------------------------------------------------------
# Start and verify Gitea
# ------------------------------------------------------------
log "Starting Gitea service..."
systemctl start gitea ||
error_exit "Failed to start Gitea service"
sleep 3
if systemctl is-active --quiet gitea; then
log "${GREEN}Gitea service is running${NC}"
else
error_exit "Gitea service failed to start. Check the service logs."
fi
# ------------------------------------------------------------
# Clean up old backups
# ------------------------------------------------------------
log "Cleaning up old backups; keeping the last five..."
cd "$BACKUP_DIR" cd "$BACKUP_DIR"
ls -t *.tar.gz 2>/dev/null | tail -n +6 | xargs -r rm
log "${GREEN}Cleanup completed${NC}" ls -t *.tar.gz 2>/dev/null |
tail -n +6 |
xargs -r rm --
log "${GREEN}Backup cleanup completed${NC}"
# ------------------------------------------------------------
# Finish
# ------------------------------------------------------------
log "${GREEN}=== Gitea Backup and Update Completed Successfully ===${NC}" log "${GREEN}=== Gitea Backup and Update Completed Successfully ===${NC}"
STATUS="SUCCESS"
STATUS_MESSAGE="Backup completed and Gitea updated to $LATEST_VERSION successfully"
send_notifications
STATUS="SUCCESS"
STATUS_MESSAGE="Backup completed and Gitea updated from $INSTALLED_VERSION to $LATEST_VERSION_NUMBER successfully"
send_notifications