Refactor variable assignments and improve script readability in validate-plex-backups.sh and validate-plex-recovery.sh

- Changed inline variable assignments to separate declaration and assignment for clarity.
- Updated condition checks and log messages for better readability and consistency.
- Added a backup of validate-plex-recovery.sh for safety.
- Introduced a new script run-docker-tests.sh for testing setup in Docker containers.
- Enhanced ssh-login.sh to improve condition checks and logging functionality.
This commit is contained in:
Peter Wood
2025-06-05 17:14:02 -04:00
parent c3f237a321
commit 58b5dea8b4
31 changed files with 5024 additions and 539 deletions

View File

@@ -54,10 +54,10 @@ usage() {
# Check dependencies
check_dependencies() {
local missing_deps=()
command -v git >/dev/null 2>&1 || missing_deps+=("git")
command -v find >/dev/null 2>&1 || missing_deps+=("find")
if [ ${#missing_deps[@]} -ne 0 ]; then
echo -e "${RED}Error: Missing required dependencies: ${missing_deps[*]}${NC}"
echo "Please install the missing dependencies and try again."
@@ -68,12 +68,12 @@ check_dependencies() {
# Find all .env files in docker directories
find_env_files() {
local base_dir="$1"
if [ ! -d "$base_dir" ]; then
echo -e "${YELLOW}Warning: Docker directory $base_dir does not exist${NC}"
return 0
fi
# Find all .env files, including hidden ones and those with different extensions
find "$base_dir" -type f \( -name "*.env" -o -name ".env*" -o -name "env.*" \) 2>/dev/null | sort
}
@@ -82,17 +82,20 @@ find_env_files() {
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)
local temp_file
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/}"
local size=$(du -h "$env_file" 2>/dev/null | cut -f1)
local modified=$(stat -c %y "$env_file" 2>/dev/null | cut -d' ' -f1)
local rel_path="${env_file#"$DOCKER_DIR"/}"
local size
local modified
size=$(du -h "$env_file" 2>/dev/null | cut -f1)
modified=$(stat -c %y "$env_file" 2>/dev/null | cut -d' ' -f1)
echo -e "${GREEN}📄 $rel_path${NC}"
echo " Size: $size | Modified: $modified"
echo " Full path: $env_file"
@@ -100,12 +103,12 @@ list_env_files() {
count=$((count + 1))
fi
done < "$temp_file"
# Clean up temp file
rm -f "$temp_file"
echo -e "${BLUE}Total .env files found: $count${NC}"
if [ $count -eq 0 ]; then
echo -e "${YELLOW}No .env files found in $DOCKER_DIR${NC}"
echo "Make sure you have Docker containers with .env files in subdirectories."
@@ -115,26 +118,26 @@ list_env_files() {
# Initialize backup repository
init_backup_repo() {
echo -e "${YELLOW}Initializing .env backup repository...${NC}"
# Prompt for Gitea details if not provided
if [ -z "$GITEA_URL" ]; then
read -p "Enter your Gitea instance URL (e.g., https://git.yourdomain.com): " GITEA_URL
read -r -p "Enter your Gitea instance URL (e.g., https://git.yourdomain.com): " GITEA_URL
fi
if [ -z "$GITEA_USERNAME" ]; then
read -p "Enter your Gitea username: " GITEA_USERNAME
read -r -p "Enter your Gitea username: " GITEA_USERNAME
fi
# Create backup directory
mkdir -p "$BACKUP_DIR"
cd "$BACKUP_DIR"
# Initialize git repository if not already done
if [ ! -d ".git" ]; then
git init
echo -e "${GREEN}Initialized local git repository${NC}"
fi
# Create .gitignore for additional security
cat > .gitignore << 'EOF'
# Temporary files
@@ -150,7 +153,7 @@ Thumbs.db
# Logs
*.log
EOF
# Create README with important information
cat > README.md << 'EOF'
# Docker Environment Files Backup
@@ -169,7 +172,7 @@ This repository contains sensitive configuration files including:
## Structure
```
$()`
docker-containers/
├── container1/
│ ├── .env
@@ -177,7 +180,7 @@ docker-containers/
├── container2/
│ └── .env
└── ...
```
$()`
## Usage
@@ -189,46 +192,47 @@ docker-containers/
This information is updated automatically by the backup script.
EOF
# Create directory structure
mkdir -p docker-containers
# Set up remote if URL provided
if [ -n "$GITEA_URL" ] && [ -n "$GITEA_USERNAME" ]; then
local remote_url="${GITEA_URL%/}/${GITEA_USERNAME}/${BACKUP_REPO_NAME}.git"
# Check if remote already exists
if ! git remote get-url origin >/dev/null 2>&1; then
git remote add origin "$remote_url"
echo -e "${GREEN}Added remote origin: $remote_url${NC}"
fi
# Save configuration
cat > .env-backup-config << EOF
GITEA_URL="$GITEA_URL"
GITEA_USERNAME="$GITEA_USERNAME"
BACKUP_REPO_NAME="$BACKUP_REPO_NAME"
EOF
echo -e "${YELLOW}Configuration saved to .env-backup-config${NC}"
echo -e "${BLUE}Next steps:${NC}"
echo "1. Create a private repository '$BACKUP_REPO_NAME' in your Gitea instance"
echo "2. Run the backup script to perform your first backup"
echo "3. The script will attempt to push to the remote repository"
fi
# Initial commit
git add .
git commit -m "Initial setup of .env backup repository" || echo "Nothing to commit"
log "Backup repository initialized at $BACKUP_DIR"
}
# Load configuration
load_config() {
local config_file="$BACKUP_DIR/.env-backup-config"
if [ -f "$config_file" ]; then
# shellcheck source=/dev/null
source "$config_file"
fi
}
@@ -237,42 +241,45 @@ load_config() {
backup_env_files() {
local dry_run="$1"
local force="$2"
echo -e "${YELLOW}Starting .env files backup...${NC}"
# Check if backup directory exists
if [ ! -d "$BACKUP_DIR" ]; then
echo -e "${RED}Backup directory not found. Run with --init first.${NC}"
exit 1
fi
cd "$BACKUP_DIR"
load_config
# Create timestamp
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local timestamp
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local backup_count=0
local unchanged_count=0
# Process each .env file using a temp file to avoid subshell issues
local temp_file=$(mktemp)
local temp_file
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
local rel_path="${env_file#$DOCKER_DIR/}"
local rel_path="${env_file#"$DOCKER_DIR"/}"
local backup_path="docker-containers/$rel_path"
local backup_dir=$(dirname "$backup_path")
local backup_dir
backup_dir=$(dirname "$backup_path")
if [ "$dry_run" = "true" ]; then
echo -e "${BLUE}Would backup: $rel_path${NC}"
continue
fi
# Create backup directory structure
mkdir -p "$backup_dir"
# Check if file has changed
local needs_backup=true
if [ -f "$backup_path" ] && [ "$force" != "true" ]; then
@@ -281,17 +288,18 @@ backup_env_files() {
unchanged_count=$((unchanged_count + 1))
fi
fi
if [ "$needs_backup" = "true" ]; then
# Copy the file
cp "$env_file" "$backup_path"
echo -e "${GREEN}✓ Backed up: $rel_path${NC}"
backup_count=$((backup_count + 1))
# Also create a reference docker-compose.yml if it exists
local compose_file=$(dirname "$env_file")/docker-compose.yml
local compose_file
compose_file=$(dirname "$env_file")/docker-compose.yml
local compose_backup="$backup_dir/docker-compose.yml.ref"
if [ -f "$compose_file" ] && [ ! -f "$compose_backup" ]; then
cp "$compose_file" "$compose_backup"
echo -e "${BLUE} + Reference: docker-compose.yml${NC}"
@@ -301,15 +309,15 @@ backup_env_files() {
fi
fi
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}"
return 0
fi
# Update README with backup information
sed -i "/^## Last Backup/,$ d" README.md
cat >> README.md << EOF
@@ -323,10 +331,10 @@ backup_env_files() {
Generated by backup-env-files.sh
EOF
# Commit changes
git add .
if git diff --staged --quiet; then
echo -e "${YELLOW}No changes to commit${NC}"
log "Backup completed - no changes detected"
@@ -336,9 +344,9 @@ EOF
- Files backed up: $backup_count
- Files unchanged: $unchanged_count
- Total files: $((backup_count + unchanged_count))"
echo -e "${GREEN}Changes committed to local repository${NC}"
# Push to remote if configured
if git remote get-url origin >/dev/null 2>&1; then
echo -e "${YELLOW}Pushing to remote repository...${NC}"
@@ -357,7 +365,7 @@ EOF
log "Backup completed locally - $backup_count files backed up, $unchanged_count unchanged"
fi
fi
echo -e "${GREEN}Backup completed!${NC}"
echo -e "${BLUE}Summary:${NC}"
echo " - Files backed up: $backup_count"
@@ -368,42 +376,44 @@ EOF
# Restore .env files
restore_env_files() {
echo -e "${YELLOW}Starting .env files restore...${NC}"
if [ ! -d "$BACKUP_DIR" ]; then
echo -e "${RED}Backup directory not found at $BACKUP_DIR${NC}"
echo "Either run --init first or clone your backup repository to this location."
exit 1
fi
cd "$BACKUP_DIR"
load_config
# Pull latest changes if remote is configured
if git remote get-url origin >/dev/null 2>&1; then
echo -e "${YELLOW}Pulling latest changes from remote...${NC}"
git pull origin main 2>/dev/null || git pull origin master 2>/dev/null || true
fi
local restore_count=0
local error_count=0
# Use a temp file to avoid subshell issues
local temp_file=$(mktemp)
local temp_file
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")
local target_dir
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
@@ -419,7 +429,7 @@ restore_env_files() {
continue
fi
fi
# Copy the file
if cp "$backup_file" "$target_file"; then
echo -e "${GREEN}✓ Restored: $rel_path${NC}"
@@ -430,15 +440,15 @@ restore_env_files() {
fi
fi
done < "$temp_file"
# Clean up temp file
rm -f "$temp_file"
echo -e "${GREEN}Restore completed!${NC}"
echo -e "${BLUE}Summary:${NC}"
echo " - Files restored: $restore_count"
echo " - Errors: $error_count"
log "Restore completed - $restore_count files restored, $error_count errors"
}
@@ -449,7 +459,7 @@ main() {
local force=false
local restore=false
local list_files=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
@@ -492,10 +502,10 @@ main() {
;;
esac
done
# Check dependencies
check_dependencies
# Execute requested action
if [ "$list_files" = true ]; then
list_env_files