majorly improved backup script, much more generally useful