Featured image of post Linux NFS 挂载与计划任务管理指南 📅

Linux NFS 挂载与计划任务管理指南 📅

Linux NFS 挂载与计划任务管理指南 📅 🚀 自动化 NFS 挂载和文件同步,实现跨服务器文件

Linux NFS 挂载与计划任务管理指南 📅

🚀 自动化 NFS 挂载和文件同步,实现跨服务器文件共享和备份


📖 目录导航


🎯 快速开始

📝 创建脚本目录

1
2
3
4
5
6
# 创建脚本存放目录
mkdir -p /mnt/mydisk/my-sh/{nfs,sync,backup}
chmod -R 755 /mnt/mydisk/my-sh/

# 查看目录结构
tree /mnt/mydisk/my-sh/ -L 2

📁 NFS 挂载脚本

🐧 Debian NFS 挂载脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
# 📍 位置: /mnt/mydisk/my-sh/nfs/debian-nfs.sh

echo "🚀 开始挂载 NFS 共享..."
echo "=========================================="

# 定义颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 创建挂载点目录
mount_points=(
    "/mnt/ARS2-NFS"
    "/mnt/PVE-NFS" 
    "/mnt/Ubuntu-NFS"
)

for mount_point in "${mount_points[@]}"; do
    if [ ! -d "$mount_point" ]; then
        mkdir -p "$mount_point"
        chmod 755 "$mount_point"
        echo -e "${GREEN}✅ 创建目录: $mount_point${NC}"
    else
        echo -e "${BLUE}📁 目录已存在: $mount_point${NC}"
    fi
done

# NFS 服务器配置
declare -A nfs_servers=(
    ["ARS2"]="10.10.10.251:/mnt/mydisk"
    ["PVE"]="10.10.10.254:/mnt/ntfs" 
    ["Ubuntu"]="10.10.10.247:/mnt/mydisk"
)

# 挂载函数
mount_nfs_share() {
    local name=$1
    local server=$2
    local mount_point=$3
    
    # 检查是否已挂载
    if mountpoint -q "$mount_point"; then
        echo -e "${YELLOW}⚠️  $name 已挂载,先卸载...${NC}"
        umount -f "$mount_point" 2>/dev/null
        sleep 2
    fi
    
    # 执行挂载
    echo -e "${BLUE}🔗 挂载 $name: $server$mount_point${NC}"
    if mount -t nfs -o rw,soft,timeo=30,retry=3 "$server" "$mount_point"; then
        echo -e "${GREEN}$name 挂载成功${NC}"
        # 验证挂载
        df -hT | grep "$mount_point"
        return 0
    else
        echo -e "${RED}$name 挂载失败${NC}"
        return 1
    fi
}

# 执行挂载
mount_nfs_share "ARS2" "${nfs_servers[ARS2]}" "/mnt/ARS2-NFS"
mount_nfs_share "PVE" "${nfs_servers[PVE]}" "/mnt/PVE-NFS" 
mount_nfs_share "Ubuntu" "${nfs_servers[Ubuntu]}" "/mnt/Ubuntu-NFS"

echo "=========================================="
echo -e "${GREEN}🎉 NFS 挂载完成!${NC}"
echo "当前挂载状态:"
df -hT | grep -E "nfs|Filesystem"

🔧 设置脚本权限

1
2
3
4
5
6
# 创建并设置脚本权限
touch /mnt/mydisk/my-sh/nfs/debian-nfs.sh
chmod +x /mnt/mydisk/my-sh/nfs/debian-nfs.sh

# 测试脚本执行
/mnt/mydisk/my-sh/nfs/debian-nfs.sh

⏰ 计划任务配置

📅 Crontab 配置

1
2
3
4
# 编辑当前用户的计划任务
crontab -e

# 添加以下内容:

🕐 计划任务示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# ┌───────────── 分钟 (0 - 59)
# │ ┌───────────── 小时 (0 - 23) 
# │ │ ┌───────────── 日 (1 - 31)
# │ │ │ ┌───────────── 月 (1 - 12)
# │ │ │ │ ┌───────────── 星期 (0 - 6) (周日是 0)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * 要执行的命令

# 📅 每天凌晨1点挂载NFS
0 1 * * * /mnt/mydisk/my-sh/nfs/debian-nfs.sh >> /var/log/nfs-mount.log 2>&1

# 🔄 每30分钟检查NFS挂载状态
*/30 * * * * /mnt/mydisk/my-sh/nfs/check-nfs.sh >> /var/log/nfs-check.log 2>&1

# 💾 每天凌晨2点执行文件同步
0 2 * * * /mnt/mydisk/my-sh/sync/sync-all.sh >> /var/log/nfs-sync.log 2>&1

# 🧹 每周日凌晨3点清理旧日志
0 3 * * 0 find /var/log -name "nfs-*.log" -mtime +7 -delete

📊 计划任务管理命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 查看当前计划任务
crontab -l

# 编辑计划任务
crontab -e

# 删除所有计划任务
crontab -r

# 查看cron日志
tail -f /var/log/syslog | grep cron

# 查看特定用户的计划任务
crontab -u username -l

🔍 NFS 状态检查脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
# 📍 位置: /mnt/mydisk/my-sh/nfs/check-nfs.sh

echo "🔍 检查 NFS 挂载状态: $(date)"
echo "================================"

mount_points=("/mnt/ARS2-NFS" "/mnt/PVE-NFS" "/mnt/Ubuntu-NFS")

for mount_point in "${mount_points[@]}"; do
    if mountpoint -q "$mount_point"; then
        echo "✅ $mount_point: 已挂载"
        # 检查可写性
        touch "$mount_point/.write_test" 2>/dev/null && rm -f "$mount_point/.write_test" && \
            echo "   📝 可写入" || echo "   🔒 只读"
    else
        echo "❌ $mount_point: 未挂载"
        # 尝试重新挂载
        echo "   🔄 尝试重新挂载..."
        /mnt/mydisk/my-sh/nfs/debian-nfs.sh
    fi
done

echo "================================"

🔄 文件同步策略

📁 目录同步脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/bash
# 📍 位置: /mnt/mydisk/my-sh/sync/sync-all.sh

echo "🔄 开始文件同步: $(date)"
echo "=========================================="

# 同步到 Ubuntu
echo "📤 同步到 Ubuntu..."
rsync -avhzp --progress --delete \
    --exclude='*.tmp' \
    --exclude='.cache/' \
    --exclude='.trash/' \
    /mnt/mydisk/my-sh/ /mnt/Ubuntu-NFS/my-sh/ \
    >> /var/log/sync-ubuntu.log 2>&1

# 同步到 PVE  
echo "📤 同步到 PVE..."
rsync -avhzp --progress --delete \
    --exclude='*.tmp' \
    --exclude='.cache/' \
    --exclude='.trash/' \
    /mnt/mydisk/my-sh/ /mnt/PVE-NFS/my-sh/ \
    >> /var/log/sync-pve.log 2>&1

echo "=========================================="
echo "📊 同步完成统计:"
echo "Ubuntu 同步日志: /var/log/sync-ubuntu.log"
echo "PVE 同步日志: /var/log/sync-pve.log"
echo "最后同步时间: $(date)"
echo "=========================================="

🎯 实时同步监控(可选)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/bin/bash
# 📍 位置: /mnt/mydisk/my-sh/sync/monitor-sync.sh

# 监控文件变化并实时同步
inotifywait -m -r -e create,modify,delete /mnt/mydisk/my-sh/ |
while read path action file; do
    echo "📁 检测到变化: $path$file$action"
    
    # 同步到 Ubuntu
    rsync -avhz --delete "$path$file" "/mnt/Ubuntu-NFS/my-sh/${path#/mnt/mydisk/my-sh/}"
    
    # 同步到 PVE
    rsync -avhz --delete "$path$file" "/mnt/PVE-NFS/my-sh/${path#/mnt/mydisk/my-sh/}"
    
    echo "✅ 实时同步完成: $(date)"
done

🔧 故障排除

🐛 常见问题解决

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# 1. 检查 NFS 服务状态
systemctl status nfs-server  # NFS 服务器
systemctl status rpcbind     # RPC 服务

# 2. 检查防火墙设置
ufw status                   # Ubuntu
firewall-cmd --list-all      # CentOS

# 3. 检查网络连通性
ping 10.10.10.251
telnet 10.10.10.251 2049     # NFS 端口

# 4. 手动测试挂载
mount -t nfs -o rw 10.10.10.251:/mnt/mydisk /mnt/test

# 5. 查看挂载详情
showmount -e 10.10.10.251    # 查看可挂载目录
nfsstat -m                   # 查看 NFS 统计信息

📝 日志检查

1
2
3
4
5
6
7
8
9
# 查看系统日志中的 NFS 相关消息
tail -f /var/log/syslog | grep nfs
journalctl -u nfs-server -f

# 查看挂载日志
tail -f /var/log/nfs-mount.log

# 查看同步日志  
tail -f /var/log/sync-ubuntu.log

🔄 自动修复脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
# 📍 位置: /mnt/mydisk/my-sh/nfs/repair-nfs.sh

echo "🔧 开始 NFS 故障修复..."
echo "=========================================="

# 强制卸载所有 NFS 挂载
umount -f /mnt/ARS2-NFS 2>/dev/null
umount -f /mnt/PVE-NFS 2>/dev/null  
umount -f /mnt/Ubuntu-NFS 2>/dev/null

# 清理残留挂载
sleep 2

# 重新挂载
/mnt/mydisk/my-sh/nfs/debian-nfs.sh

# 验证修复结果
if mountpoint -q /mnt/ARS2-NFS && mountpoint -q /mnt/PVE-NFS && mountpoint -q /mnt/Ubuntu-NFS; then
    echo "✅ NFS 修复成功"
    exit 0
else
    echo "❌ NFS 修复失败,请手动检查"
    exit 1
fi

💡 最佳实践

🎯 配置优化建议

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 1. 使用更稳定的挂载选项
mount -t nfs -o rw,soft,timeo=30,retry=3,nolock

# 2. 配置自动挂载 (/etc/fstab)
# 10.10.10.251:/mnt/mydisk /mnt/ARS2-NFS nfs rw,soft,timeo=30,retry=3 0 0

# 3. 设置开机自启动挂载
echo "/mnt/mydisk/my-sh/nfs/debian-nfs.sh" >> /etc/rc.local

# 4. 配置监控告警
# 当 NFS 挂载失败时发送邮件通知

📊 监控和告警

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/bin/bash
# 📍 位置: /mnt/mydisk/my-sh/nfs/monitor-nfs.sh

# 检查挂载状态并发送告警
if ! mountpoint -q /mnt/ARS2-NFS || ! mountpoint -q /mnt/PVE-NFS || ! mountpoint -q /mnt/Ubuntu-NFS; then
    # 发送邮件告警
    echo "NFS 挂载异常,请立即检查!" | mail -s "NFS 告警" admin@example.com
    
    # 或者使用 Webhook 通知
    curl -X POST -H "Content-Type: application/json" \
        -d '{"text":"NFS 挂载异常"}' \
        https://hooks.example.com/alert
fi

🔒 安全建议

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 1. 限制 NFS 访问权限
# 在 /etc/exports 中配置只允许特定 IP 访问

# 2. 使用防火墙规则
ufw allow from 10.10.10.0/24 to any port nfs

# 3. 定期检查挂载点权限
find /mnt/*-NFS -type d -exec chmod 755 {} \;
find /mnt/*-NFS -type f -exec chmod 644 {} \;

# 4. 配置日志轮转
cat > /etc/logrotate.d/nfs-sync << EOF
/var/log/nfs-*.log {
    daily
    rotate 7
    missingok
    notifempty
    compress
    delaycompress
    create 644 root root
}
EOF

🎯 提示:建议定期测试 NFS 挂载的稳定性和同步功能的可靠性。生产环境中应考虑使用高可用方案和定期备份重要数据。

📚 扩展阅读