你知唔知我系靓仔

pocketbase安装脚本

PocketBase 是一个开源的、轻量级的后端解决方案,专为快速构建小型应用程序或原型设计而设计。结合了数据库、用户认证、文件存储和实时订阅等功能,提供了一个简单易用的 API,适合开发者快速搭建应用后端。


#!/bin/bash

# PocketBase版本和下载链接
VERSION="0.24.1"
DOWNLOAD_URL="https://github.com/pocketbase/pocketbase/releases/download/v$VERSION/pocketbase_${VERSION}_linux_amd64.zip"

# 安装目录
INSTALL_DIR="/opt/pocketbase"
SERVICE_NAME="pocketbase"

if [ "$EUID" -ne 0 ]; then
  echo "请以root用户运行此脚本"
  exit 1
fi

install_dependencies() {
  echo "正在检测必要的工具..."
  for tool in wget unzip; do
    if ! command -v $tool &> /dev/null; then
      echo "$tool 未安装,正在安装..."
      if command -v apt-get &> /dev/null; then
        apt-get update && apt-get install -y $tool
      elif command -v yum &> /dev/null; then
        yum install -y $tool
      elif command -v dnf &> /dev/null; then
        dnf install -y $tool
      elif command -v zypper &> /dev/null; then
        zypper install -y $tool
      else
        echo "无法确定包管理器,请手动安装 $tool。"
        exit 1
      fi
    else
      echo "$tool 已安装。"
    fi
  done
}

uninstall_pocketbase() {
  echo "检测到已安装的PocketBase服务。"
  read -p "是否要卸载现有PocketBase服务?(y/n): " UNINSTALL_CHOICE
  if [[ "$UNINSTALL_CHOICE" == "y" || "$UNINSTALL_CHOICE" == "Y" ]]; then
    echo "正在停止并卸载PocketBase服务..."
    systemctl stop $SERVICE_NAME
    systemctl disable $SERVICE_NAME
    rm -f /etc/systemd/system/$SERVICE_NAME.service
    systemctl daemon-reload
    rm -rf $INSTALL_DIR
    echo "PocketBase服务已卸载。"
    exit 0
  else
    echo "取消卸载,退出脚本。"
    exit 0
  fi
}

if systemctl list-unit-files | grep -q "^$SERVICE_NAME.service"; then
  uninstall_pocketbase
fi

install_dependencies

mkdir -p $INSTALL_DIR
cd $INSTALL_DIR

echo "正在下载PocketBase..."
wget -q $DOWNLOAD_URL -O pocketbase.zip

echo "正在解压..."
unzip -q pocketbase.zip
rm pocketbase.zip

chmod +x pocketbase

read -p "是否监听所有网络接口 (0.0.0.0)?(y/n,默认y): " LISTEN_ALL
if [[ "$LISTEN_ALL" == "n" || "$LISTEN_ALL" == "N" ]]; then
  LISTEN_ADDRESS="127.0.0.1"
else
  LISTEN_ADDRESS="0.0.0.0"
fi

read -p "请输入监听端口(默认8090): " LISTEN_PORT
if [[ -z "$LISTEN_PORT" ]]; then
  LISTEN_PORT="8090"
fi

echo "正在创建系统服务..."
cat > /etc/systemd/system/$SERVICE_NAME.service <<EOF
[Unit]
Description=PocketBase Server
After=network.target

[Service]
User=root
WorkingDirectory=$INSTALL_DIR
ExecStart=$INSTALL_DIR/pocketbase serve --http=$LISTEN_ADDRESS:$LISTEN_PORT
Restart=always

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

systemctl enable $SERVICE_NAME
systemctl start $SERVICE_NAME

echo "请输入超级用户的电子邮件地址:"
read SUPERUSER_EMAIL
echo "请输入超级用户的密码:"
read SUPERUSER_PASS

echo "正在创建超级用户..."
$INSTALL_DIR/pocketbase superuser create $SUPERUSER_EMAIL $SUPERUSER_PASS

echo "PocketBase 安装完成并已启动服务。"
echo "监听地址: $LISTEN_ADDRESS"
echo "监听端口: $LISTEN_PORT"
echo "你可以通过以下命令管理服务:"
echo "  sudo systemctl start $SERVICE_NAME"
echo "  sudo systemctl stop $SERVICE_NAME"
echo "  sudo systemctl restart $SERVICE_NAME"
echo "  sudo systemctl status $SERVICE_NAME"