shell编程笔记2--nfs挂载

Posted 昕光xg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell编程笔记2--nfs挂载相关的知识,希望对你有一定的参考价值。


@[toc]shell编程笔记2-ns挂载

shell编程笔记2–ns挂载

NFS简介

NFS就是Network File System的缩写,它最大的功能就是可以通过网络,让不同的机器、不同的操作系统可以共享彼此的文件。NFS服务器可以让PC将网络中的NFS服务器共享的目录挂载到本地端的文件系统中,而在本地端的系统中来看,那个远程主机的目录就好像是自己的一个磁盘分区一样,在使用上相当便利.由於在工作中非常實用,此處將其寫成了兩個腳本,以便於後續使用時候查看.

服务|客户端shell脚本

服務端:

服務端將對應內容寫到/etc/exports中,並重啓nfs-kernel-server即可;

#!/bin/bash

usage()

echo "usage:
$0


main()

# back up fstab
user=xg
BAKCUP=/home/$user/backup/nfs
if [ ! -d $BAKCUP ];then
sudo mkdir -p $BAKCUP
fi
cp /etc/exports $BAKCUP/exports.$(date +%Y%m%d%H%M)

# prepare dir
if [ ! -d $2 ];then
sudo mkdir -p $2
fi
sudo touch $2/nfs-test.txt

# install nfs-common
dpkg -l | grep nfs
if [ $? -ne 0 ];then
apt-get install -y nfs*
if [ $? -ne 0 ];then
echo "install nfs* failed!"
exit 1
fi
fi
dpkg -l | grep rpcbind
if [ $? -ne 0 ];then
sudo apt-get install -y rpcbind
if [ $? -ne 0 ];then
echo "install rpcbind failed!"
exit 1
fi
fi

#mount_str
cat /etc/exports | grep $2
if [ $? -ne 0 ];then
mount_str=$2 $1(rw,sync,no_subtree_check)
sudo echo $mount_str >> /etc/exports
fi

# check if mount success
sudo service nfs-kernel-server restart


main $1 $2

客戶端:

將相應內容寫到/etc/fstab中,並mount即可;

#!/bin/bash

# author: xg
# email :
# function: nfs客户端挂载
# date: 2019-09-29 18:30

usage()

echo "usage:
$0


main()

# back up fstab
user=xg
BAKCUP=/home/$user/backup/nfs
if [ ! -d $BAKCUP ];then
sudo mkdir -p $BAKCUP
fi
cp /etc/fstab $BAKCUP/fstab.$(date +%Y%m%d%H%M)

# install nfs-common
dpkg -l | grep nfs-common
if [ $? -ne 0 ];then
sudo apt-get install -y nfs-common
if [ $? -ne 0 ];then
echo "install nfs-common failed!"
exit 1
fi
fi

# append to /etc/fstab
cat /etc/fstab |grep $1
if [ $? -ne 0 ];then
mount_str=$1:$2 $3 nfs rw,async,vers=3,rsize=524288,wsize=524288,acdirmin=5,acdirmax=8,hard,proto=tcp 0 0
sudo /bin/bash -c "/bin/echo $mount_str
fi

# mkidr $3
if [ ! -d $3 ];then
sudo mkdir $3
fi

# mount -a
sudo mount -a
if [ $? -ne 0 ];then
echo "mount -a error!"
exit 1
fi

# check if mount success
df -h $2
if [ $? -ne 0 ];then
echo "df -h failed!"
exit 1
fi


main $1 $2 $3

说明

测试系统版本: Ubuntu 19.10 Desktop (64-bit)


以上是关于shell编程笔记2--nfs挂载的主要内容,如果未能解决你的问题,请参考以下文章

Windows Server挂载NFS共享

Shell Script编程——USB挂载/复制文件/查找文件/压缩文件

Linux 学习笔记 基本的bash shell命令

NFS详解

centos7下NFS使用与配置

NFS文件系统