Files

327 lines
7.7 KiB
Markdown

# gitea-updater
A comprehensive bash script for automated backup and updating of Gitea instances on Linux servers with multi-channel notifications.
Features
## Disclaimer - Use at Your Own Risk
### This script is provided as-is without any warranties, guarantees, or support. By using this script, you acknowledge and accept all risks associated with its use.
Features:
* Automated Backups — Creates compressed backups before each update
* Auto-Update Detection — Fetches the latest Gitea release from GitHub
* Architecture Detection — Automatically detects your system (amd64, arm64, armv7)
* Service Management — Gracefully stops and starts Gitea service
* Multi-Channel Notifications — Email, Discord, and Slack alerts
* Status Updates — Real-time progress notifications during execution
* Error Handling — Comprehensive error reporting and automatic rollback
* Backup Retention — Keeps only the last 5 backups to save disk space
* Detailed Logging — All operations logged to file for audit trails
System Requirements
* OS: Linux (Ubuntu, Debian, CentOS, etc.)
* Privileges: Root access (required to manage Gitea service)
Dependencies:
- curl — For downloading Gitea and sending webhooks
- tar — For compressing backups
- systemctl — For service management
- Mail client (optional) — For email notifications: msmtp, ssmtp, or mailutils
- Chat Server (optional) — For IM notifications: Discord, Fluxer, or Slack
### Installation
1. Download the Script
```
sudo curl -o /usr/local/bin/gitea-backup-update.sh https://git.ncltech.co.uk/phil/gitea-updater/raw/branch/main/gitea_update.sh
```
Or create it manually:
```
sudo nano /usr/local/bin/gitea-backup-update.sh
```
Paste the script contents, then save and exit (Ctrl+X, Y, Enter).
2. Make it Executable
```
sudo chmod +x /usr/local/bin/gitea-backup-update.sh
```
3. Configure Variables
Edit the script to customize settings:
```
sudo nano /usr/local/bin/gitea-backup-update.sh
```
- Update these sections:
Gitea Paths:
```
GITEA_HOME="/var/lib/gitea" # Path to Gitea data directory
GITEA_USER="git" # User that runs Gitea
GITEA_BIN="/usr/local/bin/gitea" # Path to Gitea binary
BACKUP_DIR="/var/backups/gitea" # Backup storage location
Email Configuration:
bash
ENABLE_EMAIL="true"
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 Configuration:
bash
ENABLE_DISCORD="true"
DISCORD_WEBHOOK_URL="https://discordapp.com/api/webhooks/YOUR_WEBHOOK_ID/YOUR_WEBHOOK_TOKEN"
Slack Configuration (optional):
bash
ENABLE_SLACK="false"
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
```
### Usage
Manual Execution
* Run the script manually anytime:
```
sudo /usr/local/bin/gitea-backup-update.sh
```
* Automated Scheduling with Cron
Set up automatic backups and updates on a schedule using cron.
Edit the crontab:
```
sudo crontab -e
```
Add one of the following:
- Weekly (every Sunday at 2 AM):
```
0 2 * * 0 /usr/local/bin/gitea-backup-update.sh >> /var/log/gitea-backup-update.log 2>&1
```
- Daily (every day at 3 AM):
```
0 3 * * * /usr/local/bin/gitea-backup-update.sh >> /var/log/gitea-backup-update.log 2>&1
```
- Monthly (first day of each month at 1 AM):
```
0 1 1 * * /usr/local/bin/gitea-backup-update.sh >> /var/log/gitea-backup-update.log 2>&1
```
- Twice weekly (Tuesday and Friday at 2 AM):
```
0 2 * * 2,5 /usr/local/bin/gitea-backup-update.sh >> /var/log/gitea-backup-update.log 2>&1
```
### View Execution Logs
```
sudo tail -f /var/log/gitea-backup-update.log
```
### Script Workflow
The script performs the following steps in order:
| Step | Action | Duration |
|------|--------|----------|
| 1 | Stop Gitea service | ~2 seconds |
| 2 | Create full data backup | Varies with data size |
| 3 | Compress backup | Varies with data size |
| 4 | Fetch latest Gitea version | ~2-3 seconds |
| 5 | Download new binary | ~5-10 seconds |
| 6 | Install and verify binary | ~1 second |
| 7 | Start Gitea service | ~2 seconds |
| 8 | Verify service status | ~1 second |
| 9 | Clean up old backups | ~1 second |
| 10 | Send notifications | ~2-3 seconds |
### Notifications
What Gets Sent
Each notification includes:
* Status — RUNNING, SUCCESS, or FAILED
* Server Name — Hostname of the server
* Duration — Total execution time in minutes and seconds
* Timestamp — Date and time of execution
* Status Message — Detailed information about what happened
* Backup Location — Path where backups are stored
* Version Info — Updated Gitea version (on success)
Discord Example
```
✅ Gitea Backup & Update - SUCCESS
Server: my-server
Duration: 2m 34s
Timestamp: 2024-01-15 02:15:47
Status Message: Backup completed and Gitea updated to v1.21.5 successfully
Backup Location: /var/backups/gitea
```
Email Example:
```
To: [email protected]
Subject: Gitea Backup & Update - SUCCESS
Gitea Backup and Update Script Results
Server: my-server
Status: SUCCESS
Duration: 154 seconds
Timestamp: 2024-01-15 02:15:47
Log File: /var/log/gitea-backup-update.log
Details:
Backup completed and Gitea updated to v1.21.5 successfully
Backup Location: /var/backups/gitea
```
Slack Example:
```
Similar to Discord with formatted blocks and color coding.
Backup Management
Backup Location
By default, backups are stored in:
/var/backups/gitea/
Backup Format
```
Each backup is named with a timestamp:
```
gitea_backup_20240115_021547.tar.gz
```
### Backup Retention
The script automatically keeps only the last 5 backups. Older backups are deleted to save disk space.
To disable auto-cleanup, comment out or remove this section:
```
# Step 8: Cleanup old backups (keep only last 5)
# cd "$BACKUP_DIR"
# ls -t *.tar.gz 2>/dev/null | tail -n +6 | xargs -r rm
```
### Manual Backup Cleanup
List all backups:
```
ls -lh /var/backups/gitea/
```
Delete a specific backup:
```
sudo rm /var/backups/gitea/gitea_backup_20240115_021547.tar.gz
```
### Advanced Configuration
#### Custom Gitea Installation
If your Gitea installation uses different paths, update these variables:
bash
```
# For Docker installations
GITEA_HOME="/data/gitea"
GITEA_BIN="/data/gitea/gitea"
```
```
# For custom installations
GITEA_HOME="/opt/gitea/data"
GITEA_BIN="/opt/gitea/bin/gitea"
```
Database Backups
If Gitea uses PostgreSQL or MySQL, add backup commands:
PostgreSQL:
```
sudo -u postgres pg_dump gitea > "$BACKUP_DIR/$BACKUP_NAME/gitea_db_backup.sql"
```
MySQL:
```
mysqldump -u gitea_user -p gitea_db > "$BACKUP_DIR/$BACKUP_NAME/gitea_db_backup.sql"
```
Add these after Step 2 (backup Gitea data).
Skip Notifications for Specific Channels
Disable email:
```
ENABLE_EMAIL="false"
```
Disable Discord:
```
ENABLE_DISCORD="false"
```
Disable Slack:
```
ENABLE_SLACK="false"
```
Change Backup Retention
Keep last 10 backups instead of 5:
```
ls -t *.tar.gz 2>/dev/null | tail -n +11 | xargs -r rm
```
### Security Considerations
#### Credentials Management
Never hardcode passwords in plaintext — Use environment variables or credential files
Restrict script permissions:
```
sudo chmod 700 /usr/local/bin/gitea-backup-update.sh
sudo chmod 600 ~/.msmtprc
sudo chmod 600 /etc/ssmtp/ssmtp.conf
```
* Use app-specific passwords for email (Gmail App Passwords)
* Rotate webhook URLs regularly if security is compromised
#### Backup Security
Protect backup directory:
```
sudo chmod 700 /var/backups/gitea
sudo chown root:root /var/backups/gitea
```
Store backups offsite (cloud storage, external server)
#### Log Security
Restrict log file access:
```
sudo chmod 600 /var/log/gitea-backup-update.log
```
Monitor logs for errors:
```
sudo tail -f /var/log/gitea-backup-update.log
```
### Future Additions/Enhancements
- Will move config settings to a .env file so that wont be overwriten if the script is updated