\x89PNG\r\n\x1a\n\x00\x00\x00\x0DIHDR\x00\x00\x00\x01\x00 \x00\x00\x01\x08\x06\x00\x00\x00\x1F\x15\xC4\x89\x00\x00\x00 \x0AIDATx\x9Ccb\x00\x00\x00\x06\x00\x03\x1A\x05\x9D\x00\x00 \x00\x00IEND\xAE\x42\x60\x82
| Path : /var/www/html/yedekledb24/DatabaseBackupMasterv7/ |
|
B-Con CMD Config cPanel C-Rdp D-Log Info Jump Mass Ransom Symlink vHost Zone-H |
| Current File : /var/www/html/yedekledb24/DatabaseBackupMasterv7/add_encryption_columns.py |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
yedekleDB24 için veritabanı şeması güncelleme scripti.
Bu script, şifreleme için gerekli sütunları ekler.
"""
import os
import sys
import logging
from sqlalchemy import create_engine, text
from sqlalchemy.exc import SQLAlchemyError
# Logging yapılandırması
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def get_database_url():
"""Veritabanı URL'sini çevre değişkeninden alır."""
database_url = os.environ.get('DATABASE_URL')
if not database_url:
logger.error("DATABASE_URL çevre değişkeni bulunamadı.")
sys.exit(1)
return database_url
def add_encryption_columns():
"""Şifreleme için gerekli sütunları ekler."""
try:
# Veritabanı bağlantısı
db_url = get_database_url()
engine = create_engine(db_url)
# Backup_job tablosuna şifreleme sütunları ekle
with engine.connect() as conn:
# İlk olarak sütunların var olup olmadığını kontrol et
backup_job_columns = conn.execute(text("""
SELECT column_name FROM information_schema.columns
WHERE table_name = 'backup_job' AND column_name IN (
'encrypt_backup', 'encryption_type', 'encryption_key_id', 'encryption_passphrase'
)
""")).fetchall()
existing_columns = [col[0] for col in backup_job_columns]
# Eksik sütunları ekle
if 'encrypt_backup' not in existing_columns:
logger.info("backup_job tablosuna encrypt_backup sütunu ekleniyor...")
conn.execute(text("ALTER TABLE backup_job ADD COLUMN encrypt_backup BOOLEAN DEFAULT FALSE"))
if 'encryption_type' not in existing_columns:
logger.info("backup_job tablosuna encryption_type sütunu ekleniyor...")
conn.execute(text("ALTER TABLE backup_job ADD COLUMN encryption_type VARCHAR(16)"))
if 'encryption_key_id' not in existing_columns:
logger.info("backup_job tablosuna encryption_key_id sütunu ekleniyor...")
conn.execute(text("""
ALTER TABLE backup_job ADD COLUMN encryption_key_id INTEGER,
ADD CONSTRAINT fk_backup_job_encryption_key
FOREIGN KEY (encryption_key_id) REFERENCES encryption_key(id)
ON DELETE SET NULL
"""))
if 'encryption_passphrase' not in existing_columns:
logger.info("backup_job tablosuna encryption_passphrase sütunu ekleniyor...")
conn.execute(text("ALTER TABLE backup_job ADD COLUMN encryption_passphrase VARCHAR(256)"))
# Backup_log tablosuna şifreleme sütunları ekle
backup_log_columns = conn.execute(text("""
SELECT column_name FROM information_schema.columns
WHERE table_name = 'backup_log' AND column_name IN ('encrypted', 'encryption_type')
""")).fetchall()
existing_columns = [col[0] for col in backup_log_columns]
if 'encrypted' not in existing_columns:
logger.info("backup_log tablosuna encrypted sütunu ekleniyor...")
conn.execute(text("ALTER TABLE backup_log ADD COLUMN encrypted BOOLEAN DEFAULT FALSE"))
if 'encryption_type' not in existing_columns:
logger.info("backup_log tablosuna encryption_type sütunu ekleniyor...")
conn.execute(text("ALTER TABLE backup_log ADD COLUMN encryption_type VARCHAR(16)"))
# encryption_key tablosunun varlığını kontrol et
table_exists = conn.execute(text("""
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_name = 'encryption_key'
)
""")).scalar()
if not table_exists:
logger.info("encryption_key tablosu oluşturuluyor...")
conn.execute(text("""
CREATE TABLE encryption_key (
id SERIAL PRIMARY KEY,
name VARCHAR(64) NOT NULL,
email VARCHAR(128) NOT NULL,
key_type VARCHAR(16) NOT NULL,
key_length INTEGER NOT NULL DEFAULT 2048,
fingerprint VARCHAR(64) NOT NULL UNIQUE,
public_key TEXT NOT NULL,
private_key TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_by INTEGER REFERENCES "user"(id) ON DELETE SET NULL
)
"""))
conn.commit()
logger.info("Şifreleme sütunları başarıyla eklendi.")
return True
except SQLAlchemyError as e:
logger.error(f"Veritabanı hatası: {str(e)}")
return False
except Exception as e:
logger.error(f"Beklenmeyen hata: {str(e)}")
return False
def main():
"""Ana fonksiyon"""
# Şifreleme sütunlarını ekle
if add_encryption_columns():
logger.info("Şifreleme sütunları başarıyla eklendi.")
return 0
else:
logger.error("Şifreleme sütunları eklenirken hata oluştu.")
return 1
if __name__ == "__main__":
sys.exit(main())