Move all the document-handling functions

They're all in the same file now, which means....

1. Changing the code will be simpler now
2. There's a stepping stone to adding Document objects
3. After more refactoring, the only code left in database.js will be actual
   basic database code.

Fixed previously broken backup code. Should work now.

Change-Id: Id1ca34b56d48ef621d4a1ec8ae75f682470431e2