Save .blend file via the Python API ?

So I am running a script in UI-less mode:

blender -b -P script.py

The script is generating a lot of geometry and I want to save that to a .blend file.
I tried using:

bpy.ops.wm.save_as_mainfile("/home/user/foo.blend")

and

bpy.ops.wm.save_mainfile("/home/user/foo.blend")

at the end of the script to save the file, without much success.

I searched the API docs and the web couldn’t find any other ops or commands that can do this… how do people save files when running background scripts?

Thanks!