[파이썬] PostgreSQL 데이터베이스 백업 스케줄링

In this blog post, we will discuss how to schedule and automate backups for a PostgreSQL database using Python. Regularly backing up your database is crucial for data protection and disaster recovery. With Python’s psycopg2 library, we can connect to the PostgreSQL database and execute backup commands programmatically.

Prerequisites

Setting up the Environment

To begin, we need to install the psycopg2 library, which allows us to interact with the PostgreSQL database from Python.

pip install psycopg2

Writing the Backup Script

Create a new Python script called backup_script.py and import the necessary libraries.

import psycopg2
import datetime
import subprocess

Next, we need to define the database connection details. Replace the placeholders (HOST, DATABASE, USER, PASSWORD) with your actual database information.

DB_HOST = 'localhost'
DB_PORT = '5432'
DB_NAME = 'your_database'
DB_USER = 'your_username'
DB_PASSWORD = 'your_password'

Now, let’s create a function that performs the backup. It will connect to the database, execute the backup command, and save the backup file in a specified location.

def backup_database():
    try:
        # Connect to the database
        conn = psycopg2.connect(host=DB_HOST, port=DB_PORT, dbname=DB_NAME, user=DB_USER, password=DB_PASSWORD)
        conn.autocommit = True
        cursor = conn.cursor()

        # Create a backup file name with the current date and time
        backup_file = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + '_backup.sql'

        # Execute the backup command
        cursor.execute(f'pg_dump -U {DB_USER} -h {DB_HOST} -p {DB_PORT} -F p -b -v -f {backup_file} {DB_NAME}')

        print('Database backup successful!')

    except (psycopg2.DatabaseError, subprocess.CalledProcessError) as error:
        print(f'Error during database backup: {error}')

    finally:
        # Close the database connection
        if conn:
            cursor.close()
            conn.close()

Scheduling the Backup

Now that we have our backup script ready, we can schedule it to run at regular intervals using Python’s schedule module. Install the schedule library by running:

pip install schedule

Next, update your backup_script.py to include the schedule import and define a function to schedule our backup:

import schedule
import time

def schedule_backup():
    schedule.every(1).day.at("01:00").do(backup_database)  # Schedule backup to run every day at 01:00 AM

    while True:
        schedule.run_pending()
        time.sleep(1)

Finally, run the schedule_backup() function to start the scheduling process.

if __name__ == '__main__':
    schedule_backup()

Conclusion

In this blog post, we have discussed how to schedule and automate backups for a PostgreSQL database using Python. By automating the backup process, we ensure that our valuable database data is protected and can be easily recovered in case of any unexpected issues or failures.

Remember to always test your backup and restore processes to ensure that your backups are working correctly and that you can recover your data successfully when needed.

Feel free to customize the backup script and scheduling frequency based on your specific requirements. Happy coding!