Linux sysadmin shell scripts

From findscripter/everything-skills · View interactive version

Linux sysadmin shell scripts is an AI agent skill from the GitHub repository findscripter/everything-skills. 当需要在 Linux 服务器上快速搭起备份、监控、用户与安全、日志分析、自动化等运维脚本时使用;做生产可用的 Bash 脚本模板套用与改写,产出带时间戳/阈值告警/轮转/加密的 .sh 脚本与 cron 计划;不适用于 Windows(用 PowerShell)、可移植 POSIX sh(用 posix-shell-scripting)或一次性交互命令;触发词:备份脚本、磁盘/CPU 监控、日志分析、用户管理、cron 定时

How to use this skill

Skill file

Linux 运维脚本模板

何时使用

步骤

  1. 选模板:按任务从下方「指令」九类里挑对应骨架。
  2. 改参数:替换路径、阈值、用户名、主机、库名等占位符;变量一律加引号 "$var"。
  3. 赋权运行:chmod +x script.sh && ./script.sh;后台跑 nohup ./script.sh &。
  4. 先测后用:非生产环境跑通,必要时 bash -x script.sh 调试。
  5. 调度:交给 cron(见下)或 systemd timer;告警类脚本接邮件/Slack 通知。
  6. 加固(推荐):补 set -euo pipefail、trap 清理、绝对路径、输入校验——见 bash-defensive-patterns。

指令

约定:${var:-default} 取默认、$(date +%Y%m%d_%H%M%S) 时间戳、"$@" 全参数、$(( )) 算术。

① 备份

```bash

while [ "$(ls -1 "$backup_dir" | wc -l)" -gt "$max_backups" ]; do

oldest=$(ls -1t "$backup_dir" | tail -n 1)

rm -r "$backup_dir/$oldest"

done

```

② 监控告警(阈值默认 90)

③ 用户管理

④ 安全

```bash

openssl enc -aes-256-cbc -salt -pbkdf2 -in "$f" -out "$f.enc" # 加密

openssl enc -aes-256-cbc -d -pbkdf2 -in "$f" -out "${f%.enc}" # 解密

```

⑤ 日志分析

⑥ 网络

⑦ 自动化

⑧ 文件操作

⑨ 系统/开发

cron 格式:分(0-59) 时(0-23) 日(1-31) 月(1-12) 周(0-7,0/7=周日)。

示例

带时间戳的目录备份脚本(最小可用):

#!/bin/bash
backup_dir="/data/backups"
source_dir="/var/www"
ts=$(date +%Y%m%d_%H%M%S)
tar -czf "$backup_dir/backup_$ts.tar.gz" "$source_dir"
echo "Backup completed: backup_$ts.tar.gz"

磁盘超阈告警 + 邮件(接入通知):

#!/bin/bash
threshold=90
disk=$(df -h | grep "/dev/sda1" | awk '{print $5}' | cut -d% -f1)
if [ "$disk" -gt "$threshold" ]; then
    mail -s "Disk Alert" admin@example.com <<< "Disk usage: $disk%"
fi

每天凌晨 2 点跑备份:(crontab -l 2>/dev/null; echo "0 2 * * * /opt/backup.sh") | crontab -

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT 许可)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.