What we do is that we store a discourse_id in the original database for every post, topic and user. We create a new column in all those tables (in the old forum database) and set it to zero. If importing a certain post, topic or user succeeds we store the Discourse id (this is also handy for feeding into a SEO redirection script which we put in place instead of the old forum). If it fails we store -1. All our queries have a filter WHERE discourse_id = 0
This makes the script continue where it left off.