MCPcopy Create free account
hub / github.com/aaPanel/BaoTa / InputSql

Method InputSql

class/database.py:1721–1941  ·  view source on GitHub ↗
(self, get)

Source from the content-addressed store, hash-verified

1719
1720 # 导入
1721 def InputSql(self, get):
1722 public.writeFile("/tmp/import_sql.log", "Starting to import the database!")
1723 public.ExecShell("echo '' >> /tmp/import_sql.log")
1724 public.ExecShell("echo '=====================================================' >> /tmp/import_sql.log")
1725 if not os.path.exists(self._MYSQL_BIN):
1726 if os.path.exists("/usr/bin/yum"):
1727 return public.returnMsg(False, "缺少备份工具,请先通过软件管理安装MySQL,或终端执行以下命令安装备份工具:yum install mariadb")
1728 elif os.path.exists("/usr/bin/apt-get"):
1729 return public.returnMsg(False, "缺少备份工具,请先通过软件管理安装MySQL,或终端执行以下命令安装备份工具:apt-get install mariadb-client")
1730 else:
1731 return public.returnMsg(False, "缺少备份工具,请先通过软件管理安装MySQL!")
1732
1733 if not hasattr(get, "name"):
1734 return public.returnMsg(False, "缺少参数!name")
1735 if not hasattr(get, "file"):
1736 return public.returnMsg(False, "缺少参数!file")
1737
1738 zip_password = getattr(get, "password", None)
1739 password_shell = ""
1740 if zip_password:
1741 password_shell = "-P '{zip_password}'".format(zip_password=zip_password)
1742
1743 db_name = get.name
1744 file = get.file
1745
1746 public.ExecShell("echo '|-导入数据库名称:{} ' >> /tmp/import_sql.log".format(db_name))
1747 START_TIME=int(time.time())
1748 public.ExecShell("echo '|-开始导入时间:{} ' >> /tmp/import_sql.log".format(public.getDate()))
1749
1750 if file.count("|") == 2: # 从云存储下载
1751 local_path, cloud_name, file_name = file.split("|")
1752 if not os.path.exists(local_path):
1753 from CloudStoraUpload import CloudStoraUpload
1754 cloud = CloudStoraUpload()
1755 if cloud.run(cloud_name) is False:
1756 return public.returnMsg(False, "连接云存储失败!")
1757 clould_path = os.path.join(cloud.obj.backup_path, "database", "mysql", db_name, file_name)
1758 cloud.cloud_download_file(clould_path, local_path)
1759 if not os.path.exists(local_path):
1760 return public.returnMsg(False, "从云存储下载失败!")
1761 file = local_path
1762
1763 if not os.path.exists(file): return public.returnMsg(False, "导入路径不存在!")
1764 if not os.path.isfile(file): return public.returnMsg(False, "仅支持导入压缩文件!")
1765
1766 password = public.M("config").where("id=?", (1,)).getField("mysql_root")
1767
1768 file_name = os.path.basename(file)
1769 _, file_ext = os.path.splitext(file_name)
1770 if file_name.lower().endswith(".tar.gz"):
1771 file_ext = ".tar.gz"
1772 ext_list = [".sql", ".tar.gz", ".gz", ".zip",".tgz"]
1773 if file_ext not in ext_list:
1774 return public.returnMsg(False, "请选择sql、tar.gz、gz、zip文件格式!")
1775 db_find = public.M("databases").where("name=? AND LOWER(type)=LOWER('mysql')", db_name).find()
1776
1777 mysql_obj = public.get_mysql_obj_by_sid(db_find["sid"])
1778 if not mysql_obj: return public.returnMsg(False, "连接数据库失败!")

Callers 6

import_init_sqlMethod · 0.45
restore_mongodb_dataMethod · 0.45
restore_mongodb_dataMethod · 0.45
restoreFunction · 0.45

Calls 15

runMethod · 0.95
cloud_download_fileMethod · 0.95
IsSqlErrorMethod · 0.95
GetMySQLInfoMethod · 0.95
CheckCloudDatabaseMethod · 0.95
convert_timeMethod · 0.95
ImportSqlErrorMethod · 0.95
CloudStoraUploadClass · 0.90
writeFileMethod · 0.80
ExecShellMethod · 0.80
returnMsgMethod · 0.80
timeMethod · 0.80

Tested by 1

restore_mongodb_dataMethod · 0.36