-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration_tool.sh
More file actions
262 lines (220 loc) · 7.39 KB
/
Copy pathmigration_tool.sh
File metadata and controls
262 lines (220 loc) · 7.39 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/bin/bash
LOG_DIR="./imapsync_logs"
SEC_DIR="./.imapsync_secrets"
TRACKER_FILE="$SEC_DIR/active_migrations.txt"
# Create directories and secure the secrets folder
mkdir -p "$LOG_DIR"
mkdir -p "$SEC_DIR"
chmod 700 "$SEC_DIR"
touch "$TRACKER_FILE"
# Variables to remember previous server inputs
DEFAULT_HOST1=""
DEFAULT_HOST2=""
# --- UTILS ---
clean_tracker() {
# Removes dead processes from the tracker file
if [ -f "$TRACKER_FILE" ]; then
temp_file=$(mktemp)
while IFS="|" read -r pid user logfile; do
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
echo "$pid|$user|$logfile" >> "$temp_file"
fi
done < "$TRACKER_FILE"
mv "$temp_file" "$TRACKER_FILE"
fi
}
# --- FUNCTIONS ---
start_sync() {
clear
echo "================================================="
echo " START NEW MIGRATION "
echo "================================================="
read -p "Source Server (Host 1) [$DEFAULT_HOST1]: " input_host1
host1=${input_host1:-$DEFAULT_HOST1}
DEFAULT_HOST1=$host1
read -p "Source Email (User 1): " user1
read -s -p "Source Password (Pass 1): " pass1; echo
echo "-------------------------------------------------"
read -p "Destination Server (Host 2) [$DEFAULT_HOST2]: " input_host2
host2=${input_host2:-$DEFAULT_HOST2}
DEFAULT_HOST2=$host2
read -p "Destination Email (User 2) [$user1]: " input_user2
user2=${input_user2:-$user1}
read -s -p "Destination Password (Pass 2): " pass2; echo
echo "-------------------------------------------------"
echo "⏳ Verifying credentials... Please wait."
safe_user1=$(echo "$user1" | tr '@.' '__')
passfile1="$SEC_DIR/${safe_user1}_pass1.txt"
passfile2="$SEC_DIR/${safe_user1}_pass2.txt"
# Write passwords to files
echo -n "$pass1" > "$passfile1"
echo -n "$pass2" > "$passfile2"
# SECURITY: Set read/write only for owner (chmod 600)
chmod 600 "$passfile1" "$passfile2"
# Pre-flight check
if imapsync \
--host1 "$host1" --user1 "$user1" --passfile1 "$passfile1" --ssl1 \
--host2 "$host2" --user2 "$user2" --passfile2 "$passfile2" --ssl2 \
--justlogin > /dev/null 2>&1; then
echo "✅ Credentials verified successfully!"
else
echo "❌ Error: Could not log in. Check credentials."
# Immediate cleanup on failure
rm -f "$passfile1" "$passfile2"
read -n 1 -s -r -p "Press any key to return..."
return
fi
timestamp=$(date +"%Y%m%d_%H%M%S")
logfile="$LOG_DIR/${safe_user1}_${timestamp}.log"
echo "🚀 Starting migration..."
# Command array construction
cmd=(
imapsync
--host1 "$host1"
--user1 "$user1"
--passfile1 "$passfile1"
--ssl1
--host2 "$host2"
--user2 "$user2"
--passfile2 "$passfile2"
--ssl2
--syncinternaldates
--resyncflags
--delete2duplicates
--no-modulesversion
--errorsmax 100
--useheader "Message-Id"
--skipcrossduplicates
)
# --- SECURITY WRAPPER ---
# We run a subshell (...) in the background.
# This subshell runs imapsync, and IMMEDIATELY after it finishes (success or fail),
# it deletes the password files.
(
# Ignore HUP signal (like nohup)
trap "" HUP
# Run the command
"${cmd[@]}"
# SECURITY CLEANUP: Remove passwords immediately after process ends
rm -f "$passfile1" "$passfile2"
echo "🔒 Security: Temporary password files deleted." >> "$logfile"
) > "$logfile" 2>&1 < /dev/null &
pid=$!
# Add to tracker: PID | User | LogPath
echo "$pid|$user1|$logfile" >> "$TRACKER_FILE"
echo "✅ Started! PID: $pid"
echo "Note: Password files will be auto-deleted when this task finishes."
read -n 1 -s -r -p "Press any key to return..."
}
view_active_logs() {
clean_tracker
clear
echo "================================================="
echo " VIEW ACTIVE MIGRATION LOGS "
echo "================================================="
if [ ! -s "$TRACKER_FILE" ]; then
echo "No active migrations running."
read -n 1 -s -r -p "Press any key to return..."
return
fi
options=()
logfiles=()
i=0
while IFS="|" read -r pid user logpath; do
options+=("PID: $pid - $user")
logfiles+=("$logpath")
((i++))
done < "$TRACKER_FILE"
echo "Select an ACTIVE migration to view:"
select opt in "${options[@]}"; do
if [ -n "$opt" ]; then
index=$((REPLY-1))
selected_log="${logfiles[$index]}"
if [ -f "$selected_log" ]; then
monitor_log "$selected_log"
else
echo "Log file not found."
fi
break
else
echo "Invalid selection."
fi
done
}
monitor_log() {
local logf=$1
while true; do
clear
echo "================================================="
echo " Live View: $(basename "$logf")"
echo " 💡 Press 'q' to return to menu."
echo "================================================="
tail -n 25 "$logf"
read -t 1 -n 1 key
if [[ $key == "q" || $key == "Q" ]]; then break; fi
done
}
view_all_logs() {
clear
echo "================================================="
echo " HISTORY (ALL LOGS) "
echo "================================================="
shopt -s nullglob
logs=("$LOG_DIR"/*.log)
if [ ${#logs[@]} -eq 0 ]; then
echo "No logs found."
read -n 1 -s -r -p "Return..."
return
fi
select logf in "${logs[@]}"; do
if [ -n "$logf" ]; then
less +G "$logf"
break
fi
done
}
clean_logs() {
clear
echo "================================================="
echo " CLEANUP LOGS "
echo "================================================="
echo "Warning: This will delete all log files."
echo "It will NOT stop active migrations."
read -p "Are you sure? (y/n): " confirm
if [[ $confirm == "y" ]]; then
rm -f "$LOG_DIR"/*.log
echo "Logs deleted."
else
echo "Cancelled."
fi
read -n 1 -s -r -p "Return..."
}
# --- MAIN MENU ---
# Trap interrupt (CTRL+C) in the menu to prevent leaving mess
trap 'echo -e "\nExiting... cleaning up."; rm -f "$SEC_DIR"/*_pass*.txt; exit' INT
while true; do
clean_tracker
active_count=$(wc -l < "$TRACKER_FILE")
clear
echo "================================================="
echo " UNIVERSAL IMAP MIGRATION TOOL (SECURE) "
echo "================================================="
echo " 1) Start new migration"
echo " 2) View ACTIVE logs ($active_count running)"
echo " 3) View HISTORY (Old/Completed logs)"
echo " 4) Delete old logs"
echo " 5) Exit"
echo "================================================="
read -p "Choose: " opt
case $opt in
1) start_sync ;;
2) view_active_logs ;;
3) view_all_logs ;;
4) clean_logs ;;
5)
# Extra cleanup on exit just in case
exit 0
;;
*) echo "Invalid."; sleep 1 ;;
esac
done