mirror of
				https://github.com/kevinveenbirkenbach/docker-volume-backup.git
				synced 2025-11-04 10:38:00 +00:00 
			
		
		
		
	Added instance attribut
This commit is contained in:
		@@ -35,20 +35,33 @@ def create_backup_directories(base_dir, machine_id, repository_name, backup_time
 | 
				
			|||||||
    pathlib.Path(version_dir).mkdir(parents=True, exist_ok=True)
 | 
					    pathlib.Path(version_dir).mkdir(parents=True, exist_ok=True)
 | 
				
			||||||
    return version_dir
 | 
					    return version_dir
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Muss modifiziert werden. 
 | 
					def get_instance(container):
 | 
				
			||||||
def get_database_name(container):
 | 
					    instance_name = re.split("(_|-)(database|db|postgres)", container)[0]
 | 
				
			||||||
    database_name = re.split("(_|-)(database|db)", container)[0]
 | 
					    print(f"Extracted instance name: {instance_name}")
 | 
				
			||||||
    print(f"Extracted database name: {database_name}")
 | 
					    return instance_name
 | 
				
			||||||
    return database_name
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def backup_database(container, databases, version_dir, db_type):
 | 
					def backup_database(container, databases, version_dir, db_type):
 | 
				
			||||||
    """Backup database (MariaDB or PostgreSQL) if applicable."""
 | 
					    """Backup database (MariaDB or PostgreSQL) if applicable."""
 | 
				
			||||||
    print(f"Starting database backup for {container} using {db_type}...")
 | 
					    print(f"Starting database backup for {container} using {db_type}...")
 | 
				
			||||||
    database_name = get_database_name(container)
 | 
					    instance_name = get_instance(container)
 | 
				
			||||||
    database_entry = databases.loc[databases['database'] == database_name].iloc[0]
 | 
					
 | 
				
			||||||
 | 
					    # Filter the DataFrame for the given instance_name
 | 
				
			||||||
 | 
					    database_entries = databases.loc[databases['instance'] == instance_name]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Check if there are more than one entries
 | 
				
			||||||
 | 
					    if len(database_entries) > 1:
 | 
				
			||||||
 | 
					        raise BackupException(f"More than one entry found for instance '{instance_name}'")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Check if there is no entry
 | 
				
			||||||
 | 
					    if database_entries.empty:
 | 
				
			||||||
 | 
					        raise BackupException(f"No entry found for instance '{instance_name}'")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Get the first (and only) entry
 | 
				
			||||||
 | 
					    database_entry = database_entries.iloc[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    backup_destination_dir = os.path.join(version_dir, "sql")
 | 
					    backup_destination_dir = os.path.join(version_dir, "sql")
 | 
				
			||||||
    pathlib.Path(backup_destination_dir).mkdir(parents=True, exist_ok=True)
 | 
					    pathlib.Path(backup_destination_dir).mkdir(parents=True, exist_ok=True)
 | 
				
			||||||
    backup_destination_file = os.path.join(backup_destination_dir, f"{database_name}_backup.sql")
 | 
					    backup_destination_file = os.path.join(backup_destination_dir, f"backup.sql")
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if db_type == 'mariadb':
 | 
					    if db_type == 'mariadb':
 | 
				
			||||||
        backup_command = f"docker exec {container} /usr/bin/mariadb-dump -u {database_entry['username']} -p{database_entry['password']} {database_entry['database']} > {backup_destination_file}"
 | 
					        backup_command = f"docker exec {container} /usr/bin/mariadb-dump -u {database_entry['username']} -p{database_entry['password']} {database_entry['database']} > {backup_destination_file}"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,17 +2,17 @@ import pandas as pd
 | 
				
			|||||||
import argparse
 | 
					import argparse
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def check_and_add_entry(file_path, host, database, username, password):
 | 
					def check_and_add_entry(file_path, instance, host, database, username, password):
 | 
				
			||||||
    # Check if the file exists and is not empty
 | 
					    # Check if the file exists and is not empty
 | 
				
			||||||
    if os.path.exists(file_path) and os.path.getsize(file_path) > 0:
 | 
					    if os.path.exists(file_path) and os.path.getsize(file_path) > 0:
 | 
				
			||||||
        # Read the existing CSV file with header
 | 
					        # Read the existing CSV file with header
 | 
				
			||||||
        df = pd.read_csv(file_path, sep=';')
 | 
					        df = pd.read_csv(file_path, sep=';')
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        # Create a new DataFrame with columns if file does not exist
 | 
					        # Create a new DataFrame with columns if file does not exist
 | 
				
			||||||
        df = pd.DataFrame(columns=['host', 'database', 'username', 'password'])
 | 
					        df = pd.DataFrame(columns=['instance','host', 'database', 'username', 'password'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Check if the entry exists and remove it
 | 
					    # Check if the entry exists and remove it
 | 
				
			||||||
    mask = (df['host'] == host) & (df['database'] == database) & (df['username'] == username)
 | 
					    mask = (df['instance'] == instance) & (df['host'] == host) & (df['database'] == database) & (df['username'] == username)
 | 
				
			||||||
    if not df[mask].empty:
 | 
					    if not df[mask].empty:
 | 
				
			||||||
        print("Replacing existing entry.")
 | 
					        print("Replacing existing entry.")
 | 
				
			||||||
        df = df[~mask]
 | 
					        df = df[~mask]
 | 
				
			||||||
@@ -20,7 +20,7 @@ def check_and_add_entry(file_path, host, database, username, password):
 | 
				
			|||||||
        print("Adding new entry.")
 | 
					        print("Adding new entry.")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Create a new DataFrame for the new entry
 | 
					    # Create a new DataFrame for the new entry
 | 
				
			||||||
    new_entry = pd.DataFrame([{'host': host, 'database': database, 'username': username, 'password': password}])
 | 
					    new_entry = pd.DataFrame([{'instance': instance, 'host': host, 'database': database, 'username': username, 'password': password}])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Add (or replace) the entry using concat
 | 
					    # Add (or replace) the entry using concat
 | 
				
			||||||
    df = pd.concat([df, new_entry], ignore_index=True)
 | 
					    df = pd.concat([df, new_entry], ignore_index=True)
 | 
				
			||||||
@@ -31,6 +31,7 @@ def check_and_add_entry(file_path, host, database, username, password):
 | 
				
			|||||||
def main():
 | 
					def main():
 | 
				
			||||||
    parser = argparse.ArgumentParser(description="Check and replace (or add) a database entry in a CSV file.")
 | 
					    parser = argparse.ArgumentParser(description="Check and replace (or add) a database entry in a CSV file.")
 | 
				
			||||||
    parser.add_argument("file_path", help="Path to the CSV file")
 | 
					    parser.add_argument("file_path", help="Path to the CSV file")
 | 
				
			||||||
 | 
					    parser.add_argument("instance", help="Database instance")
 | 
				
			||||||
    parser.add_argument("host", help="Database host")
 | 
					    parser.add_argument("host", help="Database host")
 | 
				
			||||||
    parser.add_argument("database", help="Database name")
 | 
					    parser.add_argument("database", help="Database name")
 | 
				
			||||||
    parser.add_argument("username", help="Username")
 | 
					    parser.add_argument("username", help="Username")
 | 
				
			||||||
@@ -38,7 +39,7 @@ def main():
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    args = parser.parse_args()
 | 
					    args = parser.parse_args()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    check_and_add_entry(args.file_path, args.host, args.database, args.username, args.password)
 | 
					    check_and_add_entry(args.file_path, args.instance, args.host, args.database, args.username, args.password)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    main()
 | 
					    main()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user