Create install.sh

This commit is contained in:
DualStackAdmin 2025-10-09 01:26:10 +04:00 committed by GitHub
parent 49a050c803
commit 41ecb26f61
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

85
install.sh Normal file
View file

@ -0,0 +1,85 @@
#!/bin/bash
#=================================================================================
# M365 Mailbox Backup Tool - Automatic Installation Script (v2.0 - Final English)
#=================================================================================
# --- Color Codes ---
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Please run this script with 'sudo': sudo ./install.sh${NC}"
exit 1
fi
echo "=== M365 Backup Tool Installation Started ==="
# --- User Input Block ---
IP_ADDR=$(hostname -I | awk '{print $1}')
REDIRECT_URI="https://$IP_ADDR/get_token"
echo "---------------------------------------------------------------------"
echo "IMPORTANT: Please go to your Azure AD App Registration and add the following"
echo "Redirect URI under the 'Web' platform section:"
echo ""
echo "$REDIRECT_URI"
echo ""
echo "---------------------------------------------------------------------"
read -p "Press Enter after you have added the Redirect URI in the Azure Portal..."
echo -n "Enter your Azure Tenant ID: "
read TENANT_ID
echo -n "Enter your Application (Client) ID: "
read CLIENT_ID
echo -n "Enter your Client Secret Value (will not be visible): "
read -s CLIENT_SECRET
echo ""
echo -n "Enter comma-separated emails of allowed admins (e.g., admin1@company.com): "
read ALLOWED_ADMINS
echo ""
echo "Credentials received."
# --- System Dependency Installation ---
echo "--> Updating system and installing required packages (nginx, redis, python3-venv)..."
apt-get update > /dev/null 2>&1
apt-get install -y nginx redis-server python3-pip python3-venv > /dev/null 2>&1
# --- Project Directory and Virtual Environment ---
PROJECT_DIR="/opt/m365-backup-tool"
USERNAME=${SUDO_USER:-$(whoami)}
echo "--> Creating project directory: $PROJECT_DIR"
mkdir -p "$PROJECT_DIR"
cd "$PROJECT_DIR"
echo "--> Creating Python virtual environment..."
python3 -m venv venv
echo "--> Installing Python libraries..."
source venv/bin/activate
pip install Flask gunicorn "celery[redis]" msal requests python-decouple > /dev/null 2>&1
deactivate
# --- Application File Creation ---
# .env file
echo "--> Creating .env file for credentials..."
cat <<EOF > .env
TENANT_ID=${TENANT_ID}
CLIENT_ID=${CLIENT_ID}
CLIENT_SECRET=${CLIENT_SECRET}
FLASK_SECRET_KEY=$(python3 -c 'import secrets; print(secrets.token_hex(24))')
ALLOWED_ADMINS=${ALLOWED_ADMINS}
REDIRECT_URI=${REDIRECT_URI}
EOF
# app.py file
echo "--> Creating main application file (app.py)..."
cat <<'EOF' > app.py
import os, re, msal, requests, json, secrets
from flask import Flask, request, redirect, url_for, render_template, send_from_directory, session
from celery import Celery
from email import policy
from email.parser import BytesParser
from datetime