Added first version of script and updated README.md
This commit is contained in:
+369
@@ -0,0 +1,369 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Gitea Backup and Update Script with Notifications
|
||||
# Supports email and Discord/Slack webhook notifications
|
||||
|
||||
set -e
|
||||
|
||||
# Configuration
|
||||
GITEA_HOME="/var/lib/gitea"
|
||||
GITEA_USER="git"
|
||||
GITEA_BIN="/usr/local/bin/gitea"
|
||||
BACKUP_DIR="/var/backups/gitea"
|
||||
LOG_FILE="/var/log/gitea-backup-update.log"
|
||||
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
|
||||
BACKUP_NAME="gitea_backup_${TIMESTAMP}"
|
||||
HOSTNAME=$(hostname)
|
||||
|
||||
# Notification Settings
|
||||
# Email notifications
|
||||
ENABLE_EMAIL="false"
|
||||
EMAIL_FROM="[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
|
||||
ENABLE_DISCORD="false"
|
||||
DISCORD_WEBHOOK_URL="https://discordapp.com/api/webhooks/YOUR_WEBHOOK_ID/YOUR_WEBHOOK_TOKEN"
|
||||
|
||||
|
||||
# Slack webhook
|
||||
ENABLE_SLACK="false"
|
||||
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m'
|
||||
|
||||
# Status tracking
|
||||
STATUS="RUNNING"
|
||||
STATUS_MESSAGE=""
|
||||
START_TIME=$(date +%s)
|
||||
|
||||
# Logging function
|
||||
log() {
|
||||
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
|
||||
}
|
||||
|
||||
# Error handler
|
||||
error_exit() {
|
||||
log "${RED}ERROR: $1${NC}"
|
||||
STATUS="FAILED"
|
||||
STATUS_MESSAGE="$1"
|
||||
send_notifications
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Send email notification
|
||||
send_email() {
|
||||
local subject="$1"
|
||||
local body="$2"
|
||||
|
||||
if [ "$ENABLE_EMAIL" != "true" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
log "Sending email notification..."
|
||||
|
||||
# Using msmtp (lightweight SMTP client)
|
||||
if command -v msmtp &> /dev/null; then
|
||||
echo "$body" | msmtp -a default "$EMAIL_TO"
|
||||
log "${GREEN}Email sent successfully${NC}"
|
||||
return
|
||||
fi
|
||||
|
||||
# Fallback to ssmtp
|
||||
if command -v ssmtp &> /dev/null; then
|
||||
{
|
||||
echo "To: $EMAIL_TO"
|
||||
echo "From: $EMAIL_FROM"
|
||||
echo "Subject: $subject"
|
||||
echo ""
|
||||
echo "$body"
|
||||
} | ssmtp "$EMAIL_TO"
|
||||
log "${GREEN}Email sent successfully${NC}"
|
||||
return
|
||||
fi
|
||||
|
||||
# Fallback to mail command
|
||||
if command -v mail &> /dev/null; then
|
||||
echo "$body" | mail -s "$subject" "$EMAIL_TO"
|
||||
log "${GREEN}Email sent successfully${NC}"
|
||||
return
|
||||
fi
|
||||
|
||||
log "${YELLOW}Warning: No mail client found. Install msmtp, ssmtp, or mailutils${NC}"
|
||||
}
|
||||
|
||||
# Send Discord notification
|
||||
send_discord() {
|
||||
local status_color=""
|
||||
local status_emoji=""
|
||||
|
||||
case "$STATUS" in
|
||||
"RUNNING")
|
||||
status_color="3447525" # Blue
|
||||
status_emoji="⏳"
|
||||
;;
|
||||
"SUCCESS")
|
||||
status_color="3066993" # Green
|
||||
status_emoji="✅"
|
||||
;;
|
||||
"FAILED")
|
||||
status_color="15158332" # Red
|
||||
status_emoji="❌"
|
||||
;;
|
||||
esac
|
||||
|
||||
local duration=$(($(date +%s) - START_TIME))
|
||||
local duration_formatted="$((duration / 60))m $((duration % 60))s"
|
||||
|
||||
local payload=$(cat <<EOF
|
||||
{
|
||||
"embeds": [{
|
||||
"title": "$status_emoji Gitea Backup & Update - $STATUS",
|
||||
"color": $status_color,
|
||||
"fields": [
|
||||
{
|
||||
"name": "Server",
|
||||
"value": "$HOSTNAME",
|
||||
"inline": true
|
||||
},
|
||||
{
|
||||
"name": "Duration",
|
||||
"value": "$duration_formatted",
|
||||
"inline": true
|
||||
},
|
||||
{
|
||||
"name": "Timestamp",
|
||||
"value": "$(date +'%Y-%m-%d %H:%M:%S')",
|
||||
"inline": false
|
||||
},
|
||||
{
|
||||
"name": "Status Message",
|
||||
"value": "${STATUS_MESSAGE:-Operation completed}",
|
||||
"inline": false
|
||||
},
|
||||
{
|
||||
"name": "Backup Location",
|
||||
"value": "\`$BACKUP_DIR\`",
|
||||
"inline": false
|
||||
}
|
||||
],
|
||||
"footer": {
|
||||
"text": "Gitea Automation"
|
||||
}
|
||||
}]
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
if [ "$ENABLE_DISCORD" = "true" ] && [ -n "$DISCORD_WEBHOOK_URL" ]; then
|
||||
curl -s -X POST "$DISCORD_WEBHOOK_URL" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$payload" > /dev/null 2>&1 || log "${YELLOW}Warning: Discord notification failed${NC}"
|
||||
fi
|
||||
}
|
||||
|
||||
# Send Slack notification
|
||||
send_slack() {
|
||||
local status_color=""
|
||||
local status_emoji=""
|
||||
|
||||
case "$STATUS" in
|
||||
"RUNNING")
|
||||
status_color="warning"
|
||||
status_emoji="⏳"
|
||||
;;
|
||||
"SUCCESS")
|
||||
status_color="good"
|
||||
status_emoji="✅"
|
||||
;;
|
||||
"FAILED")
|
||||
status_color="danger"
|
||||
status_emoji="❌"
|
||||
;;
|
||||
esac
|
||||
|
||||
local duration=$(($(date +%s) - START_TIME))
|
||||
local duration_formatted="$((duration / 60))m $((duration % 60))s"
|
||||
|
||||
local payload=$(cat <<EOF
|
||||
{
|
||||
"attachments": [{
|
||||
"color": "$status_color",
|
||||
"title": "$status_emoji Gitea Backup & Update - $STATUS",
|
||||
"fields": [
|
||||
{
|
||||
"title": "Server",
|
||||
"value": "$HOSTNAME",
|
||||
"short": true
|
||||
},
|
||||
{
|
||||
"title": "Duration",
|
||||
"value": "$duration_formatted",
|
||||
"short": true
|
||||
},
|
||||
{
|
||||
"title": "Timestamp",
|
||||
"value": "$(date +'%Y-%m-%d %H:%M:%S')",
|
||||
"short": false
|
||||
},
|
||||
{
|
||||
"title": "Status Message",
|
||||
"value": "${STATUS_MESSAGE:-Operation completed}",
|
||||
"short": false
|
||||
},
|
||||
{
|
||||
"title": "Backup Location",
|
||||
"value": "$BACKUP_DIR",
|
||||
"short": false
|
||||
}
|
||||
]
|
||||
}]
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
if [ "$ENABLE_SLACK" = "true" ] && [ -n "$SLACK_WEBHOOK_URL" ]; then
|
||||
curl -s -X POST "$SLACK_WEBHOOK_URL" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$payload" > /dev/null 2>&1 || log "${YELLOW}Warning: Slack notification failed${NC}"
|
||||
fi
|
||||
}
|
||||
|
||||
# Send all notifications
|
||||
send_notifications() {
|
||||
log "Sending notifications..."
|
||||
|
||||
local subject="Gitea Backup & Update - $STATUS"
|
||||
local body="Gitea Backup and Update Script Results
|
||||
|
||||
Server: $HOSTNAME
|
||||
Status: $STATUS
|
||||
Duration: $(($(date +%s) - START_TIME)) seconds
|
||||
Timestamp: $(date +'%Y-%m-%d %H:%M:%S')
|
||||
Log File: $LOG_FILE
|
||||
|
||||
Details:
|
||||
$STATUS_MESSAGE
|
||||
|
||||
Backup Location: $BACKUP_DIR"
|
||||
|
||||
send_email "$subject" "$body"
|
||||
send_discord
|
||||
send_slack
|
||||
}
|
||||
|
||||
# Check if running as root
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
error_exit "This script must be run as root"
|
||||
fi
|
||||
|
||||
log "${GREEN}=== Gitea Backup and Update Started ===${NC}"
|
||||
STATUS="RUNNING"
|
||||
send_discord
|
||||
|
||||
# Create backup directory if it doesn't exist
|
||||
mkdir -p "$BACKUP_DIR"
|
||||
|
||||
# Step 1: Stop Gitea service
|
||||
log "Stopping Gitea service..."
|
||||
systemctl stop gitea || error_exit "Failed to stop Gitea service"
|
||||
sleep 2
|
||||
|
||||
# Step 2: Backup Gitea data
|
||||
log "Creating backup of Gitea data..."
|
||||
mkdir -p "$BACKUP_DIR/$BACKUP_NAME"
|
||||
|
||||
# Backup the main Gitea directory
|
||||
cp -r "$GITEA_HOME" "$BACKUP_DIR/$BACKUP_NAME/gitea_home" || error_exit "Failed to backup Gitea home directory"
|
||||
|
||||
# Backup the binary
|
||||
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
|
||||
|
||||
# Step 3: Compress backup
|
||||
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
|
||||
error_exit "Failed to fetch latest Gitea version"
|
||||
fi
|
||||
|
||||
log "Latest version: $LATEST_VERSION"
|
||||
STATUS_MESSAGE="Fetched latest version: $LATEST_VERSION"
|
||||
send_discord
|
||||
|
||||
# Determine system architecture
|
||||
ARCH=$(uname -m)
|
||||
case $ARCH in
|
||||
x86_64)
|
||||
DOWNLOAD_ARCH="amd64"
|
||||
;;
|
||||
aarch64)
|
||||
DOWNLOAD_ARCH="arm64"
|
||||
;;
|
||||
armv7l)
|
||||
DOWNLOAD_ARCH="armv7"
|
||||
;;
|
||||
*)
|
||||
error_exit "Unsupported architecture: $ARCH"
|
||||
;;
|
||||
esac
|
||||
|
||||
DOWNLOAD_URL="https://github.com/go-gitea/gitea/releases/download/${LATEST_VERSION}/gitea-${LATEST_VERSION#v}-linux-${DOWNLOAD_ARCH}"
|
||||
|
||||
log "Downloading from: $DOWNLOAD_URL"
|
||||
curl -L -o /tmp/gitea_new "$DOWNLOAD_URL" || error_exit "Failed to download Gitea"
|
||||
|
||||
# Step 5: Verify and install new binary
|
||||
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}"
|
||||
STATUS_MESSAGE="Gitea updated to $LATEST_VERSION"
|
||||
send_discord
|
||||
|
||||
# Step 6: Start Gitea service
|
||||
log "Starting Gitea service..."
|
||||
systemctl start gitea || error_exit "Failed to start Gitea service"
|
||||
sleep 3
|
||||
|
||||
# Step 7: Verify service is running
|
||||
if systemctl is-active --quiet gitea; then
|
||||
log "${GREEN}=== Gitea service is running ===${NC}"
|
||||
else
|
||||
error_exit "Gitea service failed to start. Check logs for details."
|
||||
fi
|
||||
|
||||
# Step 8: Cleanup old backups (keep only last 5)
|
||||
log "Cleaning up old backups (keeping last 5)..."
|
||||
cd "$BACKUP_DIR"
|
||||
ls -t *.tar.gz 2>/dev/null | tail -n +6 | xargs -r rm
|
||||
log "${GREEN}Cleanup completed${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
|
||||
|
||||
Reference in New Issue
Block a user