我正在为游戏服务器制作 bash 脚本,但是该脚本不能按我预期的方式工作。
脚本的思路是创建文件夹/ramdisk
,然后将其挂载为。然后从中复制需要的tmpfs
文件,备份游戏世界(现在存储在中),启动服务器(再次在中),当服务器关闭时,将文件从中复制回,然后卸载 ramdisk。/server/
/ramdisk
/ramdisk
/ramdisk
/ramdisk
/server
但是文件并没有/ramdisk
按照预期被复制...我相信它们根本没有被复制,因为脚本完成后,所做的所有更改/ramdisk
都没有保存回目录。/server
脚本还表示复制成功并且cp
没有给出任何错误代码。
以下是我正在使用的脚本:
echo Mounting RAMDISK...
if [ ! -d "/ramdisk" ]; then
echo Creating folder /ramdisk
mkdir /ramdisk
fi
mount -t tmpfs -o size=512M tmpfs /ramdisk
if [ $? -eq 0 ]; then
echo -e "Mounting RAMDISK... \033[0;32mFinished\033[1;37m"
else
echo -e "Mounting RAMDISK... \033[0;31mFAILED!!!\033[1;37m"
echo -e "\033[0;31mCritical Error\033[1;37m"
exit
fi
echo Copying files to RAMDISK...
cp -ar /server/* /ramdisk
if [ $? -eq 0 ]; then
echo -e "Copying files to RAMDISK... \033[0;32mFinished\033[1;37m"
else
echo -e "Copying files to RAMDISK... \033[0;31mFAILED!!!\033[1;37m"
echo -e "\033[0;31mCritical Error\033[1;37m"
exit
fi
tm=$(date "+%d-%m-%y_%H-%M-%S")
mkdir /server-backups/$tm-backup
echo Backing up World...
mkdir /server-backups/$tm-backup/world
cp -ar /ramdisk/world/* /server-backups/$tm-backup/world
if [ $? -eq 0 ]; then
echo -e "Backing up World... \033[0;32mFinished\033[1;37m"
else
echo -e "Backing up World... \033[0;31mFAILED!!!\033[1;37m"
fi
echo Backing up Nether...
mkdir /server-backups/$tm-backup/world_nether
cp -ar /ramdisk/world_nether/* /server-backups/$tm-backup/world_nether
if [ $? -eq 0 ]; then
echo -e "Backing up Nether... \033[0;32mFinished\033[1;37m"
else
echo -e "Backing up Nether... \033[0s;31mFAILED!!!\033[1;37m"
fi
echo Backing up End...
mkdir /server-backups/$tm-backup/world_the_end
cp -ar /ramdisk/world_the_end/* /server-backups/$tm-backup/world_the_end
if [ $? -eq 0 ]; then
echo -e "Backing up End... \033[0;32mFinished\033[1;37m"
else
echo -e "Backing up End... \033[0;31mFAILED!!!\033[1;37m"
fi
java -Xms2G -Xmx2G -jar /ramdisk/bukkit.jar -o false
#The error appears to be here
echo Copying files out of RAMDISK...
cp -ar /ramdisk/* /server
if [ $? -eq 0 ]; then
echo -e "Copying files out of RAMDISK... \033[0;32mFinished\033[1;37m"
else
echo -e "Copying files out of RAMDISK... \033[0;31mFAILED!!!\033[1;37m"
echo -e "\033[0;31mCritical Error\033[1;37m"
exit
fi
#End of error block
echo Unmounting RAMDISK...
umount /ramdisk
if [ $? -eq 0 ]; then
echo -e "Unmounting RAMDISK... \033[0;32mFinished\033[1;37m"
else
echo -e "Unmounting RAMDISK... \033[0;31mFAILED!!!\033[1;37m"
fi
这个脚本是我自己写的,我承认我这辈子没写过多少 bash 脚本。如果有人能帮我写这个,我会很高兴的。