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
echo "🚀 开始挂载 NFS 共享..." echo "=========================================="
RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m'
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
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 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
0 1 * * * /mnt/mydisk/my-sh/nfs/debian-nfs.sh >> /var/log/nfs-mount.log 2>&1
*/30 * * * * /mnt/mydisk/my-sh/nfs/check-nfs.sh >> /var/log/nfs-check.log 2>&1
0 2 * * * /mnt/mydisk/my-sh/sync/sync-all.sh >> /var/log/nfs-sync.log 2>&1
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
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
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
echo "🔄 开始文件同步: $(date)" echo "=========================================="
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
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
inotifywait -m -r -e create,modify,delete /mnt/mydisk/my-sh/ | while read path action file; do echo "📁 检测到变化: $path$file → $action" rsync -avhz --delete "$path$file" "/mnt/Ubuntu-NFS/my-sh/${path#/mnt/mydisk/my-sh/}" 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
| systemctl status nfs-server systemctl status rpcbind
ufw status firewall-cmd --list-all
ping 10.10.10.251 telnet 10.10.10.251 2049
mount -t nfs -o rw 10.10.10.251:/mnt/mydisk /mnt/test
showmount -e 10.10.10.251 nfsstat -m
|
📝 日志检查
1 2 3 4 5 6 7 8 9
| 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
echo "🔧 开始 NFS 故障修复..." echo "=========================================="
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
| mount -t nfs -o rw,soft,timeo=30,retry=3,nolock
echo "/mnt/mydisk/my-sh/nfs/debian-nfs.sh" >> /etc/rc.local
|
📊 监控和告警
1 2 3 4 5 6 7 8 9 10 11 12 13
| #!/bin/bash
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 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
|
ufw allow from 10.10.10.0/24 to any port nfs
find /mnt/*-NFS -type d -exec chmod 755 {} \; find /mnt/*-NFS -type f -exec chmod 644 {} \;
cat > /etc/logrotate.d/nfs-sync << EOF /var/log/nfs-*.log { daily rotate 7 missingok notifempty compress delaycompress create 644 root root } EOF
|
🎯 提示:建议定期测试 NFS 挂载的稳定性和同步功能的可靠性。生产环境中应考虑使用高可用方案和定期备份重要数据。
📚 扩展阅读: