fix: avoid redundant backups on export-all

main
Malar Invention 2025-03-24 01:24:39 +05:30
parent 0ee5b71b64
commit af07dde281
1 changed files with 15 additions and 5 deletions

View File

@ -177,8 +177,15 @@ cmd_import() {
# Export SSH keys and config from pass # Export SSH keys and config from pass
cmd_export() { cmd_export() {
local hostname="$1" local hostname="$1"
local backup="$2" # New parameter to receive backup path
[[ -z "$hostname" ]] && die "Usage: pass ssh export <hostname>" [[ -z "$hostname" ]] && die "Usage: pass ssh export <hostname>"
# Only create backup if not provided (single host export)
if [[ -z "$backup" ]]; then
backup="${CONFIG_FILE}.bak.$(date +%s)"
cp "$CONFIG_FILE" "$backup" || die "Failed to backup config"
fi
# Retrieve Host block # Retrieve Host block
local config_store="ssh/$hostname/config" local config_store="ssh/$hostname/config"
local host_block local host_block
@ -215,10 +222,6 @@ cmd_export() {
die "Export aborted" die "Export aborted"
fi fi
# Backup original config
local backup="${CONFIG_FILE}.bak.$(date +%s)"
cp "$CONFIG_FILE" "$backup" || die "Failed to backup config"
# Remove conflicting Host blocks # Remove conflicting Host blocks
awk -v patterns="$exported_patterns" ' awk -v patterns="$exported_patterns" '
BEGIN { in_block=0; delete_lines=0 } BEGIN { in_block=0; delete_lines=0 }
@ -299,6 +302,11 @@ cmd_import_all() {
cmd_export_all() { cmd_export_all() {
echo "Exporting all hosts to $CONFIG_FILE" echo "Exporting all hosts to $CONFIG_FILE"
# Create single backup for all exports
local backup="${CONFIG_FILE}.bak.$(date +%s)"
cp "$CONFIG_FILE" "$backup" || die "Failed to backup config"
local hosts=() local hosts=()
while IFS= read -r -d '' path; do while IFS= read -r -d '' path; do
if [[ "$path" =~ ^ssh/([^/]+)/config ]]; then if [[ "$path" =~ ^ssh/([^/]+)/config ]]; then
@ -308,8 +316,10 @@ cmd_export_all() {
for host in "${hosts[@]}"; do for host in "${hosts[@]}"; do
echo "Exporting host: $host" echo "Exporting host: $host"
cmd_export "$host" || echo "Failed to export $host" cmd_export "$host" "$backup" || echo "Failed to export $host"
done done
echo "Export complete. Original config backed up to $backup"
} }
# Connect directly using stored keys # Connect directly using stored keys