# example value for to_exist_db: 'mydb/myuser/supersavepassw,otherdb/otheruser/savepw'
db_props:List=to_exist_db.split("/")
iflen(db_props)==3:
database_name,user_name,user_password=db_props
eliflen(db_props)==1:
database_name=db_props[0]
else:
raiseValueError(
f"Unexpected format in {ValidLabels.auto_create_databases.key}. Got '{to_be_existing_databases_data}'.\nLabel help: {ValidLabels.auto_create_databases.info}"
)
ifnotuser_name:
user_name=self.user
user_password=self.password
# clean input data
database_name=database_name.strip()
user_name=user_name.strip()
user_password=user_password.strip()
# lets go
ifnotuser_nameinexisting_users:
# user does not exists. lest create it
self.executer.container_exec(
command=self.get_create_user_command(
username=user_name,
password=user_password,
executive_db_user_name=executive_user,
executive_db_user_pw=executive_user_pw,
)
)
ifnotdatabase_nameinexisting_databases:
# database does not exists. lets create it
self.executer.container_exec(
command=self.get_create_database_command(
database_name,
enabled_user_name=user_name,
encoding=self.container.coda_labels[
ValidLabels.auto_create_encoding
].val,
collation=self.container.coda_labels[
ValidLabels.auto_create_collation
].val,
executive_db_user_name=executive_user,
executive_db_user_pw=executive_user_pw,
),
)
classMySQLBackupper(BaseBackupper):
bin_dump:str="/usr/bin/mysqldump"
...
...
@@ -208,7 +298,32 @@ class MySQLBackupper(BaseBackupper):
returnf"""{self.bin_cmd} -N -h{self.host} -u{self.user} -p{self.password} -e "SELECT User FROM mysql.user;" """
defget_create_database_command(
self,
database_name,
enabled_user_name,
encoding,
collation,
executive_db_user_name=None,
executive_db_user_pw=None,
):
# character set UTF8mb4 collate utf8mb4_bin
returnf"""{self.bin_cmd} -N -h{self.host} -u{executive_db_user_nameifexecutive_db_user_nameelseself.user} -p{executive_db_user_pwifexecutive_db_user_nameelseself.password} -e "CREATE DATABASE `{database_name}` {"CHARACTER SET = '"+encoding+"'"ifencodingelse""}{"COLLATE = '"+collation+"'"ifcollationelse""}; GRANT ALL privileges ON `{database_name}`.* TO '{enabled_user_name}'@'%';FLUSH PRIVILEGES;" """
defget_create_user_command(
self,
username,
password,
executive_db_user_name=None,
executive_db_user_pw=None,
):
log.warning("Create mysql user with not network restrictions (`@'%'`)")
returnf"""{self.bin_cmd} -N -h{self.host} -u{executive_db_user_nameifexecutive_db_user_nameelseself.user} -p{executive_db_user_pwifexecutive_db_user_nameelseself.password} -e "CREATE USER '{username}'@'%' IDENTIFIED BY '{password}';" """
classPostgresBackupper(BaseBackupper):
...
...
@@ -236,14 +351,27 @@ class PostgresBackupper(BaseBackupper):
returnf"""env PGPASSWORD={self.password}{self.bin_cmd} -U {self.user} -h {self.host} -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;" """
defget_list_user_command(self):
returnf"""env PGPASSWORD={self.password}{self.bin_cmd} -U {self.user} -h {self.host} -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;" """
info="The user to create a missing database (must have the priviledge to create a database)?",
info=f"The user to create a missing database (must have the priviledge to create a database). Defaults to backup user from label `{database_username.key}`?",
info="Databases, Users and Password to create. Multiple entries seperated by comma are possible. \nformat:\n'<databasename>/<username>/<password>'\nExample:\n'mydb/myuser/supersave,otherdb/otheruser/savepw'",