68 lines
2.7 KiB
Python
68 lines
2.7 KiB
Python
import os
|
|
import zipfile
|
|
from datetime import datetime
|
|
from config.settings import BASE_DIR
|
|
import boto3
|
|
import botocore
|
|
|
|
# Configuration for Vultr S3 storage
|
|
script_klasoru = BASE_DIR
|
|
haric_dosya_uzantilari = ['.zip']
|
|
excluded_folders = ['venv', 'yedek', '.idea']
|
|
hostname = "ams1.vultrobjects.com"
|
|
secret_key = "Ec1pq3OQAObFLOQrfAVqJKhDAk4BkT7OqgYszlef"
|
|
access_key = "KQAOMJ8CQ8HP4CY23YPK"
|
|
|
|
def zip_klasor(ziplenecek_klasor, hedef_zip_adi, haric_klasorler=[], haric_dosya_uzantilari=[]):
|
|
"""Zip the target folder excluding specified folders and file extensions."""
|
|
with zipfile.ZipFile(hedef_zip_adi, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
|
for klasor_yolu, _, dosya_listesi in os.walk(ziplenecek_klasor):
|
|
if not any(k in klasor_yolu for k in haric_klasorler):
|
|
for dosya in dosya_listesi:
|
|
dosya_yolu = os.path.join(klasor_yolu, dosya)
|
|
if not any(dosya_yolu.endswith(ext) for ext in haric_dosya_uzantilari):
|
|
zipf.write(dosya_yolu, os.path.relpath(dosya_yolu, ziplenecek_klasor))
|
|
|
|
def job(folder_name):
|
|
"""Create a zip backup and upload it to Vultr S3."""
|
|
session = boto3.session.Session()
|
|
client = session.client('s3', region_name='ams1',
|
|
endpoint_url=f'https://{hostname}',
|
|
aws_access_key_id=access_key,
|
|
aws_secret_access_key=secret_key,
|
|
config=botocore.client.Config(signature_version='s3v4'))
|
|
|
|
output_zip = os.path.join(os.getcwd(), f"{folder_name}.zip")
|
|
zip_klasor(script_klasoru, output_zip, excluded_folders, haric_dosya_uzantilari)
|
|
|
|
try:
|
|
# Ensure the bucket exists
|
|
try:
|
|
client.head_bucket(Bucket=folder_name)
|
|
print(f"Bucket already exists: {folder_name}")
|
|
except botocore.exceptions.ClientError as e:
|
|
if e.response['Error']['Code'] == '404':
|
|
print(f"Bucket not found, creating: {folder_name}")
|
|
client.create_bucket(Bucket=folder_name)
|
|
|
|
# Upload the file to S3 using boto3 (avoids XAmzContentSHA256Mismatch)
|
|
try:
|
|
client.upload_file(
|
|
output_zip,
|
|
folder_name,
|
|
os.path.basename(output_zip),
|
|
ExtraArgs={
|
|
'ACL': 'public-read',
|
|
'ContentType': 'application/zip'
|
|
}
|
|
)
|
|
print(f"File successfully uploaded: {output_zip}")
|
|
except Exception as e:
|
|
print(f"Upload error: {e}")
|
|
|
|
finally:
|
|
# Clean up local zip file
|
|
if os.path.exists(output_zip):
|
|
os.remove(output_zip)
|
|
print(f"Local zip file deleted: {output_zip}")
|