我对以下脚本有疑问。由于我对 BASH 缺乏了解,我不能 100% 确定如何创建一个循环,该循环使用“for”来计算数组中变量的数量并循环遍历每个变量。基本上我想要做的是让脚本计算变量 $sqldumps 中的变量数量,然后为每个变量运行一个 MySQL Dump(也将它们记录到数据库中)。脚本的长版本发布在底部,但下面是一个短版本:
#! /usr/bin/env bash
echo -ne '\033]0;World of Clucky - Backup\007'
BINDIR="$(dirname "$(readlink -fn "$0")")"
cd "$BINDIR"
sqldumps=("lwc" "bans" "frisnuk_permissions" "jail" "imonies" "GriefP" "permissions" "showcase" "simpleclans")
#Copy MySQL Databases
echo "[`date '+%H:%M'`] Starting MySQL Backup" >> /home/clucky/MinecraftServers/backup.log
for i in ${#sqldumps[*]}
do
echo " ${sqldumps[i]}" >> /home/clucky/MinecraftServers/backup.log;
/opt/lampp/bin/mysqldump -u'root' -p'CENSORED' --skip-lock-tables ${sqldumps[i]} > /home/clucky/MinecraftServers/.backups/$timedate/MySQL/${sqldumps[i]}.sql;
done
echo " forum";
/opt/lampp/bin/mysqldump -u'root' -p'CENSORED' forum >> /home/clucky/MinecraftServers/.backups/$timedate/MySQL/forum.sql;
echo "[`date '+%H:%M'`] MySQL Backup Complete" >> /home/clucky/MinecraftServers/backup.log
我遇到的问题尤其是:
for i in ${#sqldumps[*]}
我假设我为此使用了错误的语法,所以如果有人能给我正确的语法,将不胜感激。谢谢!
#! /usr/bin/env bash
echo -ne '\033]0;World of Clucky - Backup\007'
BINDIR="$(dirname "$(readlink -fn "$0")")"
cd "$BINDIR"
timedate=`date '+%m.%d.%Y-%H:%M'`
sqldumps=("lwc" "bans" "frisnuk_permissions" "jail" "imonies" "GriefP" "permissions" "showcase" "simpleclans")
echo "-------------- `date '+%d-%B-%Y %H:%M'` --------------" >> /home/clucky/MinecraftServers/backup.log
echo "[`date '+%H:%M'`] Starting Minecraft Backup" >> /home/clucky/MinecraftServers/backup.log
mkdir -p /home/clucky/MinecraftServers/.backups/$timedate
mkdir -p /home/clucky/MinecraftServers/.backups/$timedate/MySQL
nice --adjustment=19 tar -zcpf /home/clucky/MinecraftServers/.backups/$timedate/Minecraft.tar.gz \
--directory /home/clucky/MinecraftServers/ \
--exclude=SkyBlockkit \
--exclude=Tekkit \
--exclude=Vanilla \
--exclude=test \
--exclude=Zflocco_LP \
--exclude=Mortuus/plugins/dynmap/web --exclude=Mortuus/plugins/AutoSaveWorld/backups --exclude Mortuus/CraftBukkitVersion --exclude=Mortuus/orebfuscator_cache --exclude=Mortuus/plugins/#Removed --exclude=Mortuus/server.log \
--exclude=Frisnuk/plugins/dynmap/web --exclude=Frisnuk/plugins/AutoSaveWorld/backups --exclude Frisnuk/CraftBukkitVersion --exclude=Frisnuk/orebfuscator_cache --exclude=Frisnuk/plugins/#Removed --exclude=Frisnuk/plugins/#AwaitingUpdate --exclude=Frisnuk/server.log --exclude=Frisnuk/Suwako_Moriya100 --exclude=Frisnuk/Uk_Shadow --exclude=Frisnuk/Whiteghost99 --exclude=Frisnuk/greenrangermatt \
--exclude=dailybackup.log \
--exclude=.backups \
--exclude=backup.log \
--exclude=backups.log \
--exclude=backupscript.sh \
--exclude=dailybackup.sh \
--exclude=.keptbackups .
echo "[`date '+%H:%M'`] Minecraft Backup Complete" >> /home/clucky/MinecraftServers/backup.log
#Purge files 3 days old
echo "[`date '+%H:%M'`] Purging Old Backups" >> /home/clucky/MinecraftServers/backup.log
find /home/clucky/MinecraftServers/.backups* -mmin +4320 -exec rm --recursive {} \;
echo "[`date '+%H:%M'`] Purging Complete" >> /home/clucky/MinecraftServers/backup.log
#Copy MySQL Databases
echo "[`date '+%H:%M'`] Starting MySQL Backup" >> /home/clucky/MinecraftServers/backup.log
for i in ${#sqldumps[*]}
do
echo " ${sqldumps[i]}" >> /home/clucky/MinecraftServers/backup.log;
/opt/lampp/bin/mysqldump -u'root' -p'CENSORED' --skip-lock-tables ${sqldumps[i]} > /home/clucky/MinecraftServers/.backups/$timedate/MySQL/${sqldumps[i]}.sql;
done
echo " forum";
/opt/lampp/bin/mysqldump -u'root' -p'CENSORED' forum >> /home/clucky/MinecraftServers/.backups/$timedate/MySQL/forum.sql;
echo "[`date '+%H:%M'`] MySQL Backup Complete" >> /home/clucky/MinecraftServers/backup.log
echo "[`date '+%H:%M'`] Daily Backup Complete" >> /home/clucky/MinecraftServers/backup.log
#Read back file size
filesize=$(ls -lah /home/clucky/MinecraftServers/.backups/$timedate | awk '{ print $5}')
echo " Total Compression Size: $filesize\n" >> /home/clucky/MinecraftServers/backup.log
谢谢您的帮助!