Initial commit.
This commit is contained in:
parent
ec54a6aad5
commit
b5d52bd3a3
128
README.md
128
README.md
@ -1,2 +1,128 @@
|
|||||||
# lubelogger-backup
|
# Lubelogger Backup Script
|
||||||
|
|
||||||
|
This script automates the backup process for **Lubelogger**—a self-hosted, open-source vehicle maintenance records and fuel mileage tracker. It includes:
|
||||||
|
- Creating backups of your Lubelogger database.
|
||||||
|
- Storing them in a specified directory.
|
||||||
|
- Notifying external systems like **Uptime Kuma** and **Gotify** about the backup status.
|
||||||
|
|
||||||
|
## Features:
|
||||||
|
- **Automated Backup**: Downloads backups from the Lubelogger API.
|
||||||
|
- **Notifications**: Sends notifications to Uptime Kuma and Gotify about backup success or failure.
|
||||||
|
- **Log Management**: Generates logs for each backup with timestamps.
|
||||||
|
- **Retention**: Automatically deletes old backup and log files after a specified number of days.
|
||||||
|
|
||||||
|
## Prerequisites:
|
||||||
|
- **Lubelogger API**: Make sure your Lubelogger instance is running and the API endpoint for backup generation is accessible.
|
||||||
|
- **Uptime Kuma**: For monitoring the backup process.
|
||||||
|
- **Gotify**: For receiving notifications about backup errors.
|
||||||
|
- **Curl**: This script relies on `curl` to interact with APIs and download backups.
|
||||||
|
- **Bash**: This script is written in bash and requires a Linux or Unix-based system.
|
||||||
|
|
||||||
|
## Setup Instructions:
|
||||||
|
|
||||||
|
### 1. Clone the Repository:
|
||||||
|
Clone this repository to your local machine or server:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://your-gitea-repo-url.git
|
||||||
|
cd lubelogger-backup-script
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Create `.env` File:
|
||||||
|
Create a `.env` file in the same directory as the script to store the necessary configuration:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# .env
|
||||||
|
USERNAME=rootUser
|
||||||
|
PASSWORD=rootPassword
|
||||||
|
BASE_URL=https://lubelogger.mydomain.com
|
||||||
|
|
||||||
|
BACKUP_DIR=$HOME/lubelogger_backups
|
||||||
|
RETENTION_DAYS=7
|
||||||
|
BACKUP_TAG=lubelogger-db1
|
||||||
|
|
||||||
|
# Uptime Kuma settings (only the hostname is required)
|
||||||
|
UPTIME_KUMA_URL=https://uptime-kuma.example.com
|
||||||
|
UPTIME_KUMA_API_KEY=your-uptime-kuma-api-key
|
||||||
|
|
||||||
|
# Gotify settings (only the hostname is required)
|
||||||
|
GOTIFY_PUSH_URL=https://push.example.de
|
||||||
|
GOTIFY_PUSH_KEY=your-app-token
|
||||||
|
```
|
||||||
|
|
||||||
|
- **USERNAME**: The root user for the Lubelogger API.
|
||||||
|
- **PASSWORD**: The root password for the Lubelogger API.
|
||||||
|
- **BASE_URL**: The base URL for your Lubelogger instance.
|
||||||
|
- **BACKUP_DIR**: The directory where backup files will be stored.
|
||||||
|
- **RETENTION_DAYS**: The number of days to retain backups before deleting them.
|
||||||
|
- **BACKUP_TAG**: A custom tag for identifying your backups.
|
||||||
|
- **UPTIME_KUMA_URL**: The hostname for your Uptime Kuma instance (e.g., `https://uptime-kuma.example.com`).
|
||||||
|
- **UPTIME_KUMA_API_KEY**: Your Uptime Kuma API key.
|
||||||
|
- **GOTIFY_PUSH_URL**: The hostname for your Gotify instance (e.g., `https://push.example.de`).
|
||||||
|
- **GOTIFY_PUSH_KEY**: Your Gotify application token.
|
||||||
|
|
||||||
|
### 3. Make the Script Executable:
|
||||||
|
Make the backup script executable:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
chmod +x lubelogger_backup.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Run the Backup Script:
|
||||||
|
To run the script manually, execute:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./lubelogger_backup.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. Automate the Backup (Optional):
|
||||||
|
You can automate the backup process using `cron`. For example, to run the backup script every day at 2 AM, add the following cron job:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
0 2 * * * /path/to/lubelogger_backup.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
## How It Works:
|
||||||
|
1. **Backup Creation**: The script sends a request to the Lubelogger API (`/api/makebackup`) to trigger the backup creation.
|
||||||
|
2. **Backup Download**: The script downloads the backup as a `.zip` file from the generated URL.
|
||||||
|
3. **Uptime Kuma Check-in**: The script sends a check-in to Uptime Kuma to indicate the backup is running as expected.
|
||||||
|
4. **Gotify Error Notification**: If there are any errors during the backup process, Gotify is notified with the error details.
|
||||||
|
5. **Log Management**: The script generates logs for each backup with detailed timestamps, errors, and status.
|
||||||
|
6. **Retention**: The script automatically deletes backups and logs older than the specified retention period (configured in the `.env` file).
|
||||||
|
|
||||||
|
## Example Output:
|
||||||
|
When the script runs, you should see output logs like this:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
[$(date)] 🔄 Starting Lubelogger backup job
|
||||||
|
[$(date)] ✅ Uptime Kuma check-in sent.
|
||||||
|
[$(date)] ⬇️ Downloading backup from https://lubelogger.mydomain.com/api/backup/download/abc123
|
||||||
|
[$(date)] ✅ Backup saved to /path/to/backup/backup_2025-04-25_14-30-00.zip
|
||||||
|
[$(date)] 🧹 Cleaning up old backups/logs older than 7 days
|
||||||
|
[$(date)] ✅ Backup job finished
|
||||||
|
```
|
||||||
|
|
||||||
|
### Logs and Errors:
|
||||||
|
- The script scans logs for any occurrence of "Error" and sends a Gotify notification with the details.
|
||||||
|
- In case of a failure in the backup process, Uptime Kuma and Gotify will be notified with the appropriate status.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Troubleshooting:
|
||||||
|
|
||||||
|
### Uptime Kuma:
|
||||||
|
- Ensure that the Uptime Kuma check-in URL and API key are correctly set in the `.env` file.
|
||||||
|
- Verify that Uptime Kuma is accessible and configured properly.
|
||||||
|
|
||||||
|
### Gotify:
|
||||||
|
- If Gotify notifications are not working, ensure that your Gotify server is correctly set up to accept push notifications via `POST` with form data.
|
||||||
|
- Double-check the app token and push URL in the `.env` file.
|
||||||
|
|
||||||
|
### Permissions:
|
||||||
|
- Make sure that the script has the appropriate permissions to execute and access the backup directory.
|
||||||
|
- Ensure that the directory specified in `BACKUP_DIR` exists and is writable by the user running the script.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## License:
|
||||||
|
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
||||||
|
|||||||
95
lubelogger_backup.sh
Executable file
95
lubelogger_backup.sh
Executable file
@ -0,0 +1,95 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Load config from .env
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
if [[ -f "$SCRIPT_DIR/.env" ]]; then
|
||||||
|
source "$SCRIPT_DIR/.env"
|
||||||
|
else
|
||||||
|
echo "[$(date)] Missing .env file in $SCRIPT_DIR"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Ensure backup directory exists
|
||||||
|
mkdir -p "$BACKUP_DIR"
|
||||||
|
|
||||||
|
# Generate timestamped filenames
|
||||||
|
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
|
||||||
|
OUTPUT_FILE="$BACKUP_DIR/backup_$TIMESTAMP.zip"
|
||||||
|
LOG_FILE="$BACKUP_DIR/backup_$TIMESTAMP.log"
|
||||||
|
|
||||||
|
# Logging helper
|
||||||
|
log() {
|
||||||
|
echo "[$(date)] $1" | tee -a "$LOG_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Uptime Kuma check-in (GET Request with only "status=up" and "msg=OK")
|
||||||
|
uptime_kuma_checkin() {
|
||||||
|
if [[ -n "$UPTIME_KUMA_URL" && -n "$UPTIME_KUMA_API_KEY" ]]; then
|
||||||
|
# Send GET request directly to Uptime Kuma with status=up and msg=OK
|
||||||
|
curl -s "${UPTIME_KUMA_URL}/api/push/${UPTIME_KUMA_API_KEY}?status=up&msg=OK" > /dev/null
|
||||||
|
log "✅ Uptime Kuma check-in sent."
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Gotify Error Notification
|
||||||
|
gotify_error_notify() {
|
||||||
|
local summary="Errors detected in recent Lubelogger backup logs on $(hostname):\n$1"
|
||||||
|
|
||||||
|
if [[ -n "$GOTIFY_PUSH_URL" && -n "$GOTIFY_PUSH_KEY" ]]; then
|
||||||
|
# Send the Gotify notification with form data
|
||||||
|
curl -s -X POST "${GOTIFY_PUSH_URL}/message?token=${GOTIFY_PUSH_KEY}" \
|
||||||
|
-F "title=[${BACKUP_TAG}] Backup Errors on $(hostname)" \
|
||||||
|
-F "message=$(echo -e "$summary" | head -c 1000)" \
|
||||||
|
-F "priority=5" > /dev/null
|
||||||
|
log "⚠️ Gotify error notification sent."
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Start backup
|
||||||
|
log "🔄 Starting Lubelogger backup job"
|
||||||
|
|
||||||
|
# Uptime Kuma Check-in: Inform that the backup process has started
|
||||||
|
uptime_kuma_checkin
|
||||||
|
|
||||||
|
response=$(curl -s -n -u "$USERNAME:$PASSWORD" --location "$BASE_URL/api/makebackup")
|
||||||
|
|
||||||
|
if [[ -z "$response" ]]; then
|
||||||
|
log "❌ Error: Empty response from backup API"
|
||||||
|
gotify_error_notify "Empty response from backup API."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
backup_url="${BASE_URL}${response//\"/}"
|
||||||
|
|
||||||
|
log "⬇️ Downloading backup from $backup_url"
|
||||||
|
curl -s -n -u "$USERNAME:$PASSWORD" --location "$backup_url" -o "$OUTPUT_FILE"
|
||||||
|
|
||||||
|
if [[ $? -eq 0 ]]; then
|
||||||
|
log "✅ Backup saved to $OUTPUT_FILE"
|
||||||
|
else
|
||||||
|
log "❌ Error: Failed to download backup"
|
||||||
|
gotify_error_notify "Failed to download backup."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Cleanup old backups and logs
|
||||||
|
log "🧹 Cleaning up old backups/logs older than $RETENTION_DAYS days"
|
||||||
|
find "$BACKUP_DIR" \( -name "backup_*.zip" -o -name "backup_*.log" \) -type f -mtime +$RETENTION_DAYS -exec rm {} \;
|
||||||
|
|
||||||
|
# Error summary scanning
|
||||||
|
log "🔍 Scanning recent logs for errors"
|
||||||
|
ERRORS=""
|
||||||
|
while IFS= read -r logfile; do
|
||||||
|
LOG_ERRORS=$(grep "Error" "$logfile")
|
||||||
|
if [[ -n "$LOG_ERRORS" ]]; then
|
||||||
|
ERRORS+="\n$(basename "$logfile"):\n$LOG_ERRORS\n"
|
||||||
|
fi
|
||||||
|
done < <(find "$BACKUP_DIR" -name "backup_*.log" -type f -mtime -$RETENTION_DAYS)
|
||||||
|
|
||||||
|
if [[ -n "$ERRORS" ]]; then
|
||||||
|
gotify_error_notify "$ERRORS"
|
||||||
|
fi
|
||||||
|
|
||||||
|
log "✅ Backup job finished"
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user