Python Data Backup Scripts and Tools Overview
This article introduces various Python-based data backup techniques, covering standard library modules such as shutil, zipfile, and tarfile, as well as database dump tools like pg_dump and mysqldump, and cloud storage options using awscli or boto3, with example code snippets for each method.
Data backup scripts are essential for protecting data security and ensuring business continuity, especially when hardware failures, software errors, or malicious attacks occur.
shutil
The shutil module in Python’s standard library can copy files and directories, making it useful for simple data backups.
import shutil
shutil.copytree('source_directory', 'backup_directory')zipfile
The zipfile module allows creation, reading, and updating of ZIP archives, suitable for packaging backup data.
import zipfile
with zipfile.ZipFile('backup.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk('directory_to_backup'):
for file in files:
zipf.write(os.path.join(root, file))tarfile
The tarfile module handles TAR format archives for reading and writing.
import tarfile
with tarfile.open('backup.tar.gz', 'w:gz') as tar:
tar.add('directory_to_backup', arcname=os.path.basename('directory_to_backup'))
# Example of using Django dbbackup library
from django.core.management import call_command
call_command('dbbackup', '--clean')pg_dump & psycopg2
For PostgreSQL backups, combine the system pg_dump command with Python’s psycopg2 library.
import subprocess
import psycopg2
conn = psycopg2.connect(database="your_db", user="username", password="password", host="localhost")
subprocess.call(['pg_dump', '-U', 'username', 'your_db', '-f', 'backup.sql'])
conn.close()mysqldump
For MySQL, use the mysqldump command together with MySQLdb or pymysql in Python.
import subprocess
import pymysql
cmd = f"mysqldump -u username -pPassword your_database > backup.sql"
subprocess.call(cmd, shell=True)
conn = pymysql.connect(user='username', passwd='password', db='your_database')
with conn.cursor() as cursor:
cursor.execute("SELECT * INTO OUTFILE 'backup.sql' FROM your_table")
conn.close()awscli or boto3
To back up data to Amazon S3 or other cloud storage, use the AWS CLI tool or its Python SDK boto3 .
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('your_bucket_name')
bucket.upload_file('local_file_path', 'remote_file_key')Note
Choose the appropriate backup method based on the application scenario and data type, and always ensure the security and integrity of the backup process in production environments.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.