-
Notifications
You must be signed in to change notification settings - Fork 2
/
inmanage.sh
executable file
·752 lines (676 loc) · 26.8 KB
/
inmanage.sh
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
#!/bin/bash
set -e
## Self configuration
INM_SELF_ENV_FILE=".inmanage/.env.inmanage"
INM_PROVISION_ENV_FILE=".inmanage/.env.provision"
# Declare an associative array for default settings and their corresponding prompt texts. Will be used to create .
declare -A default_settings=(
["INM_BASE_DIRECTORY"]="$PWD/"
["INM_INSTALLATION_DIRECTORY"]="./invoiceninja"
["INM_ENV_FILE"]="\${INM_BASE_DIRECTORY}\${INM_INSTALLATION_DIRECTORY}/.env"
["INM_TEMP_DOWNLOAD_DIRECTORY"]="./._in_tempDownload"
["INM_KEEP_DBTABLESPACE"]="N"
["INM_BACKUP_DIRECTORY"]="./_in_backups"
["INM_FORCE_READ_DB_PW"]="N"
["INM_ENFORCED_USER"]="www-data"
["INM_ENFORCED_SHELL"]="$(command -v bash)"
["INM_PHP_EXECUTABLE"]="$(command -v php)"
["INM_ARTISAN_STRING"]="\${INM_PHP_EXECUTABLE} \${INM_BASE_DIRECTORY}\${INM_INSTALLATION_DIRECTORY}/artisan"
["INM_PROGRAM_NAME"]="InvoiceNinja"
["INM_COMPATIBILITY_VERSION"]="5+"
["INM_KEEP_BACKUPS"]="2"
)
# Declare an associative array for the corresponding prompt texts
declare -A prompt_texts=(
["INM_BASE_DIRECTORY"]="Which directory contains your IN installation folder? Must have a trailing slash."
["INM_INSTALLATION_DIRECTORY"]="What is the installation directory name? Must be relative from \$INM_BASE_DIRECTORY and can start with a . dot."
["INM_KEEP_DBTABLESPACE"]="In shared hosting you may need to exclude tablespaces from mysqldump command. You need to keep it if your DB is very optimized for memory usage. Keep (Y) or Drop (N)"
["INM_BACKUP_DIRECTORY"]="Where shall backups go?"
["INM_FORCE_READ_DB_PW"]="Include database password in backup command? May be a security concern and may be visible for other server users while the task is running. If (N) the script assumes you have a secure and working .my.cnf file with your DB credentials and no password may get exposed to a task list. (Y/N)"
["INM_ENFORCED_USER"]="The user running the script? Should be the webserver user in most cases. Check twice if this value is set correct according to your webserver's setup."
["INM_ENFORCED_SHELL"]="Which shell should be used? In doubt, keep as is."
["INM_PHP_EXECUTABLE"]="Path to the PHP executable? In doubt, keep as is."
["INM_KEEP_BACKUPS"]="How many backup files and update iterations to keep? If set to 7 and backups occour on a daily basis you have 7 snapshots available. Make sure you have enough disk space."
)
# Function to prompt for user input
prompt() {
local var_name="$1"
local default_value="$2"
local prompt_message="$3"
while true; do
read -r -p "${prompt_message} [default: $default_value]: " input
if [ -z "$input" ]; then
input="$default_value"
fi
if [ -n "$input" ]; then
echo "$input"
return
else
echo "Invalid input. Please try again."
fi
done
}
## Create Database
## Consumes parameters for the connection like: create_database "$DB_ELEVATED_USERNAME" "$DB_ELEVATED_PASSWORD"
## If username is missing, it prompts for user input. If password is missing it tries to connect with elevated user only.
create_database() {
local username="$1"
local password="$2"
if [ -z "$username" ]; then
username=$(prompt "DB_ELEVATED_USERNAME" "" "Enter a DB username with create database permissions.")
echo "Enter the password (input will be hidden):"
read -s password
fi
# Create database and user
if [ -z "$password" ]; then
echo -e "No password given: Assuming .my.cnf credentials connection."
mysql -h "$DB_HOST" -P "$DB_PORT" -u "$username" <<EOF
CREATE DATABASE IF NOT EXISTS $DB_DATABASE DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER IF NOT EXISTS '$DB_USERNAME'@'localhost' IDENTIFIED BY '$DB_PASSWORD';
GRANT ALL PRIVILEGES ON $DB_DATABASE.* TO '$DB_USERNAME'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
CREATE USER IF NOT EXISTS '$DB_USERNAME'@'$DB_HOST' IDENTIFIED BY '$DB_PASSWORD';
GRANT ALL PRIVILEGES ON $DB_DATABASE.* TO '$DB_USERNAME'@'$DB_HOST' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF
else
mysql -h "$DB_HOST" -P "$DB_PORT" -u "$username" -p"$password" <<EOF
CREATE DATABASE IF NOT EXISTS $DB_DATABASE DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER IF NOT EXISTS '$DB_USERNAME'@'localhost' IDENTIFIED BY '$DB_PASSWORD';
GRANT ALL PRIVILEGES ON $DB_DATABASE.* TO '$DB_USERNAME'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
CREATE USER IF NOT EXISTS '$DB_USERNAME'@'$DB_HOST' IDENTIFIED BY '$DB_PASSWORD';
GRANT ALL PRIVILEGES ON $DB_DATABASE.* TO '$DB_USERNAME'@'$DB_HOST' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF
fi
if [ $? -eq 0 ]; then
echo "Database and user created successfully. If they already existed, they were untouched. Privileges were granted."
# Remove DB_ELEVATED_USERNAME and DB_ELEVATED_PASSWORD from INM_PROVISION_ENV_FILE
if [ -f "$INM_PROVISION_ENV_FILE" ]; then
sed -i '/^DB_ELEVATED_USERNAME/d' "$INM_PROVISION_ENV_FILE"
sed -i '/^DB_ELEVATED_PASSWORD/d' "$INM_PROVISION_ENV_FILE"
echo "Removed DB_ELEVATED_USERNAME and DB_ELEVATED_PASSWORD from $INM_PROVISION_ENV_FILE if they were there."
else
echo "$INM_PROVISION_ENV_FILE not found, cannot remove elevated credentials."
fi
else
echo "Failed to create database and user."
exit 1
fi
}
## Check for .env for installation provisioning, create db, move IN config to target
check_provision_file() {
if [ -f "$INM_PROVISION_ENV_FILE" ]; then
source "$INM_PROVISION_ENV_FILE"
if [ -z "$DB_HOST" ] || [ -z "$DB_DATABASE" ] || [ -z "$DB_USERNAME" ] || [ -z "$DB_PORT" ]; then
echo "Some DB variables are missing in provision file."
exit 1
fi
# Check for elevated credentials
if [ -n "$DB_ELEVATED_USERNAME" ]; then
echo "Elevated SQL user $DB_ELEVATED_USERNAME found in $INM_PROVISION_ENV_FILE."
elevated_username="$DB_ELEVATED_USERNAME"
elevated_password="$DB_ELEVATED_PASSWORD"
else
echo "No elevated SQL username found. Continuing with standard credentials."
elevated_username=""
elevated_password=""
fi
echo "Provision file loaded. Checking database connection and database existence now."
# Attempt connection using elevated credentials if available
if [ -n "$elevated_username" ]; then
if mysql -h "$DB_HOST" -P "$DB_PORT" -u "$elevated_username" -p"$elevated_password" -e 'quit'; then
echo "Elevated credentials: Connection successful."
# Check if database exists
if mysql -h "$DB_HOST" -P "$DB_PORT" -u "$elevated_username" -p"$elevated_password" -e "use $DB_DATABASE"; then
echo "Connection Possible. Database already exists."
else
echo "Connection Possible. Database does not exist."
echo "Trying to create database now."
create_database "$elevated_username" "$elevated_password"
fi
else
echo "Failed to connect using elevated credentials. Check your elevated DB credentials and connection settings."
# Prompt for elevated password if not provided
if [ -z "$elevated_password" ]; then
elevated_password=$(prompt "DB_ELEVATED_PASSWORD" "" "Enter the password for elevated user")
fi
# Retry connection with the provided password
if mysql -h "$DB_HOST" -P "$DB_PORT" -u "$elevated_username" -p"$elevated_password" -e 'quit'; then
echo "Connection successful with provided elevated credentials."
# Check if database exists
if mysql -h "$DB_HOST" -P "$DB_PORT" -u "$elevated_username" -p"$elevated_password" -e "use $DB_DATABASE"; then
echo "Connection Possible. Database already exists."
else
echo "Connection Possible. Database does not exist."
echo "Trying to create database now."
create_database "$elevated_username" "$elevated_password"
fi
else
echo "Failed to connect with provided elevated credentials. Unable to proceed."
exit 1
fi
fi
else
echo "No elevated credentials available. Trying to connect with standard user."
# Check database connection using standard user credentials
if mysql -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USERNAME" -p"$DB_PASSWORD" -e 'quit'; then
# Check if database exists
if mysql -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USERNAME" -p"$DB_PASSWORD" -e "use $DB_DATABASE"; then
echo "Connection Possible. Database already exists."
else
echo "Connection Possible. Database does not exist."
echo "Trying to create database now."
create_database "$DB_USERNAME" "$DB_PASSWORD"
fi
else
echo "Failed to connect to the database with standard credentials. Check your DB credentials and connection settings."
exit 1
fi
fi
install_tar "Provisioned"
else
echo "No provision."
fi
}
# Check and load the self environment file
check_env() {
echo -e "Environment check starts."
if [ ! -f "$INM_SELF_ENV_FILE" ]; then
echo "$INM_SELF_ENV_FILE configuration file for this script not found. Attempting to create it..."
create_own_config
else
echo -e "Self configuration found"
source "$INM_SELF_ENV_FILE"
# Ensure script runs as INM_ENFORCED_USER
if [ "$(whoami)" != "$INM_ENFORCED_USER" ]; then
INM_SCRIPT_PATH=$(realpath "$0")
echo "Switching to user '$INM_ENFORCED_USER'."
exec sudo -u "$INM_ENFORCED_USER" "$INM_ENFORCED_SHELL" -c "cd '$(pwd)' && \"$INM_SCRIPT_PATH\" \"$@\""
exit 0
fi
check_missing_settings
check_provision_file
fi
}
# Create config file and symlink in base directory
create_own_config() {
if touch "$INM_SELF_ENV_FILE"; then
echo "Write Permissions OK. Proceeding with configuration..."
rm $INM_SELF_ENV_FILE
echo -e "\n\n Just press [ENTER] to accept defaults. \n\n"
# Loop through default settings and prompt user for input, allowing them to override
for key in "${!default_settings[@]}"; do
value=${default_settings[$key]}
prompt_text=${prompt_texts[$key]:-"Provide value for $key:"}
default_settings[$key]=$(prompt "$key" "$value" "$prompt_text")
done
# Save configuration to .env.inmanage
for key in "${!default_settings[@]}"; do
echo "$key=\"${default_settings[$key]}\"" >> "$INM_SELF_ENV_FILE"
done
echo "$INM_SELF_ENV_FILE has been created and configured."
# Handle symlink
target="$INM_BASE_DIRECTORY.inmanage/inmanage.sh"
link="$INM_BASE_DIRECTORY/inmanage.sh"
# Check if the link exists
if [ -L "$link" ]; then
# Check if it points to the correct target
if [ "$(readlink "$link")" == "$target" ]; then
echo "The symlink is correct."
else
echo "The symlink is incorrect. Updating..."
ln -sf "$target" "$link"
fi
else
echo "The symlink does not exist. Creating..."
ln -s "$target" "$link"
fi
# Download .env.example for provisioning
env_example_file="$INM_BASE_DIRECTORY.inmanage/.env.example"
echo "Downloading .env.example for provisioning"
curl -sL "https://raw.githubusercontent.com/invoiceninja/invoiceninja/v5-stable/.env.example" -o "$env_example_file" || {
echo "Failed to download .env.example for seeding"
exit 1
}
# Modify the downloaded file
if [ -f "$env_example_file" ]; then
sed -i '/^DB_PORT=/a DB_ELEVATED_USERNAME=\nDB_ELEVATED_PASSWORD=' "$env_example_file"
fi
# Source the configuration file and check for provisioning
source "$INM_SELF_ENV_FILE"
check_provision_file
else
echo "Error: Could not create $INM_SELF_ENV_FILE. Aborting configuration."
exit 1
fi
}
# Check if any new settings are missing from the environment file
check_missing_settings() {
updated=0
# Loop through default settings and check if they exist in the env file
for key in "${!default_settings[@]}"; do
if ! grep -q "^$key=" "$INM_SELF_ENV_FILE"; then
echo "$key not found in $INM_SELF_ENV_FILE. Adding with default value '${default_settings[$key]}'."
echo "$key=\"${default_settings[$key]}\"" >> "$INM_SELF_ENV_FILE"
updated=1
fi
done
# Reload the environment file if any settings were updated
if [ "$updated" -eq 1 ]; then
echo "Updated $INM_SELF_ENV_FILE with missing settings. Reloading..."
source "$INM_SELF_ENV_FILE"
else
echo "All settings are present in $INM_SELF_ENV_FILE."
fi
}
# Check required commands
check_commands() {
local commands=("curl" "tar" "cp" "mv" "mkdir" "chown" "find" "rm" "mysqldump" "mysql" "grep" "xargs" "php" "read" "source" "touch" "sed")
for cmd in "${commands[@]}"; do
if ! command -v "$cmd" &>/dev/null; then
echo "Error: Command '$cmd' is not available. Please install it and try again."
exit 1
fi
done
}
# Get installed version
get_installed_version() {
local version
if [ -f "$INM_BASE_DIRECTORY$INM_INSTALLATION_DIRECTORY/VERSION.txt" ]; then
version=$(cat "$INM_BASE_DIRECTORY$INM_INSTALLATION_DIRECTORY/VERSION.txt") || {
echo "Failed to read installed version"
exit 1
}
echo "$version"
else
echo "VERSION.txt not found"
exit 1
fi
}
# Get latest version
get_latest_version() {
local version
version=$(curl -s https://api.github.com/repos/invoiceninja/invoiceninja/releases/latest | grep tag_name | cut -d '"' -f 4 | sed 's/v//') || {
echo "Failed to retrieve latest version"
exit 1
}
echo "$version"
}
# Download Invoice Ninja
download_ninja() {
[ -d "$INM_TEMP_DOWNLOAD_DIRECTORY" ] && rm -Rf "$INM_TEMP_DOWNLOAD_DIRECTORY"
mkdir -p "$INM_TEMP_DOWNLOAD_DIRECTORY" || {
echo "Failed to create temp directory"
exit 1
}
cd "$INM_TEMP_DOWNLOAD_DIRECTORY" || {
echo "Failed to change to temp directory"
exit 1
}
local app_version
app_version=$(get_latest_version)
sleep 2
echo "Downloading Invoice Ninja version $app_version..."
curl -sL "https://github.com/invoiceninja/invoiceninja/releases/download/v$app_version/invoiceninja.tar" -o invoiceninja.tar || {
echo "Failed to download"
exit 1
}
}
# Install tar
install_tar() {
local mode="$1"
local env_file
if [ "$mode" == "Provisioned" ]; then
env_file="$INM_BASE_DIRECTORY$INM_PROVISION_ENV_FILE"
else
env_file="$INM_INSTALLATION_DIRECTORY/.env.example"
fi
local latest_version
latest_version=$(get_latest_version)
if [ -d "$INM_BASE_DIRECTORY$INM_INSTALLATION_DIRECTORY" ]; then
echo -n "Caution: Installation directory already exists! Current installation directory will get renamed. Proceed with installation? (yes/no): "
# Set a timeout for 60 seconds
if ! read -t 60 response; then
echo "No response within 60 seconds. Installation aborted."
exit 0
fi
if [[ ! "$response" =~ ^[Yy]([Ee][Ss])?$ ]]; then
echo "Installation aborted."
exit 0
fi
mv "$INM_BASE_DIRECTORY$INM_INSTALLATION_DIRECTORY" "_last_IN_$(date +'%Y%m%d_%H%M%S')"
fi
echo "Installation starts now"
download_ninja
mkdir "$INM_INSTALLATION_DIRECTORY" || {
echo "Failed to create installation directory"
exit 1
}
chown "$INM_ENFORCED_USER" "$INM_INSTALLATION_DIRECTORY" || {
echo "Failed to change owner"
exit 1
}
echo -e "Unpacking tar"
tar -xzf invoiceninja.tar -C "$INM_INSTALLATION_DIRECTORY" || {
echo "Failed to unpack"
exit 1
}
mv "$env_file" "$INM_INSTALLATION_DIRECTORY/.env" || {
echo "Failed to move .env file"
exit 1
}
chmod 600 "$INM_INSTALLATION_DIRECTORY/.env" || {
echo "Failed to chmod 600 .env file"
exit 1
}
mv "$INM_INSTALLATION_DIRECTORY" "$INM_BASE_DIRECTORY$INM_INSTALLATION_DIRECTORY" || {
echo "Failed move installation to target directory $INM_BASE_DIRECTORY$INM_INSTALLATION_DIRECTORY"
exit 1
}
echo -e "Generating Key"
$INM_ARTISAN_STRING key:generate --force || {
echo "Failed to generate key"
exit 1
}
$INM_ARTISAN_STRING optimize || {
echo "Failed to run optimize"
exit 1
}
$INM_ARTISAN_STRING up || {
echo "Failed to run artisan up"
exit 1
}
if [ "$mode" == "Provisioned" ]; then
$INM_ARTISAN_STRING migrate:fresh --seed --force || {
echo "Failed to run artisan migrate"
exit 1
}
$INM_ARTISAN_STRING ninja:create-account --email=admin@admin.com --password=admin && echo -e "\n\nLogin: $APP_URL Username: admin@admin.com Password: admin" || {
echo "Standard user creation failed"
exit 1
}
echo -e "\n\nSetup Complete!\n\n\
Open your browser at $APP_URL to access the application.\n\
The database and user are configured.\n\n\
IT'S A GOOD TIME TO MAKE YOUR FIRST BACKUP NOW!!\n\n\
Cronjob Setup:\n\
Add this for scheduled tasks:\n\
* * * * * $INM_ENFORCED_USER $INM_ARTISAN_STRING schedule:run >> /dev/null 2>&1\n\n\
Scheduled Backup:\n\
To schedule a backup, add this:\n\
* 3 * * * $INM_ENFORCED_USER $INM_ENFORCED_SHELL -c \"$INM_BASE_DIRECTORY./inmanage.sh backup\" >> /dev/null 2>&1\n\n"
else
echo -e "\n\nSetup Complete!\n\n\
Open your browser at your configured address https://your.url/setup now to carry on with database setup.\n\n\
IT'S A GOOD TIME TO MAKE YOUR FIRST BACKUP NOW!!\n\n\
Cronjob Setup:\n\
Add this for scheduled tasks:\n\
* * * * * $INM_ENFORCED_USER $INM_ARTISAN_STRING schedule:run >> /dev/null 2>&1\n\n\
Scheduled Backup:\n\
To schedule a backup, add this:\n\
* 3 * * * $INM_ENFORCED_USER $INM_ENFORCED_SHELL -c \"$INM_BASE_DIRECTORY./inmanage.sh backup\" >> /dev/null 2>&1\n\n"
fi
cd "$INM_BASE_DIRECTORY" && rm -Rf "$INM_TEMP_DOWNLOAD_DIRECTORY"
}
# Run update
run_update() {
local installed_version latest_version
installed_version=$(get_installed_version)
latest_version=$(get_latest_version)
if [ "$installed_version" == "$latest_version" ] && [ "$force_update" != true ]; then
echo -e "Already up-to-date. Proceed with update? (yes/no): "
# Set a timeout for 60 seconds
if ! read -t 60 response; then
echo "No response within 60 seconds. Update aborted."
exit 0
fi
if [[ ! "$response" =~ ^[Yy]([Ee][Ss])?$ ]]; then
echo "Update aborted."
exit 0
fi
fi
echo "Update starts now."
download_ninja
mkdir "$INM_INSTALLATION_DIRECTORY" || {
echo "Failed to create installation directory"
exit 1
}
chown "$INM_ENFORCED_USER" "$INM_INSTALLATION_DIRECTORY" || {
echo "Failed to change owner"
exit 1
}
echo -e "Unpacking Data."
tar -xzf invoiceninja.tar -C "$INM_INSTALLATION_DIRECTORY" || {
echo "Failed to unpack"
exit 1
}
$INM_ARTISAN_STRING cache:clear || {
echo "Failed to clear artisan cache"
exit 1
}
$INM_ARTISAN_STRING down || {
echo "Failed to run artisan down"
exit 1
}
cp "$INM_ENV_FILE" "$INM_INSTALLATION_DIRECTORY/" || {
echo "Failed to copy .env"
exit 1
}
if [ -d "$INM_BASE_DIRECTORY$INM_INSTALLATION_DIRECTORY/public/storage/" ]; then
cp -R "$INM_BASE_DIRECTORY$INM_INSTALLATION_DIRECTORY/public/storage/." "$INM_INSTALLATION_DIRECTORY/public/storage/" || {
echo "Failed to copy storage from $INM_BASE_DIRECTORY$INM_INSTALLATION_DIRECTORY/public/storage/."
}
else
echo "Directory does not exist: $INM_BASE_DIRECTORY$INM_INSTALLATION_DIRECTORY/public/storage/"
echo "This may be normal if this is an initial installation, or if your storage is located somewhere different."
fi
mv "$INM_BASE_DIRECTORY$INM_INSTALLATION_DIRECTORY" "$INM_BASE_DIRECTORY${INM_INSTALLATION_DIRECTORY}_$(date +'%Y%m%d_%H%M%S')" || {
echo "Failed to rename old installation"
exit 1
}
# Remove the 'down' file if it exists in the versioned old directory
old_version_dir="$INM_BASE_DIRECTORY${INM_INSTALLATION_DIRECTORY}_$(date +'%Y%m%d_%H%M%S')"
if [ -f "$old_version_dir/public/storage/framework/down" ]; then
rm "$old_version_dir/public/storage/framework/down" || {
echo "Failed to remove 'Maintenance' file from $old_version_dir/public/storage/framework/"
exit 1
}
echo "'Maintenanace' file removed from $old_version_dir/public/storage/framework/."
fi
mv "$INM_BASE_DIRECTORY$INM_TEMP_DOWNLOAD_DIRECTORY/$INM_INSTALLATION_DIRECTORY" "$INM_BASE_DIRECTORY$INM_INSTALLATION_DIRECTORY" || {
echo "Failed to move new installation"
exit 1
}
$INM_ARTISAN_STRING optimize || {
echo "Failed to artisan optimize"
exit 1
}
$INM_ARTISAN_STRING ninja:post-update || {
echo "Failed to run post-update"
exit 1
}
$INM_ARTISAN_STRING migrate --force || {
echo "Failed to run artisan migrate"
exit 1
}
$INM_ARTISAN_STRING ninja:check-data || {
echo "Failed to run check data"
exit 1
}
$INM_ARTISAN_STRING ninja:translations || {
echo "Failed to run translations"
exit 1
}
$INM_ARTISAN_STRING up || {
echo "Failed to run artisan up"
exit 1
}
# Do if Snappdf set in .env file
source "$INM_ENV_FILE"
if [ "$PDF_GENERATOR" = "snappdf" ]; then
echo "Snappdf configuration detected. Updating binaries. Downloading ungoogled chrome."
cd "${INM_BASE_DIRECTORY}${INM_INSTALLATION_DIRECTORY}"
## composer require beganovich/snappdf
./vendor/bin/snappdf download
else
echo "Skipping snappdf config."
fi
cleanup_old_versions
}
# Run backup
run_backup() {
if [ ! -d "$INM_BASE_DIRECTORY$INM_BACKUP_DIRECTORY" ]; then
echo "Creating backup directory."
mkdir -p "$INM_BASE_DIRECTORY$INM_BACKUP_DIRECTORY" || {
echo "Failed to create backup directory"
exit 1
}
else
echo "Backup directory already exists. Using it."
fi
if [ -f "$INM_ENV_FILE" ]; then
local export_vars
export_vars=$("$INM_ENFORCED_SHELL" -c "grep '^DB_' '$INM_ENV_FILE' | xargs") || {
echo "Failed to extract DB variables"
exit 1
}
eval "$export_vars" || {
echo "Failed to evaluate DB variables"
exit 1
}
if [ -z "$DB_HOST" ] || [ -z "$DB_DATABASE" ] || [ -z "$DB_USERNAME" ] || [ -z "$DB_PORT" ]; then
echo "Some DB variables are missing."
exit 1
fi
fi
cd "$INM_BACKUP_DIRECTORY" || {
echo "Failed to change to backup directory"
exit 1
}
# Check, if INM_KEEP_DBTABLESPACE is N
if [ "$INM_KEEP_DBTABLESPACE" == "N" ]; then
tablespace_option="--no-tablespaces"
else
tablespace_option=""
fi
# Use CLI Password mode or my.cnf
if [ "$INM_FORCE_READ_DB_PW" == "Y" ]; then
mysqldump -f --no-create-db $tablespace_option -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USERNAME" -p"$DB_PASSWORD" "$DB_DATABASE" >"${DB_DATABASE}_$(date +'%Y%m%d_%H%M%S').sql" || {
echo "Failed to dump database"
exit 1
}
else
echo "Using .my.cnf for database access"
mysqldump -f --no-create-db $tablespace_option -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USERNAME" "$DB_DATABASE" >"${DB_DATABASE}_$(date +'%Y%m%d_%H%M%S').sql" || {
echo "Failed to dump database"
exit 1
}
fi
cd "$INM_BASE_DIRECTORY" || {
echo "Failed to change to base directory"
exit 1
}
echo -e "Compressing Data. This may take a while. Hang on..."
tar -czf "${INM_PROGRAM_NAME}_$(date +'%Y%m%d_%H%M%S').tar.gz" "$INM_BACKUP_DIRECTORY"/*.sql -C "$INM_INSTALLATION_DIRECTORY" . || {
echo "Failed to create backup"
exit 1
}
rm "$INM_BACKUP_DIRECTORY"/*.sql || {
echo "Failed to remove SQL files"
exit 1
}
mv ${INM_PROGRAM_NAME}_*.tar.gz "$INM_BACKUP_DIRECTORY" || {
echo "Failed to move backups"
exit 1
}
cleanup_old_backups
}
# Cleanup old versions
cleanup_old_versions() {
echo "Cleaning up old update directory versions."
local update_dirs
update_dirs=$(find "$INM_BASE_DIRECTORY" -maxdepth 1 -type d -name "$(basename "$INM_INSTALLATION_DIRECTORY")_*" | sort -r | tail -n +$((INM_KEEP_BACKUPS + 1)))
if [ -n "$update_dirs" ]; then
echo "$update_dirs" | xargs -r rm -rf || {
echo "Failed to clean up old versions"
exit 1
}
fi
rm -Rf "$INM_TEMP_DOWNLOAD_DIRECTORY"
ls -la "$INM_BASE_DIRECTORY"
}
# Cleanup old backups
cleanup_old_backups() {
echo "Cleaning up old backups."
# Find backup files and list them
local backup_files
backup_files=$(find "$INM_BASE_DIRECTORY$INM_BACKUP_DIRECTORY" -maxdepth 1 -type f -name "*.tar.gz" | sort -r | tail -n +$((INM_KEEP_BACKUPS + 1)))
if [ -n "$backup_files" ]; then
echo "$backup_files" | xargs -r rm -f || {
echo "Failed to clean up old backups"
exit 1
}
fi
rm -Rf "$INM_TEMP_DOWNLOAD_DIRECTORY"
# List remaining files in the backup directory
ls -la "$INM_BASE_DIRECTORY$INM_BACKUP_DIRECTORY"
}
# Function caller
function_caller() {
case "$1" in
clean_install)
install_tar
;;
update)
run_update
;;
backup)
run_backup
;;
create_db)
create_database
;;
cleanup_versions)
cleanup_old_versions
;;
cleanup_backups)
cleanup_old_backups
;;
*)
echo "Unknown parameter $1"
return 1
;;
esac
}
# Parse command-line options
command=""
force_update=false
parse_options() {
while [[ $# -gt 0 ]]; do
case $1 in
--force)
force_update=true
shift
;;
clean_install | update | backup | cleanup_versions | cleanup_backups)
command=$1
shift
;;
*)
echo "Unknown option: $1"
echo -e "\n\n Usage: ./inmanage.sh <update|backup|clean_install|cleanup_versions|cleanup_backups> [--force] \n Full Documentation https://github.com/DrDBanner/inmanage/#readme \n\n"
exit 1
;;
esac
done
}
check_commands
check_env
parse_options "$@"
if [ -z "$command" ]; then
echo -e "\n\n Usage: ./inmanage.sh <update|backup|clean_install|cleanup_versions|cleanup_backups> [--force] \n Full Documentation https://github.com/DrDBanner/inmanage/#readme \n\n"
exit 1
fi
cd "$INM_BASE_DIRECTORY" && function_caller "$command"