#!/bin/bash # ZScript Bootstrap Installer # Lệnh cài đặt: curl -sO https://zscript.zor.vn/install && bash install set -euo pipefail REPO_URL="https://gitlab.com/vinhtech-group/zscript.git" INSTALL_DIR="/opt/zscript" LOG_DIR="${INSTALL_DIR}/logs" # Màu sắc RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' CYAN='\033[0;36m' BOLD='\033[1m' NC='\033[0m' echo -e "${CYAN}╔═══════════════════════════════════════════╗${NC}" echo -e "${CYAN}║${NC} ${BOLD}ZScript - LEMP Stack Auto Installer${NC} ${CYAN}║${NC}" echo -e "${CYAN}║${NC} ${YELLOW}Developed by ZOR${NC} ${CYAN}║${NC}" echo -e "${CYAN}╚═══════════════════════════════════════════╝${NC}" echo "" # Kiểm tra quyền root if [[ $EUID -ne 0 ]]; then echo -e "${RED}LỖI: Vui lòng chạy script với quyền root.${NC}" echo "Chạy lại: sudo bash install" exit 1 fi # Kiểm tra git đã cài chưa if ! command -v git &>/dev/null; then echo -e "${YELLOW}Đang cài đặt git...${NC}" if command -v apt-get &>/dev/null; then apt-get update -qq && apt-get install -y -qq git elif command -v dnf &>/dev/null; then dnf install -y -q git else echo -e "${RED}LỖI: Không thể cài đặt git. Vui lòng cài thủ công.${NC}" exit 1 fi fi # Clone hoặc cập nhật từ GitLab if [ -d "${INSTALL_DIR}/.git" ]; then echo -e "${YELLOW}ZScript đã tồn tại. Đang cập nhật...${NC}" cd "$INSTALL_DIR" # Backup các file config/logs riêng người dùng trước khi reset BACKUP_PATH="/opt/zscript-backup-$(date +%Y%m%d-%H%M%S).tgz" if [ -d "${INSTALL_DIR}/config" ] || [ -d "${INSTALL_DIR}/logs" ]; then echo -e "${YELLOW}Sao lưu config/logs trước khi cập nhật → ${BACKUP_PATH}${NC}" tar -czf "$BACKUP_PATH" -C /opt zscript/config zscript/logs 2>/dev/null || true chmod 600 "$BACKUP_PATH" 2>/dev/null || true fi git fetch origin # Giữ các thay đổi local vào stash (an toàn hơn reset --hard) if ! git diff --quiet || ! git diff --cached --quiet; then echo -e "${YELLOW}Có thay đổi local — đang lưu vào git stash.${NC}" git stash push -u -m "zscript-auto-stash-$(date +%s)" || true fi git reset --hard origin/main echo -e "${GREEN}Đã cập nhật ZScript thành công.${NC}" [ -f "$BACKUP_PATH" ] && echo -e "${GREEN}Backup config cũ tại: ${BACKUP_PATH}${NC}" else # Backup thư mục cũ nếu tồn tại (không phải git repo) if [ -d "$INSTALL_DIR" ]; then BACKUP_PATH="/opt/zscript-backup-$(date +%Y%m%d-%H%M%S).tgz" echo -e "${YELLOW}Phát hiện bản cài cũ. Sao lưu trước khi xóa → ${BACKUP_PATH}${NC}" tar -czf "$BACKUP_PATH" -C /opt "$(basename "$INSTALL_DIR")" 2>/dev/null || true chmod 600 "$BACKUP_PATH" 2>/dev/null || true rm -rf "$INSTALL_DIR" fi echo -e "${YELLOW}Đang tải ZScript từ GitLab...${NC}" git clone "$REPO_URL" "$INSTALL_DIR" echo -e "${GREEN}Đã tải ZScript thành công.${NC}" fi # Tạo thư mục logs mkdir -p "$LOG_DIR" # Kiểm tra các file quan trọng for required_file in install.sh menu.sh; do if [ ! -f "${INSTALL_DIR}/${required_file}" ]; then echo -e "${RED}LỖI: Thiếu file ${required_file}. Repo có thể bị lỗi.${NC}" exit 1 fi done # Đặt quyền thực thi chmod +x "${INSTALL_DIR}/install.sh" chmod +x "${INSTALL_DIR}/menu.sh" [ -f "${INSTALL_DIR}/lib/ssh-alert.sh" ] && chmod +x "${INSTALL_DIR}/lib/ssh-alert.sh" # Chạy cài đặt echo "" echo -e "${BOLD}${CYAN}=== Bắt đầu cài đặt ZScript ===${NC}" bash "${INSTALL_DIR}/install.sh" # Dọn dẹp file bootstrap rm -f "$(pwd)/install" 2>/dev/null || true