summaryrefslogtreecommitdiff
path: root/update.sh
blob: 6bc6de82f98fdf981cb0f113c9f2b25666b4ad86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72<
#!/bin/bash
set -euo pipefail

# Update script for drestic installations
# Detects installation type and updates files safely

log() {
	echo "--- $* ---"
}

error() {
	echo "Error: $*" >&2
	exit 1
}

# Detect installation type
detect_installation() {
	if [ -f "/root/.restic_env" ] && [ -d "/etc/restic" ]; then
		echo "system"
	elif [ -f "$HOME/.config/restic/env" ]; then
		echo "user"
	else
		error "Cannot detect drestic installation. Not found in system (/root/.restic_env) or user ($HOME/.config/restic/env) locations."
	fi
}

# Check if running as root for system installation
check_permissions() {
	local install_type="$1"
	if [ "$install_type" = "system" ] && [ "$EUID" -ne 0 ]; then
		error "System installation update requires root. Run with sudo."
	fi
}

# Backup existing configuration files
backup_configs() {
	local install_type="$1"
	local backup_dir
	
	if [ "$install_type" = "system" ]; then
		backup_dir="/etc/restic/backup-$(date +%Y%m%d-%H%M%S)"
		sudo mkdir -p "$backup_dir"
 else
		backup_dir="$HOME/.config/restic/backup-$(date +%Y%m%d-%H%M%S)"
		mkdir -p "$backup_dir"
	fi
	
	log "Backing up configuration to $backup_dir"
	
	if [ "$install_type" = "system" ]; then
		sudo cp -r /etc/restic/* "$backup_dir/" 2>/dev/null || true
 else
		cp -r "$HOME/.config/restic"/* "$backup_dir/" 2>/dev/null || true
	fi
	
	echo "$backup_dir"
}

# Update scripts
update_scripts() {
	local install_type="$1"
	local install_dir
	
	if [ "$install_type" = "system" ]; then
		install_dir="/usr/local/bin"
		log "Updating scripts in $install_dir"
		sudo cp restic_backup.sh "$install_dir/"
		sudo cp restic_check.sh "$install_dir/"
		sudo cp common.sh "$install_dir/"
		sudo chmod +x "$install_dir/restic_*.sh" "$install_dir/common.sh"
	else
		install_dir="$HOME/.local/bin"
		mkdir -p "$install_dir"
		log "Updating scripts in $install_dir"
		cp restic_backup.sh "$install_dir/"
		cp restic_check.sh "$install_dir/"
		cp common.sh "$install_dir/"
		chmod +x "$install_dir/restic_*.sh" "$install_dir/common.sh"
	fi
}

# Update systemd services
update_services() {
	local install_type="$1"
	local systemd_dir
	
	if [ "$install_type" = "system" ]; then
		systemd_dir="/etc/systemd/system"
		log "Updating systemd services in $systemd_dir"
		sudo cp systemd/restic-backup.service "$systemd_dir/"
		sudo cp systemd/restic-check.service "$systemd_dir/"
		sudo systemctl daemon-reload
	else
		systemd_dir="$HOME/.config/systemd/user"
		mkdir -p "$systemd_dir"
		log "Updating systemd services in $systemd_dir"
		cp systemd/restic-backup.service "$systemd_dir/"
		cp systemd/restic-check.service "$systemd_dir/"
		systemctl --user daemon-reload
	fi
}

# Optional: Update bandwidth limit in environment file
update_bandwidth() {
	local install_type="$1"
	local env_file
	
	if [ "$install_type" = "system" ]; then
		env_file="/root/.restic_env"
	else
		env_file="$HOME/.config/restic/env"
	fi
	
	# Check if file exists and has old bandwidth setting
	if [ -f "$env_file" ] && grep -q "RCLONE_BWLIMIT=1M" "$env_file"; then
		log "Updating rclone bandwidth limit from 1M to 2M"
		if [ "$install_type" = "system" ]; then
			sudo sed -i 's/RCLONE_BWLIMIT=1M/RCLONE_BWLIMIT=2M/' "$env_file"
		else
			sed -i 's/RCLONE_BWLIMIT=1M/RCLONE_BWLIMIT=2M/' "$env_file"
		fi
	fi
}

# Restart services
restart_services() {
	local install_type="$1"
	
	log "Restarting systemd timers"
	if [ "$install_type" = "system" ]; then
		sudo systemctl restart restic-backup.timer restic-check.timer
		echo "System services updated. Check status with:"
		echo "  sudo systemctl status restic-backup.timer restic-check.timer"
	else
		systemctl --user restart restic-backup.timer restic-check.timer
		echo "User services updated. Check status with:"
		echo "  systemctl --user status restic-backup.timer restic-check.timer"
	fi
}

# Main update logic
main() {
	log "Starting drestic update process"
	
	# Get script directory
	SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
	cd "$SCRIPT_DIR"
	
	# Check we're in a git repo
	if ! git rev-parse --git-dir > /dev/null 2>&1; then
		error "Not in a git repository. Please run from drestic directory."
	fi
	
	# Detect installation type
	install_type=$(detect_installation)
	log "Detected $install_type installation"
	
	# Check permissions
	check_permissions "$install_type"
	
	# Backup configs
	backup_dir=$(backup_configs "$install_type")
	log "Configuration backed up to: $backup_dir"
	
	# Update scripts
	update_scripts "$install_type"
	
	# Update services
	update_services "$install_type"
	
	# Update bandwidth (optional)
	update_bandwidth "$install_type"
	
	# Restart services
	restart_services "$install_type"
	
	log "Update completed successfully!"
	echo ""
	echo "Key improvements in this update:"
	echo "  - Memory limits increased (512M RAM, 1G swap) to prevent OOM"
	echo "  - Prune coordination (2-4 AM window) to avoid multi-device conflicts"
	echo "  - Lock retry settings for robust multi-device operation"
	echo "  - Bandwidth increased from 1M to 2M for better performance"
	echo ""
	echo "If you encounter issues, restore from: $backup_dir"
}

# Run main function
main "$@"