refactor: Improve backup-env-files.sh for temp file handling and enhance crontab entries for .env backups

This commit is contained in:
Peter Wood
2025-05-29 07:19:12 -04:00
parent 20737f7872
commit faf5102cd7
6 changed files with 99 additions and 40 deletions

View File

@@ -83,6 +83,10 @@ list_env_files() {
echo -e "${BLUE}=== Environment Files Found ===${NC}"
local count=0
# Use a temp file to avoid subshell issues
local temp_file=$(mktemp)
find_env_files "$DOCKER_DIR" > "$temp_file"
while IFS= read -r env_file; do
if [ -n "$env_file" ]; then
local rel_path="${env_file#$DOCKER_DIR/}"
@@ -93,9 +97,12 @@ list_env_files() {
echo " Size: $size | Modified: $modified"
echo " Full path: $env_file"
echo ""
((count++))
count=$((count + 1))
fi
done < <(find_env_files "$DOCKER_DIR")
done < "$temp_file"
# Clean up temp file
rm -f "$temp_file"
echo -e "${BLUE}Total .env files found: $count${NC}"
@@ -247,7 +254,10 @@ backup_env_files() {
local backup_count=0
local unchanged_count=0
# Process each .env file
# Process each .env file using a temp file to avoid subshell issues
local temp_file=$(mktemp)
find_env_files "$DOCKER_DIR" > "$temp_file"
while IFS= read -r env_file; do
if [ -n "$env_file" ]; then
# Determine relative path and backup location
@@ -268,7 +278,7 @@ backup_env_files() {
if [ -f "$backup_path" ] && [ "$force" != "true" ]; then
if cmp -s "$env_file" "$backup_path"; then
needs_backup=false
((unchanged_count++))
unchanged_count=$((unchanged_count + 1))
fi
fi
@@ -276,7 +286,7 @@ backup_env_files() {
# Copy the file
cp "$env_file" "$backup_path"
echo -e "${GREEN}✓ Backed up: $rel_path${NC}"
((backup_count++))
backup_count=$((backup_count + 1))
# Also create a reference docker-compose.yml if it exists
local compose_file=$(dirname "$env_file")/docker-compose.yml
@@ -290,7 +300,10 @@ backup_env_files() {
echo -e "${YELLOW}- Unchanged: $rel_path${NC}"
fi
fi
done < <(find_env_files "$DOCKER_DIR")
done < "$temp_file"
# Clean up temp file
rm -f "$temp_file"
if [ "$dry_run" = "true" ]; then
echo -e "${BLUE}Dry run completed. No files were actually backed up.${NC}"
@@ -374,44 +387,52 @@ restore_env_files() {
local restore_count=0
local error_count=0
# Find all backed up .env files
find docker-containers -name "*.env" -type f 2>/dev/null | while IFS= read -r backup_file; do
# Determine target path
local rel_path="${backup_file#docker-containers/}"
local target_file="$DOCKER_DIR/$rel_path"
local target_dir=$(dirname "$target_file")
# Create target directory if it doesn't exist
if [ ! -d "$target_dir" ]; then
echo -e "${YELLOW}Creating directory: $target_dir${NC}"
mkdir -p "$target_dir"
fi
# Ask for confirmation if file exists and is different
if [ -f "$target_file" ]; then
if ! cmp -s "$backup_file" "$target_file"; then
echo -e "${YELLOW}File exists and differs: $rel_path${NC}"
read -p "Overwrite? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}Skipped: $rel_path${NC}"
# Use a temp file to avoid subshell issues
local temp_file=$(mktemp)
find docker-containers -name "*.env" -type f 2>/dev/null > "$temp_file"
while IFS= read -r backup_file; do
if [ -n "$backup_file" ]; then
# Determine target path
local rel_path="${backup_file#docker-containers/}"
local target_file="$DOCKER_DIR/$rel_path"
local target_dir=$(dirname "$target_file")
# Create target directory if it doesn't exist
if [ ! -d "$target_dir" ]; then
echo -e "${YELLOW}Creating directory: $target_dir${NC}"
mkdir -p "$target_dir"
fi
# Ask for confirmation if file exists and is different
if [ -f "$target_file" ]; then
if ! cmp -s "$backup_file" "$target_file"; then
echo -e "${YELLOW}File exists and differs: $rel_path${NC}"
read -p "Overwrite? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}Skipped: $rel_path${NC}"
continue
fi
else
echo -e "${GREEN}Identical: $rel_path${NC}"
continue
fi
fi
# Copy the file
if cp "$backup_file" "$target_file"; then
echo -e "${GREEN}✓ Restored: $rel_path${NC}"
restore_count=$((restore_count + 1))
else
echo -e "${GREEN}Identical: $rel_path${NC}"
continue
echo -e "${RED}✗ Failed to restore: $rel_path${NC}"
error_count=$((error_count + 1))
fi
fi
# Copy the file
if cp "$backup_file" "$target_file"; then
echo -e "${GREEN}✓ Restored: $rel_path${NC}"
((restore_count++))
else
echo -e "${RED}✗ Failed to restore: $rel_path${NC}"
((error_count++))
fi
done
done < "$temp_file"
# Clean up temp file
rm -f "$temp_file"
echo -e "${GREEN}Restore completed!${NC}"
echo -e "${BLUE}Summary:${NC}"
@@ -479,7 +500,7 @@ main() {
if [ "$list_files" = true ]; then
list_env_files
elif [ "$init_repo" = true ]; then
init_repo
init_backup_repo
elif [ "$restore" = true ]; then
restore_env_files
else