-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlBackup.sh
More file actions
311 lines (273 loc) · 9.65 KB
/
sqlBackup.sh
File metadata and controls
311 lines (273 loc) · 9.65 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/bin/bash
#
# sqlBackup.sh - Startup script for sqlBackup MySQL backup tool
# Version: 0.23.0
# Author: Gregor
# Description: Launcher script that checks dependencies and runs sqlBackup
#
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="${SCRIPT_DIR}/config.ini"
DEFAULT_CONFIG="${SCRIPT_DIR}/config.ini.default"
LOG_DIR="${SCRIPT_DIR}/logs"
LOG_FILE="${LOG_DIR}/sqlbackup_startup.log"
# 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 print colored output
print_status() {
local color=$1
local message=$2
echo -e "${color}[$(date '+%Y-%m-%d %H:%M:%S')] ${message}${NC}"
}
# Function to log messages
log_message() {
local message=$1
mkdir -p "${LOG_DIR}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${message}" >> "${LOG_FILE}"
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check Python version
check_python() {
if command_exists python3; then
local python_version=$(python3 -c "import sys; print('{}.{}'.format(sys.version_info.major, sys.version_info.minor))")
local major=$(echo $python_version | cut -d. -f1)
local minor=$(echo $python_version | cut -d. -f2)
if [ "$major" -eq 3 ] && [ "$minor" -ge 6 ]; then
print_status "$GREEN" "Python $python_version found ✓"
return 0
else
print_status "$RED" "Python 3.6+ required, found $python_version ✗"
return 1
fi
else
print_status "$RED" "Python 3 not found ✗"
return 1
fi
}
# Function to check if sqlBackup package is installed
check_sqlbackup_installed() {
if python3 -c "import sql_backup" 2>/dev/null; then
local version=$(python3 -c "import sql_backup; print(getattr(sql_backup, '__version__', 'unknown'))" 2>/dev/null)
print_status "$GREEN" "sqlBackup package installed (version: $version) ✓"
return 0
else
print_status "$RED" "sqlBackup package not installed ✗"
return 1
fi
}
# Function to check MySQL client tools
check_mysql_tools() {
local mysql_found=true
local mysqldump_found=true
if ! command_exists mysql; then
print_status "$RED" "MySQL client not found ✗"
mysql_found=false
else
print_status "$GREEN" "MySQL client found ✓"
fi
if ! command_exists mysqldump; then
print_status "$RED" "mysqldump not found ✗"
mysqldump_found=false
else
print_status "$GREEN" "mysqldump found ✓"
fi
if [ "$mysql_found" = true ] && [ "$mysqldump_found" = true ]; then
return 0
else
return 1
fi
}
# Function to check/create configuration
check_configuration() {
if [ ! -f "$CONFIG_FILE" ]; then
if [ -f "$DEFAULT_CONFIG" ]; then
print_status "$YELLOW" "Configuration file not found, creating from template..."
cp "$DEFAULT_CONFIG" "$CONFIG_FILE"
print_status "$GREEN" "Configuration file created: $CONFIG_FILE"
print_status "$YELLOW" "Please edit $CONFIG_FILE with your settings before running backups!"
return 2 # Config created but needs editing
else
print_status "$RED" "Configuration template not found: $DEFAULT_CONFIG ✗"
return 1
fi
else
print_status "$GREEN" "Configuration file found ✓"
return 0
fi
}
# Function to install sqlBackup package
install_sqlbackup() {
print_status "$YELLOW" "Installing sqlBackup package..."
if command_exists pip3; then
pip3 install sqlBackup
elif command_exists pip; then
pip install sqlBackup
else
print_status "$RED" "pip not found. Please install pip first."
return 1
fi
if [ $? -eq 0 ]; then
print_status "$GREEN" "sqlBackup package installed successfully ✓"
return 0
else
print_status "$RED" "Failed to install sqlBackup package ✗"
return 1
fi
}
# Function to show usage
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --install Install sqlBackup package via pip"
echo " --check Check dependencies and configuration"
echo " --config Show configuration file path"
echo " --logs Show logs directory"
echo " --help Show this help message"
echo " --version Show version information"
echo ""
echo "Without options, runs the backup process."
echo ""
echo "Examples:"
echo " $0 --check # Check if everything is ready"
echo " $0 --install # Install sqlBackup package"
echo " $0 # Run backup process"
}
# Function to run backup
run_backup() {
print_status "$BLUE" "Starting sqlBackup..."
log_message "Starting backup process via sqlBackup.sh"
# Change to script directory to ensure relative paths work
cd "${SCRIPT_DIR}"
# Run sqlBackup using the installed package
# Diagnostic: show where the console scripts are (if present)
local sql_cli_path
local sql_cli_alt_path
sql_cli_path=$(command -v sql-backup 2>/dev/null || true)
sql_cli_alt_path=$(command -v sqlbackup 2>/dev/null || true)
print_status "$BLUE" "sql-backup path: ${sql_cli_path:-not found}"
print_status "$BLUE" "sqlbackup path: ${sql_cli_alt_path:-not found}"
if [ -n "$sql_cli_path" ]; then
sql-backup "$@"
local exit_code=$?
elif [ -n "$sql_cli_alt_path" ]; then
sqlbackup "$@"
local exit_code=$?
else
# Fallback: run the package using python -m so the script works even when a venv
# is not activated (the installed console scripts may be out of PATH).
if command_exists python3; then
print_status "$YELLOW" "sqlBackup CLI not found on PATH, trying 'python3 -m sql_backup'..."
python3 -m sql_backup "$@"
local exit_code=$?
else
print_status "$RED" "sqlBackup commands not found and python3 not available."
log_message "ERROR: sqlBackup commands not found and python3 missing"
return 1
fi
fi
if [ $exit_code -eq 0 ]; then
print_status "$GREEN" "Backup completed successfully ✓"
log_message "Backup completed successfully"
else
print_status "$RED" "Backup failed with exit code $exit_code ✗"
log_message "Backup failed with exit code $exit_code"
fi
return $exit_code
}
# Main script logic
main() {
print_status "$BLUE" "sqlBackup Startup Script v1.0"
print_status "$BLUE" "=============================="
# Parse command line arguments
case "${1:-}" in
--help|-h)
show_usage
exit 0
;;
--version|-v)
if check_sqlbackup_installed; then
python3 -c "import sql_backup; print(f'sqlBackup version: {getattr(sql_backup, \"__version__\", \"unknown\")}')" 2>/dev/null
else
print_status "$RED" "sqlBackup package not installed"
fi
exit 0
;;
--config)
echo "Configuration file: $CONFIG_FILE"
echo "Default template: $DEFAULT_CONFIG"
exit 0
;;
--logs)
echo "Logs directory: $LOG_DIR"
echo "Startup log: $LOG_FILE"
exit 0
;;
--install)
check_python || exit 1
install_sqlbackup
exit $?
;;
--check)
print_status "$BLUE" "Checking dependencies..."
local all_good=true
check_python || all_good=false
check_mysql_tools || all_good=false
check_sqlbackup_installed || all_good=false
local config_status
check_configuration
config_status=$?
if [ $config_status -eq 1 ]; then
all_good=false
elif [ $config_status -eq 2 ]; then
print_status "$YELLOW" "Configuration created but needs editing!"
fi
if [ "$all_good" = true ] && [ $config_status -eq 0 ]; then
print_status "$GREEN" "All dependencies satisfied ✓"
exit 0
else
print_status "$RED" "Some dependencies missing or configuration needed ✗"
exit 1
fi
;;
"")
# No arguments - run backup
print_status "$BLUE" "Performing pre-flight checks..."
# Check all dependencies
check_python || exit 1
check_mysql_tools || exit 1
check_sqlbackup_installed || exit 1
# Check configuration
check_configuration
local config_status=$?
if [ $config_status -eq 1 ]; then
exit 1
elif [ $config_status -eq 2 ]; then
print_status "$YELLOW" "Please edit $CONFIG_FILE with your settings before running backups!"
exit 1
fi
print_status "$GREEN" "Pre-flight checks passed ✓"
# Run backup
run_backup
exit $?
;;
*)
# Unknown argument - pass to sqlBackup
print_status "$BLUE" "Passing arguments to sqlBackup..."
# Quick checks
check_sqlbackup_installed || exit 1
# Run with arguments
run_backup "$@"
exit $?
;;
esac
}
# Run main function with all arguments
main "$@"