Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 0 additions & 79 deletions lib/jobs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ get_job_scripts_dir() {
echo "$JOBS_DIR/$job_name/scripts"
}

# Get job config file path
# Usage: config_file=$(get_job_config_file "production")
get_job_config_file() {
local job_name="$1"
echo "$JOBS_DIR/$job_name/$JOB_CONFIG_FILE"
}

# ---------- Job Listing ----------

# List all job names (one per line)
Expand Down Expand Up @@ -236,57 +229,6 @@ save_job_config() {
chmod 600 "$config_file"
}

# Validate job configuration has required fields
# Usage: validate_job_config "production" || exit 1
validate_job_config() {
local job_name="$1"
local errors=0

# Required fields
local required=("RCLONE_REMOTE")
local field

for field in "${required[@]}"; do
local value
value="$(get_job_config "$job_name" "$field")"
if [[ -z "$value" ]]; then
print_error "Job '$job_name' missing required config: $field"
((errors++)) || true
fi
done

# At least one backup type must be enabled
local do_db do_files
do_db="$(get_job_config "$job_name" "DO_DATABASE")"
do_files="$(get_job_config "$job_name" "DO_FILES")"

if [[ "$do_db" != "true" && "$do_files" != "true" ]]; then
print_error "Job '$job_name' must have DO_DATABASE=true or DO_FILES=true"
((errors++)) || true
fi

# Validate paths if specified
if [[ "$do_db" == "true" ]]; then
local db_path
db_path="$(get_job_config "$job_name" "RCLONE_DB_PATH")"
if [[ -z "$db_path" ]]; then
print_error "Job '$job_name' has DO_DATABASE=true but no RCLONE_DB_PATH"
((errors++)) || true
fi
fi

if [[ "$do_files" == "true" ]]; then
local files_path
files_path="$(get_job_config "$job_name" "RCLONE_FILES_PATH")"
if [[ -z "$files_path" ]]; then
print_error "Job '$job_name' has DO_FILES=true but no RCLONE_FILES_PATH"
((errors++)) || true
fi
fi

[[ $errors -eq 0 ]]
}

# ---------- Job CRUD Operations ----------

# Create a new job
Expand Down Expand Up @@ -1090,24 +1032,3 @@ count_jobs() {

echo "$count"
}

# Get the default job name (or first available)
# Usage: job=$(get_default_job)
get_default_job() {
# If default job exists, use it
if job_exists "$DEFAULT_JOB_NAME"; then
echo "$DEFAULT_JOB_NAME"
return 0
fi

# Otherwise return first job
list_jobs | head -1
}

# Check if we're in multi-job mode (more than one job configured)
# Usage: is_multi_job && echo "multiple jobs"
is_multi_job() {
local count
count="$(count_jobs)"
[[ $count -gt 1 ]]
}
130 changes: 0 additions & 130 deletions lib/migration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ needs_migration() {
return 1
}

# Check if system is using multi-job structure
is_multi_job_enabled() {
local install_dir="${INSTALL_DIR:-/etc/backupd}"
[[ -f "$MIGRATION_MARKER" || -d "$install_dir/jobs" ]]
}

# ---------- Migration Functions ----------

# Migrate legacy .config to jobs/default/
Expand Down Expand Up @@ -167,130 +161,6 @@ EOF
return 0
}

# Rollback migration (restore single-config operation)
# Usage: rollback_migration [--force]
rollback_migration() {
local force="${1:-}"
local install_dir="${INSTALL_DIR:-/etc/backupd}"
local jobs_dir="$install_dir/jobs"

# Check if migrated
if [[ ! -f "$MIGRATION_MARKER" ]]; then
print_warning "System was not migrated, nothing to rollback"
return 0
fi

# Count jobs - only allow rollback if only default job exists
local job_count=0
local job_dir
for job_dir in "$jobs_dir"/*/; do
[[ ! -d "$job_dir" ]] && continue
[[ -f "$job_dir/job.conf" ]] && ((job_count++)) || true
done

if [[ $job_count -gt 1 && "$force" != "--force" ]]; then
print_error "Multiple jobs exist. Remove extra jobs first or use --force"
return 1
fi

print_info "Rolling back to single-config mode..."

# Stop all job-specific timers (non-default)
if [[ -d "$jobs_dir" ]]; then
for job_dir in "$jobs_dir"/*/; do
[[ ! -d "$job_dir" ]] && continue
local job_name
job_name="$(basename "$job_dir")"
[[ "$job_name" == "default" ]] && continue

# Remove job-specific timers
local timer_type
for timer_type in db files verify verify-full; do
local timer_name="backupd-${job_name}-${timer_type}.timer"
local service_name="backupd-${job_name}-${timer_type}.service"
systemctl stop "$timer_name" 2>/dev/null || true
systemctl disable "$timer_name" 2>/dev/null || true
rm -f "/etc/systemd/system/$timer_name" 2>/dev/null || true
rm -f "/etc/systemd/system/$service_name" 2>/dev/null || true
done
done
systemctl daemon-reload 2>/dev/null || true
fi

# Remove jobs directory
rm -rf "$jobs_dir"

# Remove migration marker
rm -f "$MIGRATION_MARKER"

print_success "Rollback complete"
print_info "System restored to single-config mode"
print_info "Legacy config preserved at: $install_dir/.config"

return 0
}

# ---------- Migration Status ----------

# Show migration status
# Usage: show_migration_status
show_migration_status() {
local install_dir="${INSTALL_DIR:-/etc/backupd}"

echo "Migration Status"
echo "================"
echo

if [[ -f "$MIGRATION_MARKER" ]]; then
local version date from
version="$(grep "^MIGRATION_VERSION=" "$MIGRATION_MARKER" 2>/dev/null | cut -d'"' -f2)" || version="unknown"
date="$(grep "^MIGRATION_DATE=" "$MIGRATION_MARKER" 2>/dev/null | cut -d'"' -f2)" || date="unknown"
from="$(grep "^MIGRATED_FROM=" "$MIGRATION_MARKER" 2>/dev/null | cut -d'"' -f2)" || from="unknown"

print_success "Multi-job mode: ENABLED"
echo " Version: $version"
echo " Date: $date"
echo " From: $from"
else
print_warning "Multi-job mode: NOT ENABLED"
if [[ -f "$install_dir/.config" ]]; then
echo " Legacy config exists"
if needs_migration; then
print_info "Migration available: run 'backupd' to migrate"
fi
else
echo " No configuration found"
fi
fi

echo

# Count jobs
local job_count=0
if [[ -d "$install_dir/jobs" ]]; then
local job_dir
for job_dir in "$install_dir/jobs"/*/; do
[[ ! -d "$job_dir" ]] && continue
[[ -f "$job_dir/job.conf" ]] && ((job_count++)) || true
done
fi

echo "Jobs configured: $job_count"
if [[ $job_count -gt 0 ]]; then
echo
echo "Job list:"
for job_dir in "$install_dir/jobs"/*/; do
[[ ! -d "$job_dir" ]] && continue
[[ -f "$job_dir/job.conf" ]] || continue
local job_name
job_name="$(basename "$job_dir")"
local enabled
enabled="$(grep "^JOB_ENABLED=" "$job_dir/job.conf" 2>/dev/null | cut -d'"' -f2)" || enabled="unknown"
echo " - $job_name (enabled: $enabled)"
done
fi
}

# ---------- Auto-Migration Hook ----------

# Run migration if needed (called at startup)
Expand Down
35 changes: 0 additions & 35 deletions lib/schedule.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,6 @@ get_template_schedule() {
[[ -n "$template" ]] && echo "$template" | cut -d'|' -f1
}

# BACKUPD-035: Get template display name by name
# Usage: get_template_display "daily_2am"
# Returns: Display name or empty if not found
get_template_display() {
local name="$1"
local template="${SCHEDULE_TEMPLATES[$name]}"
[[ -n "$template" ]] && echo "$template" | cut -d'|' -f2
}

# BACKUPD-035: Get template description by name
# Usage: get_template_description "daily_2am"
# Returns: Description or empty if not found
get_template_description() {
local name="$1"
local template="${SCHEDULE_TEMPLATES[$name]}"
[[ -n "$template" ]] && echo "$template" | cut -d'|' -f3
}

# BACKUPD-035: Detect template name from OnCalendar expression
# Usage: detect_template_name "*-*-* 02:00:00"
# Returns: Template name or empty if no match
detect_template_name() {
local schedule="$1"
local name oncalendar

for name in "${!SCHEDULE_TEMPLATES[@]}"; do
oncalendar=$(echo "${SCHEDULE_TEMPLATES[$name]}" | cut -d'|' -f1)
if [[ "$oncalendar" == "$schedule" ]]; then
echo "$name"
return 0
fi
done
return 1
}

# BACKUPD-035: List all available templates
# Usage: list_schedule_templates
# Output: name|oncalendar|display|description (one per line)
Expand Down