-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdomain-replace-script.sh
More file actions
executable file
·136 lines (116 loc) · 4 KB
/
domain-replace-script.sh
File metadata and controls
executable file
·136 lines (116 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
# Nginx Domain Replace Script
# This script replaces domain names in nginx configuration files
# Usage: ./domain-replace-script.sh [source_domain] [target_domain] [file_pattern]
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to display usage
usage() {
echo -e "${BLUE}Nginx Domain Replace Script${NC}"
echo ""
echo "Usage: $0 [source_domain] [target_domain] [file_pattern]"
echo ""
echo "Arguments:"
echo " source_domain - Domain to replace (e.g., 'mydomain.com')"
echo " target_domain - Replacement domain (default: 'bankai-tech.com')"
echo " file_pattern - File pattern to search (default: '*.conf')"
echo ""
echo "Examples:"
echo " $0 mydomain.com bankai-tech.com '*.conf'"
echo " $0 example.org bankai-tech.com '*.nginx'"
echo " $0 test.local # Uses default target and pattern"
echo ""
echo "The script will:"
echo " 1. Create backups of original files (.backup extension)"
echo " 2. Replace all occurrences of source_domain with target_domain"
echo " 3. Show a summary of changes made"
exit 1
}
# Check if help is requested
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
usage
fi
# Set defaults
SOURCE_DOMAIN="${1}"
TARGET_DOMAIN="${2:-bankai-tech.com}"
FILE_PATTERN="${3:-*.conf}"
# Validate input
if [[ -z "$SOURCE_DOMAIN" ]]; then
echo -e "${RED}Error: Source domain is required${NC}"
echo ""
usage
fi
echo -e "${BLUE}=== Nginx Domain Replace Script ===${NC}"
echo -e "Source domain: ${YELLOW}$SOURCE_DOMAIN${NC}"
echo -e "Target domain: ${GREEN}$TARGET_DOMAIN${NC}"
echo -e "File pattern: ${BLUE}$FILE_PATTERN${NC}"
echo ""
# Find files matching the pattern
echo -e "${BLUE}Searching for files...${NC}"
FILES=($(find . -name "$FILE_PATTERN" -type f))
if [[ ${#FILES[@]} -eq 0 ]]; then
echo -e "${RED}No files found matching pattern: $FILE_PATTERN${NC}"
exit 1
fi
echo -e "Found ${GREEN}${#FILES[@]}${NC} files:"
for file in "${FILES[@]}"; do
echo " - $file"
done
echo ""
# Ask for confirmation
read -p "Do you want to proceed with the replacement? (y/N): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}Operation cancelled${NC}"
exit 0
fi
# Process files
echo -e "${BLUE}Processing files...${NC}"
TOTAL_REPLACEMENTS=0
PROCESSED_FILES=0
for file in "${FILES[@]}"; do
# Check if file contains the source domain
if grep -q "$SOURCE_DOMAIN" "$file"; then
# Create backup
cp "$file" "$file.backup"
# Count occurrences before replacement
OCCURRENCES=$(grep -o "$SOURCE_DOMAIN" "$file" | wc -l)
# Perform replacement
sed -i.tmp "s/$SOURCE_DOMAIN/$TARGET_DOMAIN/g" "$file"
rm "$file.tmp"
echo -e " ${GREEN}✓${NC} $file (${OCCURRENCES} replacements)"
TOTAL_REPLACEMENTS=$((TOTAL_REPLACEMENTS + OCCURRENCES))
PROCESSED_FILES=$((PROCESSED_FILES + 1))
else
echo -e " ${YELLOW}-${NC} $file (no matches)"
fi
done
echo ""
echo -e "${GREEN}=== Summary ===${NC}"
echo -e "Files processed: ${GREEN}$PROCESSED_FILES${NC} / ${#FILES[@]}"
echo -e "Total replacements: ${GREEN}$TOTAL_REPLACEMENTS${NC}"
echo -e "Backup files created: ${BLUE}*.backup${NC}"
echo ""
if [[ $PROCESSED_FILES -gt 0 ]]; then
echo -e "${GREEN}✓ Domain replacement completed successfully!${NC}"
echo ""
echo -e "${BLUE}Next steps:${NC}"
echo "1. Review the modified files to ensure they look correct"
echo "2. Test the configurations if needed"
echo "3. Remove backup files when satisfied: rm *.backup"
else
echo -e "${YELLOW}No files were modified (no domain matches found)${NC}"
fi
# Additional cleanup function
echo ""
echo "Additional commands you might need:"
echo -e "${BLUE}# Remove all backup files:${NC}"
echo "find . -name '*.backup' -delete"
echo ""
echo -e "${BLUE}# Restore all files from backup:${NC}"
echo "for f in *.backup; do mv \"\$f\" \"\${f%.backup}\"; done"