write a backup function that will copy and zip my sqlite db to a blob, then write a function that exports the blob to a cloudfare api endpoint using post and encodable in swift

main.swift
import Foundation

func backupSQLiteDB(dbPath: String) -> Data? {
    do {
        let dbData = try Data(contentsOf: URL(fileURLWithPath: dbPath))
        return try dbData.gzipped()
    } catch {
        print("Error backing up SQLite DB: \(error)")
        return nil
    }
}

func exportBackupToCloudflare(blob: Data, cloudflareURL: URL) {
    var request = URLRequest(url: cloudflareURL)
    request.httpMethod = "POST"
    request.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
    request.httpBody = blob

    let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
        if let error = error {
            print("Error exporting backup to Cloudflare: \(error)")
        }
        
        if let response = response as? HTTPURLResponse {
            print("Response from Cloudflare API: \(response.statusCode)")
        }
    }

    task.resume()
}
910 chars
31 lines

related categories

gistlibby LogSnag