From f189c8d175a92ce7484be803ab9baf7bdd9e1728 Mon Sep 17 00:00:00 2001 From: harshadyeola Date: Mon, 12 Jan 2015 14:53:09 +0530 Subject: [PATCH] site backup module added --- ee/cli/plugins/site_functions.py | 35 ++++++++++++++++-- ee/core/fileutils.py | 61 ++++++++++++++++++++++++-------- ee/core/variables.py | 3 ++ 3 files changed, 83 insertions(+), 16 deletions(-) diff --git a/ee/cli/plugins/site_functions.py b/ee/cli/plugins/site_functions.py index 3ec10f92..3c5f3b12 100644 --- a/ee/cli/plugins/site_functions.py +++ b/ee/cli/plugins/site_functions.py @@ -8,6 +8,7 @@ from ee.core.mysql import EEMysql from ee.core.shellexec import EEShellExec from ee.core.variables import EEVariables from ee.core.logging import Log +import glob def SetupDomain(self, data): @@ -141,7 +142,8 @@ def SetupWordpress(self, data): EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot)) EEShellExec.cmd_exec(self, "wp --allow-root core download") - data = SetupDatabase(self, data) + if not (data['ee_db_name'] and data['ee_db_user'] and data['ee_db_pass']): + data = SetupDatabase(self, data) if prompt_wpprefix == 'True' or prompt_wpprefix == 'true': try: ee_wp_prefix = input('Enter the WordPress table prefix [wp_]: ' @@ -247,7 +249,9 @@ def SetupWordpressNetwork(self, data): ee_site_webroot = data['webroot'] EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot)) EEShellExec.cmd_exec(self, 'wp --allow-root core multisite-convert' - '--title=') + '--title={0} {subdomains}' + .format(data['www_domain'], subdomains='--subdomains' + if not data['wpsubdir'] else '')) def InstallWP_Plugin(self, plugin_name, data): @@ -271,3 +275,30 @@ def SetWebrootPermissions(self, webroot): self.app.log.debug("Setting Up Permissions...") EEFileUtils.chown(self, webroot, EEVariables.ee_php_user, EEVariables.ee_php_user, recursive=True) + + +def siteBackup(self, data): + ee_site_webroot = data['webroot'] + backup_path = ee_site_webroot + '/backup/{0}'.format(EEVariables.ee_date) + if not EEFileUtils.isexist(self, backup_path): + EEFileUtils.mkdir(self, backup_path) + Log.info(self, "Backup Location : {0}".format(backup_path)) + EEFileUtils.copyfile(self, '/etc/nginx/sites-available/{0}.conf' + .format(data['ee_domain']), backup_path) + + if data['currsitetype'] in ['html', 'php', 'mysql']: + Log.info(self, "Backup Webroot ...") + EEFileUtils.mvfile(self, ee_site_webroot + '/htdocs', backup_path) + + configfiles = glob(ee_site_webroot + '/htdocs/*-config.php') + + for file in configfiles: + if EEFileUtils.isexist(self, file): + ee_db_name = (EEFileUtils.grep(self, file, 'DB_NAME').split(',')[1] + .split(')')[0].strip()) + Log.info(self, 'Backup Database, please wait') + EEShellExec.cmd_exec(self, "mysqldump {0} > {1}/{0}.sql" + .format(ee_db_name, backup_path), + "Failed: Backup Database") + # move wp-config.php/ee-config.php to backup + EEFileUtils.mvfile(self, file, backup_path) diff --git a/ee/core/fileutils.py b/ee/core/fileutils.py index 654970ca..90c95e24 100644 --- a/ee/core/fileutils.py +++ b/ee/core/fileutils.py @@ -5,6 +5,7 @@ import sys import glob import shutil import fileinput +from ee.core.logging import Log class EEFileUtils(): @@ -25,26 +26,29 @@ class EEFileUtils(): shutil.rmtree(file) self.app.log.info("Done") except shutil.Error as e: - self.app.log.error('Unable to Remove file {err}' - .format(err=str(e.reason))) + Log.error(self, 'Unable to Remove file {err}' + .format(err=str(e.reason))) sys.exit(1) - def create_symlink(self, paths): + def create_symlink(self, paths, errormsg=''): src = paths[0] dst = paths[1] try: os.symlink(src, dst) except Exception as e: - self.app.log.error("Unable to create symbolic link ...\n {0} {1}" - .format(e.errno, e.strerror)) + if errormsg: + Log.error(self, errormsg + 'n {0} {1}'. + format(e.errno, e.strerror)) + Log.debug(self, "Unable to create symbolic link ...\n {0} {1}" + .format(e.errno, e.strerror)) sys.exit(1) def remove_symlink(self, filepath): try: os.unlink(filepath) except Exception as e: - self.app.log.error("Unable to reomove symbolic link ...\n {0} {1}" - .format(e.errno, e.strerror)) + Log.error(self, "Unable to reomove symbolic link ...\n {0} {1}" + .format(e.errno, e.strerror)) sys.exit(1) def copyfile(self, src, dest): @@ -67,16 +71,16 @@ class EEFileUtils(): try: shutil.move(src, dst) except shutil.Error as e: - self.app.log.error('Unable to move file {err}' - .format(err=str(e.reason))) + Log.error(self, 'Unable to move file {err}' + .format(err=str(e.reason))) sys.exit(1) def chdir(self, path): try: os.chdir(path) except OSError as e: - self.app.log.error('Unable to Change Directory {err}' - .format(err=e.strerror)) + Log.error(self, 'Unable to Change Directory {err}' + .format(err=e.strerror)) sys.exit(1) def chown(self, path, user, group, recursive=False): @@ -92,10 +96,10 @@ class EEFileUtils(): else: shutil.chown(path, user=user, group=group) except shutil.Error as e: - self.app.log.error("Unable to change owner : {0} ".format(e)) + Log.error(self, "Unable to change owner : {0} ".format(e)) sys.exit(1) except Exception as e: - self.app.log.error("Unable to change owner {0}".format(e)) + Log.error(self, "Unable to change owner {0}".format(e)) sys.exit(1) def chmod(self, path, perm, recursive=False): @@ -109,5 +113,34 @@ class EEFileUtils(): else: os.chmod(path, perm) except OSError as e: - self.log.error("Unable to change owner {0}".format(e.strerror)) + Log.error(self, "Unable to change owner {0}".format(e.strerror)) + sys.exit(1) + + def mkdir(self, path): + try: + os.makedirs(path) + except OSError as e: + Log.error(self, "Unable to create directory {0}" + .format(e.strerror)) + sys.exit(1) + + def isexist(self, path): + try: + if os.path.exists(path): + return (True) + else: + return (False) + except OSError as e: + Log.error(self, "Unable to check path {0}" + .format(e.strerror)) + sys.exit(1) + + def grep(self, fnm, sstr): + try: + for line in open(fnm): + if sstr in line: + return line + except OSError as e: + Log.error(self, "Unable to Search string {0}" + .format(e.strerror)) sys.exit(1) diff --git a/ee/core/variables.py b/ee/core/variables.py index f804ed03..38f8bc31 100644 --- a/ee/core/variables.py +++ b/ee/core/variables.py @@ -5,6 +5,7 @@ import configparser import os import sys import psutil +import datetime class EEVariables(): @@ -12,6 +13,8 @@ class EEVariables(): config = configparser.ConfigParser() config.read(os.path.expanduser("~")+'/.gitconfig') + ee_date = datetime.datetime.now().strftime('%d%b%Y%H%M%S') + # EasyEngine core variables ee_platform_distro = platform.linux_distribution()[0] ee_platform_version = platform.linux_distribution()[1]