Kaynağa Gözat

【新增】添加系统安装和卸载脚本

实现了通过 systemd 服务自动清理回收站的功能,支持远程和本地安装方式。
PJ568 1 yıl önce
ebeveyn
işleme
d4f607d316
4 değiştirilmiş dosya ile 207 ekleme ve 1 silme
  1. 29 1
      README.en.md
  2. 28 0
      README.md
  3. 93 0
      scripts/install.sh
  4. 57 0
      scripts/uninstall.sh

+ 29 - 1
README.en.md

@@ -6,6 +6,12 @@
 
 A script to clean files in the trash that have been kept for more than a specified number of days.
 
+Project URL: [https://github.com/PJ-568/clean_trash](https://github.com/PJ-568/clean_trash)
+
+## Installation and Usage
+
+### Method 1: Direct Script Usage
+
 ```bash
 Trash Cleaner Tool
 Usage: ./clean_trash.sh [-h] [-v] <days to keep>
@@ -17,6 +23,28 @@ Examples:
   ./clean_trash.sh -v 7  # Verbose mode to clean files older than 7 days
 ```
 
+### Method 2: System Installation (Recommended)
+
+The installation script will copy `clean_trash.sh` to `/usr/local/bin/clean_trash` and create a systemd service (not enabled by default):
+
+```bash
+# Remote installation from GitHub (without cloning the repository)
+curl -sS https://raw.githubusercontent.com/PJ-568/clean_trash/master/scripts/install.sh | bash
+
+# Or clone the repository and install locally
+git clone https://github.com/PJ-568/clean_trash.git
+cd clean_trash
+sudo ./scripts/install.sh
+
+# Enable daily automatic cleanup (files older than 30 days)
+sudo systemctl enable --now clean-trash.timer
+```
+
+```bash
+# Uninstall
+curl -sS https://raw.githubusercontent.com/PJ-568/clean_trash/master/scripts/uninstall.sh | bash
+```
+
 ## Contributing
 
 [Contribution Guide](CONTRIBUTING.md)
@@ -41,4 +69,4 @@ The trash directory typically contains three subdirectories:
 
 - **"Permanent Deletion" Operation:** When you decide to empty the trash (select the "Empty Trash" menu item in the file manager), the desktop environment does not simply delete all the contents of the `files/` and `info/` directories.
 - **Marking Process:** It first **moves** the corresponding files in the `files/` directory to the `~/.local/share/Trash/expunged/` directory (because moving files is fast).
-- **Physical Deletion:** Then, it **physically deletes** the corresponding metadata files in the `info/` directory; and slowly **physically deletes** the corresponding files in the `expunged/` directory (deleting a large number of files takes time, and doing it too quickly may slow down the entire computer).
+- **Physical Deletion:** Then, it **physically deletes** the corresponding metadata files in the `info/` directory; and slowly **physically deletes** the corresponding files in the `expunged/` directory (deleting a large number of files takes time, and doing it too quickly may slow down the entire computer).

+ 28 - 0
README.md

@@ -6,6 +6,12 @@
 
 实现清理在回收站里保存超过指定天数的文件。
 
+项目地址:[https://github.com/PJ-568/clean_trash](https://github.com/PJ-568/clean_trash)
+
+## 安装和使用
+
+### 方法 1:直接使用脚本
+
 ```bash
 回收站清理工具
 用法:./clean_trash.sh [-h] [-v] <保留天数>
@@ -17,6 +23,28 @@
   ./clean_trash.sh -v 7  # 详细模式清理 7 天前文件
 ```
 
+### 方法 2:安装到系统(推荐)
+
+安装脚本会将 `clean_trash.sh` 复制到 `/usr/local/bin/clean_trash` 并创建 systemd 服务(默认不启用):
+
+```bash
+# 从 GitHub 远程安装(无需克隆仓库)
+curl -sS https://raw.githubusercontent.com/PJ-568/clean_trash/master/scripts/install.sh | bash
+
+# 或者克隆仓库后本地安装
+git clone https://github.com/PJ-568/clean_trash.git
+cd clean_trash
+sudo ./scripts/install.sh
+
+# 启用每天自动清理(超过 30 天的文件)
+sudo systemctl enable --now clean-trash.timer
+```
+
+```bash
+# 卸载
+curl -sS https://raw.githubusercontent.com/PJ-568/clean_trash/master/scripts/uninstall.sh | bash
+```
+
 ## 贡献
 
 [贡献指北](CONTRIBUTING.md)

+ 93 - 0
scripts/install.sh

@@ -0,0 +1,93 @@
+#!/usr/bin/env bash
+
+# 安装脚本 - 将 clean_trash.sh 安装到系统并创建 systemd 服务
+
+set -e  # 遇到错误时退出
+
+# 检查是否以 root 权限运行
+if [[ $EUID -eq 0 ]]; then
+   echo "请不要以 root 权限运行此脚本"
+   exit 1
+fi
+
+SERVICE_NAME="clean-trash"
+EXEC_NAME="clean_trash"
+
+# 定义安装路径
+BIN_PATH="/usr/local/bin"
+SERVICE_PATH="/etc/systemd/system"
+
+# 检查是否是远程安装模式(通过管道执行)
+if [ -t 0 ]; then
+    # 本地执行模式
+    SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+    ROOT_DIR="$(dirname "$SCRIPT_DIR")"
+    CLEAN_TRASH_SOURCE="$ROOT_DIR/clean_trash.sh"
+    
+    echo "本地安装模式"
+else
+    # 远程执行模式(通过管道)
+    echo "远程安装模式"
+    CLEAN_TRASH_SOURCE="https://raw.githubusercontent.com/PJ-568/clean_trash/master/clean_trash.sh"
+fi
+
+echo "正在安装 $EXEC_NAME..."
+
+# 1. 复制/下载执行文件到系统路径
+echo "1. 安装执行文件到 $BIN_PATH/$EXEC_NAME"
+if [ -t 0 ]; then
+    # 本地复制
+    sudo cp "$CLEAN_TRASH_SOURCE" "$BIN_PATH/$EXEC_NAME"
+else
+    # 远程下载
+    sudo curl -sSL "$CLEAN_TRASH_SOURCE" -o "$BIN_PATH/$EXEC_NAME"
+fi
+sudo chmod +x "$BIN_PATH/$EXEC_NAME"
+
+# 2. 创建 systemd 服务文件
+echo "2. 创建 systemd 服务文件"
+sudo tee "$SERVICE_PATH/$SERVICE_NAME.service" > /dev/null <<EOF
+[Unit]
+Description=Clean Trash Service
+After=multi-user.target
+
+[Service]
+Type=oneshot
+User=$USER
+ExecStart=$BIN_PATH/$EXEC_NAME 30
+StandardOutput=journal
+StandardError=journal
+
+[Install]
+WantedBy=multi-user.target
+EOF
+
+# 3. 创建 systemd 定时器文件
+echo "3. 创建 systemd 定时器文件"
+sudo tee "$SERVICE_PATH/$SERVICE_NAME.timer" > /dev/null <<EOF
+[Unit]
+Description=Clean trash daily
+Requires=$SERVICE_NAME.service
+
+[Timer]
+OnCalendar=daily
+Persistent=true
+
+[Install]
+WantedBy=timers.target
+EOF
+
+# 重新加载 systemd 配置
+echo "4. 重新加载 systemd 配置"
+sudo systemctl daemon-reload
+
+echo "安装完成!"
+echo ""
+echo "要启用每天自动清理,请运行:"
+echo "  sudo systemctl enable --now $SERVICE_NAME.timer"
+echo ""
+echo "要手动运行清理:"
+echo "  $EXEC_NAME 30"
+echo ""
+echo "要查看服务状态:"
+echo "  systemctl status $SERVICE_NAME.timer"

+ 57 - 0
scripts/uninstall.sh

@@ -0,0 +1,57 @@
+#!/usr/bin/env bash
+
+# 卸载脚本 - 删除 clean_trash 的 systemd 服务和执行文件
+
+set -e  # 遇到错误时退出
+
+# 检查是否以 root 权限运行
+if [[ $EUID -eq 0 ]]; then
+   echo "请不要以 root 权限运行此脚本"
+   exit 1
+fi
+
+SERVICE_NAME="clean-trash"
+EXEC_NAME="clean_trash"
+
+# 定义路径
+BIN_PATH="/usr/local/bin"
+SERVICE_PATH="/etc/systemd/system"
+
+echo "正在卸载 $EXEC_NAME..."
+
+# 1. 停止并禁用 systemd 定时器(如果存在)
+echo "1. 停止并禁用 systemd 定时器"
+if systemctl is-active --quiet "$SERVICE_NAME.timer"; then
+    sudo systemctl stop "$SERVICE_NAME.timer"
+fi
+
+if systemctl is-enabled --quiet "$SERVICE_NAME.timer"; then
+    sudo systemctl disable "$SERVICE_NAME.timer"
+fi
+
+# 2. 停止并禁用 systemd 服务(如果存在)
+echo "2. 停止并禁用 systemd 服务"
+if systemctl is-active --quiet "$SERVICE_NAME.service"; then
+    sudo systemctl stop "$SERVICE_NAME.service"
+fi
+
+if systemctl is-enabled --quiet "$SERVICE_NAME.service"; then
+    sudo systemctl disable "$SERVICE_NAME.service"
+fi
+
+# 3. 重新加载 systemd 配置
+echo "3. 重新加载 systemd 配置"
+sudo systemctl daemon-reload 2>/dev/null || true
+
+# 4. 删除 systemd 文件
+echo "4. 删除 systemd 文件"
+sudo rm -f "$SERVICE_PATH/$SERVICE_NAME.service"
+sudo rm -f "$SERVICE_PATH/$SERVICE_NAME.timer"
+
+# 5. 删除执行文件
+echo "5. 删除执行文件"
+sudo rm -f "$BIN_PATH/$EXEC_NAME"
+
+echo "卸载完成!"
+echo ""
+echo "所有相关文件和 systemd 服务均已删除。"