Script waiting until save-all is ready before archiving

I had some major issues with the minos Backups and Archives. So I wrote ‘my own’ script that I run with Crontab to archive one of my MC servers.

The scripts looks like this:

#!/bin/bash -e
cd /usr/games/minecraft
./mineos_console.js -s Survival2 stuff "say $TIME_NL: Greetings miners, making backup now"
./mineos_console.js -s Survival2 stuff "save-off"
./mineos_console.js -s Survival2 stuff "save-all"
sleep 30
./mineos_console.js -s Survival2 archive
./mineos_console.js -s Survival2 stuff "save-on"
./mineos_console.js -s Survival2 stuff "say Backup created. Happy Mining"

The sleep 30 bothers me. Sometimes is works, sometimes it does not.
When the ‘sleep’ is to short, I get the error on the ‘…Survival2 archive…’ line:

[Survival2] Error executing "archive" because server condition not met: 1

When I wait longer, I do not get this error and all is fine.

Is there a way to wait until the previous command (save-all) is finished so I can run the ‘archive’ line after that? Of is there a way to capture the exception, and retry after waiting some more?

The problem is also that when getting the error, there is already an archive file. So if I program a retry around the exception, multiple archives will be created.

Please do not reply to this topic that I should just use the build-in Archive/Restore point mechanism, because i’ve tried that (also on a fresh reinstall of MineOs), and still loosing lot’s of days building and mining in MC.