From 4e7b7690ee0b8de2b102e433035e4e0964e86475 Mon Sep 17 00:00:00 2001 From: Revant Nandgaonkar Date: Fri, 27 Mar 2020 16:07:12 +0530 Subject: [PATCH] fix: backup and restore new command FORCE=1 error fixed only push backups if exists prepare and process db restore --- build/common/commands/new.py | 21 ++++++--------------- build/common/commands/push_backup.py | 4 +++- build/common/commands/restore_backup.py | 21 +++++++++++++++++---- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/build/common/commands/new.py b/build/common/commands/new.py index b603458c..5fbe2cb9 100644 --- a/build/common/commands/new.py +++ b/build/common/commands/new.py @@ -29,36 +29,27 @@ def main(): site_config = get_site_config(site_name) - # update User's host to '%' required to connect from any container - command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format( + mysql_command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format( db_host=config.get('db_host'), mariadb_root_username=mariadb_root_username, mariadb_root_password=mariadb_root_password ) - command += "\"UPDATE mysql.user SET Host = '%' where User = '{db_name}'; FLUSH PRIVILEGES;\"".format( + + # update User's host to '%' required to connect from any container + command = mysql_command + "\"UPDATE mysql.user SET Host = '%' where User = '{db_name}'; FLUSH PRIVILEGES;\"".format( db_name=site_config.get('db_name') ) os.system(command) # Set db password - command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format( - db_host=config.get('db_host'), - mariadb_root_username=mariadb_root_username, - mariadb_root_password=mariadb_root_password - ) - command += "\"SET PASSWORD FOR '{db_name}'@'%' = PASSWORD('{db_password}'); FLUSH PRIVILEGES;\"".format( + command = mysql_command + "\"UPDATE mysql.user SET authentication_string = PASSWORD('{db_password}') WHERE User = \'{db_name}\' AND Host = \'%\';\"".format( db_name=site_config.get('db_name'), db_password=site_config.get('db_password') ) os.system(command) # Grant permission to database - command = 'mysql -h{db_host} -u{mariadb_root_username} -p{mariadb_root_password} -e '.format( - db_host=config.get('db_host'), - mariadb_root_username=mariadb_root_username, - mariadb_root_password=mariadb_root_password - ) - command += "\"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%'; FLUSH PRIVILEGES;\"".format( + command = mysql_command + "\"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%'; FLUSH PRIVILEGES;\"".format( db_name=site_config.get('db_name') ) os.system(command) diff --git a/build/common/commands/push_backup.py b/build/common/commands/push_backup.py index 0e291060..e795b3ef 100644 --- a/build/common/commands/push_backup.py +++ b/build/common/commands/push_backup.py @@ -92,6 +92,7 @@ def delete_old_backups(limit, bucket, site_name): backup_limit = int(limit) check_environment_variables() bucket_dir = os.environ.get('BUCKET_DIR') + oldest_backup_date = None s3 = boto3.resource( 's3', @@ -123,7 +124,8 @@ def delete_old_backups(limit, bucket, site_name): print(error) exit(1) - oldest_backup_date = min(all_backup_dates) + if len(all_backup_dates) > 0: + oldest_backup_date = min(all_backup_dates) if len(all_backups) / 3 > backup_limit: oldest_backup = None diff --git a/build/common/commands/restore_backup.py b/build/common/commands/restore_backup.py index f7e14b4a..82854e85 100644 --- a/build/common/commands/restore_backup.py +++ b/build/common/commands/restore_backup.py @@ -8,7 +8,7 @@ import boto3 from push_backup import DATE_FORMAT, check_environment_variables from frappe.utils import get_sites, random_string from frappe.commands.site import _new_site -from frappe.installer import make_conf, get_conf_params +from frappe.installer import make_conf, get_conf_params, make_site_dirs from check_connection import get_site_config, get_config def list_directories(path): @@ -44,8 +44,8 @@ def restore_database(files_base, site): exit(1) db_root_user = os.environ.get("DB_ROOT_USER", 'root') - # restore database + # restore database database_file = files_base + '-database.sql.gz' decompress_db(files_base, site) config = get_config() @@ -58,6 +58,12 @@ def restore_database(files_base, site): db_password=db_root_password ) + # drop db if exists for clean restore + drop_database = mysql_command + "\"DROP DATABASE IF EXISTS \`{db_name}\`;\"".format( + db_name=site_config.get('db_name') + ) + os.system(drop_database) + # create db create_database = mysql_command + "\"CREATE DATABASE IF NOT EXISTS \`{db_name}\`;\"".format( db_name=site_config.get('db_name') @@ -71,6 +77,13 @@ def restore_database(files_base, site): ) os.system(create_user) + # create user password + set_user_password = mysql_command + "\"UPDATE mysql.user SET authentication_string = PASSWORD('{db_password}') WHERE User = \'{db_name}\' AND Host = \'%\';\"".format( + db_name=site_config.get('db_name'), + db_password=site_config.get('db_password') + ) + os.system(set_user_password) + # grant db privileges to user grant_privileges = mysql_command + "\"GRANT ALL PRIVILEGES ON \`{db_name}\`.* TO '{db_name}'@'%'; FLUSH PRIVILEGES;\"".format( db_name=site_config.get('db_name') @@ -162,9 +175,9 @@ def main(): frappe.local.site_path = os.getcwd() + '/' + site make_conf( db_name=site_config.get('db_name'), - db_password=site_config.get('db_name'), + db_password=site_config.get('db_password'), ) - + make_site_dirs() restore_database(files_base, site) restore_private_files(files_base) restore_files(files_base)