Storing Data Locally

Length: 00:02:01

Lesson Summary:

Now that we're able to get the database information from our remote server, we're ready to implement the two backup methods that we need. To start, we'll add the local storage functionality.

Documentation for This Video

Implement Local Storage

Our pgbackup.dump method is returning subprocess.Popen and that object has a stdout attribute that acts like a file. We also want to write the contents of the backup to a file, so we can write a function that assumes it will receive two open files and transfers the contents from one file to another. Let's add all of the code related to storing the backup into a new file called storage.py.

By limiting our scope in that way, we can write a fairly simple function that creates the local backup. Here's what we plan to do:

  • Receive 2 open files: an infile and outfile.
  • Read the contents from the infile and write them to the outfile.
  • Close both of the files.

We want to call close on the "writeable" file to ensure that all of the content gets written (the database backup could be quite large).

src/pgbackup/storage.py

def local(infile, outfile):
    outfile.write(infile.read())
    outfile.close()
    infile.close()

This lesson is only available to Linux Academy members.

Sign Up To View This Lesson

Or Log In

Looking For Team Training?

Learn More