温馨提示×

CentOS如何设置回收站过期时间

小樊
50
2025-06-28 15:55:41
栏目: 智能运维

在 CentOS 系统中,可以通过配置 rsync 服务来实现文件回收站的过期时间。以下是设置回收站过期时间的步骤:

方法一:使用 rsyncinotify

  1. 安装 rsyncinotify-tools

    sudo yum install rsync inotify-tools
    
  2. 创建回收站目录

    sudo mkdir -p /var/spool/rsyncd/recoveries
    
  3. 配置 rsyncd.conf

    编辑 /etc/rsyncd.conf 文件,添加以下内容:

    [recoveries]
    path = /var/spool/rsyncd/recoveries
    comment = Recovery directory
    read only = no
    list = yes
    auth users = nobody
    secrets file = /etc/rsyncd.secrets
    
  4. 创建 rsyncd.secrets 文件

    创建并编辑 /etc/rsyncd.secrets 文件,添加以下内容:

    nobody:your_password
    

    然后设置文件权限:

    sudo chmod 600 /etc/rsyncd.secrets
    
  5. 启动 rsync 服务

    sudo systemctl start rsyncd
    sudo systemctl enable rsyncd
    
  6. 编写脚本监控回收站目录

    创建一个脚本 cleanup_recovery.sh 来监控回收站目录并删除过期文件:

    #!/bin/bash
    
    RECOVERY_DIR="/var/spool/rsyncd/recoveries"
    EXPIRATION_DAYS=7
    
    find "$RECOVERY_DIR" -type f -mtime +$EXPIRATION_DAYS -exec rm -f {} \;
    

    赋予脚本执行权限:

    chmod +x cleanup_recovery.sh
    
  7. 设置定时任务

    使用 cron 设置定时任务,每天运行一次清理脚本:

    crontab -e
    

    添加以下行:

    0 0 * * * /path/to/cleanup_recovery.sh
    

方法二:使用 trash-cli

trash-cli 是一个命令行工具,可以方便地管理文件回收站。以下是安装和使用方法:

  1. 安装 trash-cli

    sudo yum install trash-cli
    
  2. 设置回收站过期时间

    trash-cli 本身不直接支持设置过期时间,但可以通过脚本结合 find 命令来实现:

    #!/bin/bash
    
    TRASH_DIR="$HOME/.local/share/Trash/files"
    EXPIRATION_DAYS=7
    
    find "$TRASH_DIR" -type f -mtime +$EXPIRATION_DAYS -exec rm -rf {} \;
    

    赋予脚本执行权限:

    chmod +x cleanup_trash.sh
    
  3. 设置定时任务

    使用 cron 设置定时任务,每天运行一次清理脚本:

    crontab -e
    

    添加以下行:

    0 0 * * * /path/to/cleanup_trash.sh
    

通过以上方法,你可以在 CentOS 系统中设置文件回收站的过期时间。选择适合你需求的方法进行配置即可。

0