Fix: Resolve relative path issue in backup script git operations
- Convert backup file path to absolute path before git add/commit - Fixes 'pathspec did not match any files' error when using ../ - Use absolute_backup_path consistently in commit_to_git() - Tested successfully: backups now commit to git without errors
This commit is contained in:
@@ -212,15 +212,17 @@ validate_config() {
|
|||||||
commit_to_git() {
|
commit_to_git() {
|
||||||
log_info "Committing backup to git repository..."
|
log_info "Committing backup to git repository..."
|
||||||
|
|
||||||
local relative_path
|
# Convert to absolute path to avoid issues with relative paths containing ../
|
||||||
relative_path=$(git -C "${REPO_ROOT}" ls-files --full-name "${BACKUP_FILE}" 2>/dev/null || echo "${BACKUP_FILE}")
|
local absolute_backup_path
|
||||||
|
absolute_backup_path="$(cd "$(dirname "${BACKUP_FILE}")" && pwd)/$(basename "${BACKUP_FILE}")"
|
||||||
|
|
||||||
if [[ -z "${relative_path}" ]]; then
|
if [[ ! -f "${absolute_backup_path}" ]]; then
|
||||||
relative_path="${BACKUP_FILE}"
|
log_error "Backup file not found at: ${absolute_backup_path}"
|
||||||
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Add the backup file to git
|
# Add the backup file to git using absolute path
|
||||||
if git -C "${REPO_ROOT}" add "${BACKUP_FILE}"; then
|
if git -C "${REPO_ROOT}" add "${absolute_backup_path}"; then
|
||||||
log_success "Added backup to git staging area"
|
log_success "Added backup to git staging area"
|
||||||
else
|
else
|
||||||
log_error "Failed to add backup to git"
|
log_error "Failed to add backup to git"
|
||||||
@@ -234,9 +236,9 @@ commit_to_git() {
|
|||||||
|
|
||||||
# Get configuration summary from XML
|
# Get configuration summary from XML
|
||||||
local vlan_count
|
local vlan_count
|
||||||
vlan_count=$(grep -c '<vlan>' "${BACKUP_FILE}" 2>/dev/null || echo "unknown")
|
vlan_count=$(grep -c '<vlan>' "${absolute_backup_path}" 2>/dev/null || echo "0")
|
||||||
local rule_count
|
local rule_count
|
||||||
rule_count=$(grep -c '<rule>' "${BACKUP_FILE}" 2>/dev/null || echo "unknown")
|
rule_count=$(grep -c '<rule>' "${absolute_backup_path}" 2>/dev/null || echo "0")
|
||||||
|
|
||||||
# Enhanced commit message with details
|
# Enhanced commit message with details
|
||||||
local detailed_msg="${commit_msg}
|
local detailed_msg="${commit_msg}
|
||||||
@@ -247,7 +249,7 @@ Contains:
|
|||||||
- Backup timestamp: ${TIMESTAMP}
|
- Backup timestamp: ${TIMESTAMP}
|
||||||
|
|
||||||
File: $(basename "${BACKUP_FILE}")
|
File: $(basename "${BACKUP_FILE}")
|
||||||
SHA256: $(shasum -a 256 "${BACKUP_FILE}" | awk '{print $1}')
|
SHA256: $(shasum -a 256 "${absolute_backup_path}" | awk '{print $1}')
|
||||||
"
|
"
|
||||||
|
|
||||||
# Commit the backup
|
# Commit the backup
|
||||||
|
|||||||
Reference in New Issue
Block a user