|
#!/bin/bash |
|
|
|
# --- Configuration --- |
|
REPO_URL="https://repo.jotta.us/archives/linux/arm64/" |
|
LOG_FILE="/var/log/jotta-update.log" |
|
BACKUP_DIR="/var/backups/jotta_$(date +%Y%m%d_%H%M%S)" |
|
TMP_DIR="/tmp/jotta_update_$(date +%s)" |
|
|
|
# Ensure script is run as root |
|
if [ "$EUID" -ne 0 ]; then |
|
echo "Error: Please run this script with sudo." |
|
exit 1 |
|
fi |
|
|
|
# Logging Function |
|
log() { |
|
local message="$(date '+%Y-%m-%d %H:%M:%S') - $1" |
|
echo "$message" |
|
echo "$message" >> "$LOG_FILE" |
|
} |
|
|
|
log "Starting Jotta update process..." |
|
|
|
# --- Step 1: Find and Verify Latest Version --- |
|
log "Fetching latest version info from $REPO_URL" |
|
LATEST_TAR=$(curl -s "$REPO_URL" | grep -oE 'href="[^"]+\.tar\.gz"' | sed 's/href="//;s/"//' | sort -V | tail -n 1) |
|
|
|
if [ -z "$LATEST_TAR" ]; then |
|
log "ERROR: Could not find any .tar.gz files. Aborting." |
|
exit 1 |
|
fi |
|
|
|
mkdir -p "$TMP_DIR" |
|
cd "$TMP_DIR" || exit |
|
|
|
log "Downloading $LATEST_TAR and checksum..." |
|
curl -L -O "${REPO_URL}${LATEST_TAR}" |
|
curl -L -O "${REPO_URL}${LATEST_TAR}.sha256" |
|
|
|
if sha256sum -c "${LATEST_TAR}.sha256" > /dev/null 2>&1; then |
|
log "Checksum verified successfully." |
|
else |
|
log "ERROR: Checksum mismatch! Potential corruption. Aborting." |
|
rm -rf "$TMP_DIR" |
|
exit 1 |
|
fi |
|
|
|
# --- Step 2: Create Rollback Backup --- |
|
log "Creating backup for rollback in $BACKUP_DIR..." |
|
mkdir -p "$BACKUP_DIR/bin" "$BACKUP_DIR/etc" "$BACKUP_DIR/share" |
|
|
|
# Copy current files to backup location |
|
[ -d "/etc/jottad" ] && cp -rp /etc/jottad "$BACKUP_DIR/etc/" |
|
[ -d "/usr/share/jottad" ] && cp -rp /usr/share/jottad "$BACKUP_DIR/share/" |
|
cp -p /usr/bin/jotta* "$BACKUP_DIR/bin/" 2>/dev/null |
|
|
|
# --- Step 3: Perform Update --- |
|
log "Stopping jottad service..." |
|
systemctl stop jottad |
|
|
|
log "Extracting and installing new files..." |
|
tar -xf "$LATEST_TAR" |
|
|
|
# Assume extraction creates ./etc and ./usr directories |
|
if [ -d "./etc/jottad" ]; then |
|
# Delete old files |
|
rm -rf /etc/jottad /usr/share/jottad /usr/bin/jotta* |
|
|
|
# Move new files in |
|
mv ./etc/jottad /etc/ |
|
mv ./usr/share/jottad /usr/share/ |
|
mv ./usr/bin/jotta* /usr/bin/ |
|
chmod +x /usr/bin/jotta* |
|
|
|
log "Starting jottad service..." |
|
systemctl start jottad |
|
|
|
# --- Step 4: Health Check & Rollback Logic --- |
|
sleep 3 # Give it a moment to initialize |
|
if systemctl is-active --quiet jottad; then |
|
log "SUCCESS: Jotta updated to $(/usr/bin/jottad version 2>/dev/null || echo 'latest version') and is running." |
|
# Keep backup for a while or remove it: |
|
# rm -rf "$BACKUP_DIR" |
|
else |
|
log "CRITICAL: Service failed to start after update! Initiating Rollback..." |
|
|
|
systemctl stop jottad |
|
rm -rf /etc/jottad /usr/share/jottad /usr/bin/jotta* |
|
|
|
# Restore from backup |
|
mv "$BACKUP_DIR/etc/jottad" /etc/ |
|
mv "$BACKUP_DIR/share/jottad" /usr/share/ |
|
mv "$BACKUP_DIR/bin/jotta"* /usr/bin/ |
|
|
|
systemctl start jottad |
|
log "ROLLBACK COMPLETE: Restored previous version. Check logs for service errors." |
|
fi |
|
else |
|
log "ERROR: Extraction failed. Structure not as expected. Restarting old service." |
|
systemctl start jottad |
|
fi |
|
|
|
# --- Step 5: Final Cleanup --- |
|
rm -rf "$TMP_DIR" |
|
log "Update script finished." |