mirror of
				https://github.com/kevinveenbirkenbach/docker-volume-backup.git
				synced 2025-11-04 10:38:00 +00:00 
			
		
		
		
	Used streaming instead of memory to prevent overflow
This commit is contained in:
		@@ -11,13 +11,13 @@ import sys
 | 
			
		||||
import os
 | 
			
		||||
import glob
 | 
			
		||||
 | 
			
		||||
def run_command(cmd, input_data=None):
 | 
			
		||||
def run_command(cmd, stdin=None):
 | 
			
		||||
    """
 | 
			
		||||
    Run a subprocess command and abort immediately on any failure.
 | 
			
		||||
    :param cmd: list of command parts
 | 
			
		||||
    :param input_data: bytes to send to process stdin
 | 
			
		||||
    :param stdin: file-like object to use as stdin
 | 
			
		||||
    """
 | 
			
		||||
    subprocess.run(cmd, input=input_data, check=True)
 | 
			
		||||
    subprocess.run(cmd, stdin=stdin, check=True)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
@@ -79,14 +79,13 @@ def main():
 | 
			
		||||
            )
 | 
			
		||||
        ])
 | 
			
		||||
 | 
			
		||||
        # Restore the dump into the database (will abort on first error)
 | 
			
		||||
        print(f"Restoring dump into {dbname}…")
 | 
			
		||||
        with open(sqlfile, "rb") as f:
 | 
			
		||||
            sql_data = f.read()
 | 
			
		||||
        run_command([
 | 
			
		||||
            "docker", "exec", "-i", container,
 | 
			
		||||
            "psql", "-U", "postgres", "-d", dbname
 | 
			
		||||
        ], input_data=sql_data)
 | 
			
		||||
        # Restore the dump into the database by streaming file (will abort on first error)
 | 
			
		||||
        print(f"Restoring dump into {dbname} (this may take a while)…")
 | 
			
		||||
        with open(sqlfile, 'rb') as infile:
 | 
			
		||||
            run_command([
 | 
			
		||||
                "docker", "exec", "-i", container,
 | 
			
		||||
                "psql", "-U", "postgres", "-d", dbname
 | 
			
		||||
            ], stdin=infile)
 | 
			
		||||
 | 
			
		||||
        print(f"✔ {dbname} restored.")
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user