/usr/share/mysql-workbench/mysqlwbmeb.py is in mysql-workbench-data 6.3.6+dfsg-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 | #!/usr/bin/env python
# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 of the
# License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
import os
import sys
import StringIO
import ConfigParser
from time import strptime, strftime
import re
import subprocess
import logging
import json
is_library = True
def call_system(command, spawn, output_handler = None):
result = 0
logging.info('Executing command: %s' % command)
if spawn or output_handler is None:
try:
if os.fork() != 0:
return result
os.setpgrp()
for i in range(0,100):
try:
os.close(i)
except:
pass
os.execvp("/bin/sh", ["/bin/sh", "-c", command])
except OSError, e:
logging.error('Error on command execution: %s' % str(e))
result = 1
else:
child = subprocess.Popen(command, bufsize=0, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True, preexec_fn=os.setpgrp)
if not spawn:
if output_handler:
for line in iter(child.stdout.readline, ""):
output_handler(line)
child.wait()
result = child.returncode
return result
def check_version_at_least(command, major, minor, revno):
output = StringIO.StringIO()
ret_val = call_system("%s --version" % command, False, output.write)
if ret_val == 0:
tokens = output.getvalue().strip().split()
version = ""
found_major = 0
found_minor = 0
found_revno = 0
for token in tokens:
if token == 'version':
version = 'found'
else:
if version == 'found':
version = token
version_tokens = version.split('.')
found_major = int(version_tokens[0])
found_minor = int(version_tokens[1])
found_revno = int(version_tokens[2])
return (found_major, found_minor, found_revno) >= (major, minor, revno)
class ConfigReader(object):
def __init__(self, file):
self._file = file
profile_file = open(file, 'r')
data = profile_file.read()
self.doc = ConfigParser.ConfigParser()
self.doc.readfp(StringIO.StringIO(data))
def read_value(self, section, item, mandatory = False, default = None):
value = default
try:
value = self.doc.get(section, item)
except:
if mandatory:
raise
else:
pass
return value
class ConfigUpdater(object):
def __init__(self):
self.config = None
def load_config(self, file_name):
self.config = ConfigReader(file_name)
def update(self, file_name):
my_source_handler = open(file_name, 'r')
my_target_handler = open(file_name + ".tmp", 'w')
current_section = None
for line in my_source_handler:
line = line.strip()
if line.startswith('[') and line.endswith(']'):
current_section = line[1:-1]
elif not line.startswith('#') and line.find('=') != -1:
att, val = line.split('=', 2)
att = att.strip()
val = val.strip()
if current_section:
new_val = self.config.read_value(current_section, att, False, None)
if new_val is not None:
line = '%s = %s' % (att, new_val)
my_target_handler.write('%s\n' % line)
my_source_handler.close()
my_target_handler.close()
os.remove(file_name)
os.rename(file_name + ".tmp", file_name)
class MEBCommand(object):
def __init__(self, params = None, output_handler = None):
self.params = params
self.output_handler = output_handler
def read_params(self):
pass
def print_usage(self):
pass
def execute(self):
if self.read_params():
print "ERROR: Missing command execution logic"
else:
self.print_usage()
return 1
# The parameter count increases in 2 when run as a script
# The first two parameters are the script name and the command identifier
def param_count(self):
return len(self.params) if is_library else len(self.params) - 2
def get_param(self, index):
real_index = index
if not is_library:
real_index += 2
return self.params[real_index]
def write_output(self, output):
if self.output_handler:
self.output_handler(output + '\n')
else:
print output
class MEBCommandProcessor(MEBCommand):
def __init__(self, params = None, output_handler = None):
super(MEBCommandProcessor, self).__init__(params, output_handler)
self.help_needed = False
self._commands = {}
self._commands['BACKUP'] = MEBBackup
self._commands['VERSION'] = MEBHelperVersion
self._commands['GET_PROFILES'] = MEBGetProfiles
self._commands['UPDATE_SCHEDULING'] = MEBUpdateScheduling
self._commands['PROPAGATE_DATA'] = MEBPropagateSettings
def read_params(self):
ret_val = True
try:
self.command = sys.argv[1]
if self.command.upper() == 'HELP':
self.command = sys.argv[2]
self.help_needed = True
except IndexError:
print "Error executing helper, use it as follows:"
logging.error("Unable to execute %s with no parameters." % __file__)
ret_val = False
return ret_val
def print_usage(self):
self.write_output("mysqlwbmeb <command> <parameters>")
self.write_output("WHERE : <command> : is one of %s" % ','.join(self._commands.keys()))
self.write_output(" <parameters> : are the parameters needed for each command.")
self.write_output("\nYou can also use as follows to get each command parameters:\n")
self.write_output("mysqlwbmeb help <command>")
self.write_output("WHERE : <command> : is one of %s\n" % self._commands.keys())
def execute(self):
processed = False
ret_val = 1
if self.read_params():
command = self.command.upper()
if self._commands.has_key(command):
klass = self._commands[command]
instance = klass(self.params, self.output_handler)
processed = True
if self.help_needed:
instance.print_usage()
ret_val = 0
else:
ret_val = instance.execute()
if not processed:
self.write_output("\nERROR Executing mysqlwbmeb\n")
self.print_usage()
return ret_val
class MEBBackup(MEBCommand):
def __init__(self, params = None, output_handler = None):
super(MEBBackup, self).__init__(params, output_handler)
self.file_name=''
self.time_format = '%Y-%m-%d_%H-%M-%S'
self.time_format_re = re.compile("\\b[1-9]\\d\\d\\d\\b-[0-1]\\d-[0-3]\\d_[0-2]\\d-[0-5]\\d-[0-5]\\d")
self.backup_ok_re = re.compile("^mysqlbackup completed OK!$") # according to http://dev.mysql.com/doc/mysql-enterprise-backup/3.10/en/mysqlbackup.html, we can rely on this appearing at the end of the backup log when the backup succeeds
def print_usage(self):
self.write_output("BACKUP <profile> <compress> <incremental> <to_single_file> <report_progress> <command>")
self.write_output("WHERE : <profile> : is the UUID of the profile to be used for the backup process.")
self.write_output(" <compress> : indicates if the backup will be compressed. (1 or 0)")
self.write_output(" <incremental> : indicates the backup should be incremental. (1 or 0)")
self.write_output(" <to_single_file> : indicates the backup will be done to an image file. (1 or 0)")
self.write_output(" <report_progress>: indicates the backup should be compressed. (1 or 0)")
self.write_output(" <command> : indicates the backup operation to be done, could be backup or backup-and-apply-log")
self.write_output(" [file_name] : indicates the target name for the backup file or folder\n\n")
def read_bool_param(self, param):
true_param = ['1', 'TRUE', 'YES']
return param.upper() in true_param;
def read_params(self):
ret_val = False
if self.param_count() >= 6:
self.profile_file = self.get_param(0)
self.compress = self.read_bool_param(self.get_param(1))
self.incremental = self.read_bool_param(self.get_param(2))
self.to_single_file = self.read_bool_param(self.get_param(3))
self.report_progress = self.read_bool_param(self.get_param(4))
self.bkcommand = self.get_param(5)
if self.param_count() > 6:
self.file_name = self.get_param(6)
ret_val = True
return ret_val
def read_profile_data(self):
# Gets the path to this file which should be on the
# backups home directory
this_file_path = os.path.realpath(__file__)
backups_home = os.path.dirname(this_file_path)
# Creates the full path to the profile file
self.profile_file = os.path.join(backups_home, self.profile_file)
# Loads the information to be used to create the command call
profile = ConfigReader(self.profile_file)
self.command = profile.read_value('meb_manager', 'command', True, "")
self.backup_dir = profile.read_value('mysqlbackup', 'backup_dir', True, "")
self.inc_backup_dir = profile.read_value('mysqlbackup', 'incremental_backup_dir', True, "")
self.use_tts = profile.read_value('meb_manager', 'using_tts', False, "0")
self.compress_method = profile.read_value('meb_manager', 'compress_method', False, 'lz4')
self.compress_level = profile.read_value('meb_manager', 'compress_level', False, '1')
self.skip_unused_pages = profile.read_value('meb_manager', 'skip_unused_pages', False, False) == 'True'
def set_backup_paths(self):
target_folder = ''
# Defines the target file/folder name for the backup
if self.file_name != '':
if self.to_single_file:
if self.file_name.lower()[-4:] != '.mbi':
self.file_name += '.mbi'
# On an image backup the backup dir will be the one
# Received as a parameter
self.backup_dir = self.file_name[:-4]
self.log_path = self.file_name.replace('.mbi', '.log')
else:
# If a file name is passed it is used as the backup folder
target_folder = self.file_name
else:
# If no file name is passed, uses the timestamp to create one
target_folder = strftime(self.time_format)
# The full path is the target folder under the backups home for
# the profile
if target_folder:
self.backup_dir = os.path.join(self.backup_dir, target_folder)
self.log_path = self.backup_dir + '.log'
def prepare_command(self):
ret_val = True
self.command_call = '"%s" --defaults-file="%s"' % (self.command, self.profile_file)
# Adds the compress parameter if needed
if self.compress:
if check_version_at_least(self.command, 3, 10, 0):
# lz4 is the default so id it is selected only sets the --compress option
if self.compress_method == 'lz4':
self.command_call += " --compress"
# Otherwise using --compress-method makes --compress to be not needed
else:
self.command_call += " --compress-method=%s" % self.compress_method
# Level is only specified if not using the default value
if self.compress_level != '1':
self.command_call += " --compress-level=%s" % self.compress_level
else:
self.command_call += " --compress"
# Get the right path parameter, path and running type
path_param = " --backup-dir"
# If the backup is incremental
if self.incremental:
base_folder = self.get_incremental_base_folder()
if base_folder:
self.command_call += ' --incremental --incremental-base=dir:"%s"' % base_folder
self.backup_dir = self.inc_backup_dir
path_param = ' --incremental-backup-dir'
else:
self.write_output("ERROR: Unable to run incremental backup without a base folder.")
ret_val = False
elif self.skip_unused_pages:
self.command_call += " --skip-unused-pages"
# Sets the needed backup paths
self.set_backup_paths()
# Adds the backup folder to the command
self.command_call += ' %s="%s"' % (path_param, self.backup_dir)
if self.to_single_file:
self.command_call += ' --backup-image=%s' % self.file_name
if self.use_tts != "0":
tts_value="with-minimum-locking"
if self.use_tts=="2":
tts_value="with-full-locking"
self.command_call += " --use-tts=%s" % tts_value
if self.report_progress:
self.command_call += ' --show-progress=stdout'
self.command_call += ' %s > "%s" 2>&1' % (self.bkcommand, self.log_path)
return ret_val
def execute(self):
ret_val = 1
if self.read_params():
# Loads the profile information to be used on the
# explicitly on the command call
self.read_profile_data()
if self.prepare_command():
# Spawns the backup command execution
ret_val = call_system(self.command_call, True)
# Prints the data to be returned back
self.write_output(json.dumps({'LOG_PATH':self.log_path}))
else:
self.print_usage()
return ret_val
def get_incremental_base_folder(self):
base_folder=''
base_tstamp=strptime('1900-01-01_00-00-00', self.time_format)
lastest_full = self.find_lastest_backup(self.backup_dir, base_tstamp)
if lastest_full > base_tstamp:
base_folder = self.backup_dir
base_tstamp = lastest_full
lastest_inc = self.find_lastest_backup(self.inc_backup_dir, base_tstamp)
if lastest_inc > base_tstamp:
base_tstamp = lastest_inc
base_folder = self.inc_backup_dir
if base_folder:
base_folder = os.path.join(base_folder, strftime(self.time_format, base_tstamp))
return base_folder
def find_lastest_backup(self, path, base_time):
sub_folders = [name for name in os.listdir(path) if os.path.isdir(os.path.join(path, name))]
sub_folders.sort(reverse=True) # sort backups in newest-to-oldest order
lastest_time = base_time
# find the most-recent valid backup
for folder in sub_folders:
if self.time_format_re.match(folder):
if self.is_backup_dir_valid(os.path.join(path,folder)):
lastest_time = strptime(folder, self.time_format)
break
return lastest_time
def is_backup_dir_valid(self, folder):
log_filename = folder + '.log' # each /path/to/backup/dir has a corresponding /path/to/backup/dir.log
# grep log file for the flag message
f = open(log_filename, 'r')
for line in f:
if self.backup_ok_re.match(line):
return True
return False
class MEBUpdateScheduling(MEBCommand):
def __init__(self, params = None, output_handler = None):
super(MEBUpdateScheduling, self).__init__(params, output_handler)
# Initializes these to False so they serve their purpose
# in the case of a DELETE
self.new_fb_schedule = "False"
self.new_ib_schedule = "False"
def print_usage(self):
self.write_output("UPDATE_SCHEDULING <profile> <old_label> <old_full> <old_incremental>")
self.write_output("WHERE : <change> : Indicate the operation being done with the profile: NEW, UPDATE, DELETE.")
self.write_output(" <profile> : is the UUID of the profile to be used for the scheduling.")
self.write_output(" [<old_label>] : indicates the label under which the jobs were scheduled. Applies on UPDATE and DELETE changes.")
self.write_output(" [<old_full>] : indicates if the profile was scheduled for full backup. (0 or 1). Applies on UPDATE and DELETE changes.")
self.write_output(" [<old_incremental>]: indicates if the profile was scheduled for incremental backup. (0 or 1). Applies on UPDATE and DELETE changes.\n\n")
def read_params(self):
ret_val = False
if self.param_count() >= 2:
self.change = self.get_param(0)
if self.change == "NEW":
param_count = 2
else:
param_count = 5
if self.param_count() == param_count:
self.uuid = self.get_param(1)
self.profile_file = '%s.cnf' % self.uuid
if self.change != "NEW":
self.old_label = self.get_param(2)
self.old_fb_schedule = self.get_param(3)
self.old_ib_schedule = self.get_param(4)
ret_val = True
return ret_val
def init_profile(self):
# Gets the path to this file which should be on the
# backups home directory
this_file_path = os.path.realpath(__file__)
backups_home = os.path.dirname(this_file_path)
# Creates the full path to the profile file
self.profile_file = os.path.join(backups_home, self.profile_file)
# Loads the information to be used to create the command call
self.profile = ConfigReader(self.profile_file)
self.new_label = self.profile.read_value("meb_manager", "label", "")
self.uuid = self.profile.read_value("meb_manager", "uuid", "")
self.new_fb_schedule = self.profile.read_value("meb_manager", "full_backups_enabled", "")
self.new_ib_schedule = self.profile.read_value("meb_manager", "inc_backups_enabled", "")
self.compress = self.profile.read_value("meb_manager", "compress", "")
self.apply_log = self.profile.read_value("meb_manager", "apply_log", "")
self.backup_dir = self.profile.read_value('mysqlbackup', 'backup_dir', True, "")
def read_profile_data(self, backup_type):
self.frequency = self.profile.read_value("meb_manager", backup_type + "_backups_frequency", "")
self.month_day = self.profile.read_value("meb_manager", backup_type + "_backups_month_day", "")
self.week_days = self.profile.read_value("meb_manager", backup_type + "_backups_week_days", "")
self.hour = self.profile.read_value("meb_manager", backup_type + "_backups_hour", "")
self.minute = self.profile.read_value("meb_manager", backup_type + "_backups_minute", "")
print "Loaded data for type : %s" % backup_type
print "Frequency: %s" % self.frequency
print "Month Day: %s" % self.month_day
print "Week Days: %s" % self.week_days
print "Hour: %s" % self.hour
print "Minute: %s" % self.minute
def get_unschedule_command(self, backup_type):
cron_file = "%s/wb_cron_file" % os.path.dirname(__file__)
if backup_type == 'full':
command = "crontab -l | grep -P -v '%s.*%s\.cnf\s\d\s0' > '%s'; crontab '%s';rm '%s'" % (__file__, self.uuid, cron_file, cron_file, cron_file)
else:
command = "crontab -l | grep -P -v '%s.*%s\.cnf\s\d\s1' > '%s'; crontab '%s';rm '%s'" % (__file__, self.uuid, cron_file, cron_file, cron_file)
return command
def create_backup_command_call(self, backup_type):
cmd_data = []
cmd_data.append('"%s"' % __file__)
# Appends the command to be handled by the helper
cmd_data.append('BACKUP')
# Uses the configuration file for the backup (MUST be the 1st option)
cmd_data.append('%s.cnf' % self.uuid)
real_compress = "0"
real_incremental = "0"
if backup_type == 'full':
# The compress option is set only for full backups when apply-log is not
# enabled, this is because --compress will be ignored anyway
if self.compress and not self.apply_log:
# 1 to indicate compress, 0 to indicate NOT incremental
real_compress = "1"
elif backup_type == 'inc':
real_incremental = "1"
cmd_data.append(real_compress)
cmd_data.append(real_incremental)
cmd_data.append("0")
cmd_data.append("1")
if self.apply_log == "True" and backup_type == 'full':
cmd_data.append('backup-and-apply-log')
else:
cmd_data.append('backup')
return " ".join(cmd_data)
def get_schedule_command(self, backup_type):
# Configures the backup and log target paths
log_path = self.backup_dir
target_path = "\$BACKUP_NAME.log"
if backup_type == "inc":
target_path = "inc/\$BACKUP_NAME.log"
log_path = os.path.join(log_path, target_path)
# Creates the mysqlbackup command call
command = self.create_backup_command_call(backup_type)
self.read_profile_data(backup_type)
schedule = []
schedule.append(str(self.minute))
schedule.append('*' if self.frequency == "0" else str(self.hour))
schedule.append('*' if self.frequency in ["0", "1", "2"] else str(self.month_day))
schedule.append('*')
schedule.append('*' if self.frequency != "2" else self.week_days)
schedule.append("BACKUP_NAME=\$(date +\%Y-\%m-\%d_\%H-\%M-\%S); " + command)
schedule.append('> \\"%s\\" 2>&1' % log_path)
# A temporary file to store the crontab
cron_file = "'%s/wb_cron_file'" % self.backup_dir
cron_entry = " ".join(schedule)
schedule_command = 'crontab -l > %s; echo "%s" >> %s; crontab %s; rm %s' % (cron_file, cron_entry, cron_file, cron_file, cron_file)
return schedule_command
def execute(self):
output = StringIO.StringIO()
ret_val = 0
if self.read_params():
if self.change != "DELETE":
self.init_profile()
# Unscheduling would NOT occur on NEW profiles
command = ""
if self.change != "NEW":
label = self.new_label if hasattr(self, 'new_label') else self.old_label
message = ", renamed from %s" % self.old_label if self.old_label != label else "."
if self.old_fb_schedule == "1" or self.old_label != label:
command = self.get_unschedule_command("full")
logging.debug("Unscheduling full backup for profile: %s%s" % (label, message))
ret_val = ret_val + call_system(command, False, output.write)
if self.old_ib_schedule == "1" or self.old_label != label:
command = self.get_unschedule_command("inc")
logging.debug("Unscheduling incremental backup for profile: %s%s" % (label, message))
ret_val = ret_val + call_system(command, False, output.write)
if self.new_fb_schedule == "True":
self.read_profile_data("full")
command = self.get_schedule_command("full")
logging.debug("Scheduling full backup for profile: %s." % self.new_label)
ret_val = ret_val + call_system(command, False, output.write)
if self.new_ib_schedule == "True":
self.read_profile_data("inc")
command = self.get_schedule_command("inc")
logging.debug("Scheduling incremental backup for profile: %s." % self.new_label)
ret_val = ret_val + call_system(command, False, output.write)
else:
self.print_usage()
# In case of error prints the output
if ret_val:
self.write_output(output.getvalue().strip())
return ret_val
class MEBPropagateSettings(MEBCommand):
def __init__(self, params = None, output_handler = None):
super(MEBPropagateSettings, self).__init__(params, output_handler)
self.datafile = ""
self.datadir = ""
this_file_path = os.path.realpath(__file__)
self.backups_home = os.path.dirname(this_file_path)
def read_params(self):
ret_val = False
if self.param_count() == 1:
self.datafile = self.get_param(0)
ret_val = True
return ret_val
def print_usage(self):
self.write_output("PROPAGATE_DATA <datadir>")
self.write_output("WHERE : <datadir> : is the path to the datadir of the server instance for which the profiles are")
self.write_output(" being loaded. (There could be more than one instance on the same box).")
def execute(self):
ret_val = 1
if self.read_params():
ret_val = 0
# Loads the information to be propagated
updater = ConfigUpdater()
source_config_file = os.path.join(self.backups_home, self.datafile)
updater.load_config(source_config_file)
self.datadir = updater.config.read_value('target', 'datadir', None, None)
if self.datadir is not None:
# Creates the string to be used on the file search
search_string = os.path.join(self.backups_home, '*.cnf')
# The glob module will be used to list only the required files
import glob
for filename in glob.glob(search_string):
# Creates a config reader for each profile
profile = ConfigReader(filename)
# Verifies the datadir to ensure it belongs to the requested instance
profile_datadir = profile.read_value('mysqlbackup', 'datadir', False, "")
if profile_datadir == self.datadir:
updater.update(filename)
else:
self.write_output('Data propagation file is missing the target datadir.')
# Deletes the temporary file
os.remove(source_config_file)
else:
self.print_usage()
return ret_val
class MEBGetProfiles(MEBCommand):
def __init__(self, params = None, output_handler = None):
super(MEBGetProfiles, self).__init__(params, output_handler)
self.datadir = ''
self.meb_version=0
def read_params(self):
ret_val = False
if self.param_count() == 2:
self.meb_command = self.get_param(0)
self.datadir = self.get_param(1)
ret_val = True
return ret_val
def print_usage(self):
self.write_output("GET_PROFILES <meb_command> <datadir>")
self.write_output("WHERE : <meb_command> : is the path to a valid MEB executable.")
self.write_output("WHERE : : is the path to the datadir of the server instance for which the profiles are")
self.write_output(" being loaded. (There could be more than one instance on the same box).")
if is_library:
self.write_output("\nWhen executed from python code the WBMEB_BACKUPSHOME environment variable must be set.")
def execute(self):
# Gets the backups home path
backups_home = ''
if os.environ.has_key('WBMEB_BACKUPSHOME'):
backups_home = os.environ['WBMEB_BACKUPSHOME']
if backups_home and self.read_params():
master_data = {}
# Creates the string to be used on the file search
search_string = os.path.join(backups_home, '*.cnf')
# The glob module will be used to list only the required files
import glob
for filename in glob.glob(search_string):
profile_issues = 0
# Creates a config reader for each profile
profile = ConfigReader(filename)
# Verifies the profile configured command is the same as the received as parameter
profile_command = profile.read_value('meb_manager', 'command', False, "")
if profile_command != self.meb_command:
profile_issues |= 16
# Verifies the datadir to ensure it belongs to the requested instance
profile_datadir = profile.read_value('mysqlbackup', 'datadir', False, "")
if profile_datadir == self.datadir:
data = {}
data['LABEL'] = profile.read_value('meb_manager', 'label', False, "")
data['PARTIAL'] = profile.read_value('meb_manager', 'partial', False, "")
data['USING_TTS'] = profile.read_value('meb_manager', 'using_tts', False, "0")
data['BACKUP_DIR'] = profile.read_value('mysqlbackup', 'backup_dir', False, "")
# Gets the folder stats to calculate available space
fs = os.statvfs(data['BACKUP_DIR'])
total = fs.f_frsize * fs.f_blocks
free = fs.f_frsize * fs.f_bfree
# Formats to a human readable format
data['AVAILABLE'] = self.get_available_space(total, free)
# Validates the backups folder for write permission
if not os.access(data['BACKUP_DIR'], os.W_OK):
profile_issues |= 1
# Gets the full schedule data
e = profile.read_value('meb_manager', 'full_backups_enabled', False, "")
f = profile.read_value('meb_manager', 'full_backups_frequency', False, "")
md = profile.read_value('meb_manager', 'full_backups_month_day', False, "")
wd = profile.read_value('meb_manager', 'full_backups_week_days', False, "")
h = profile.read_value('meb_manager', 'full_backups_hour', False, "")
m = profile.read_value('meb_manager', 'full_backups_minute', False, "")
data['FSCHEDULE'] = '-'.join([e, f, md, wd, h, m])
# Gets the incremental schedule data
e = profile.read_value('meb_manager', 'inc_backups_enabled', False, "")
f = profile.read_value('meb_manager', 'inc_backups_frequency', False, "")
md = profile.read_value('meb_manager', 'inc_backups_month_day', False, "")
wd = profile.read_value('meb_manager', 'inc_backups_week_days', False, "")
h = profile.read_value('meb_manager', 'inc_backups_hour', False, "")
m = profile.read_value('meb_manager', 'inc_backups_minute', False, "")
data['ISCHEDULE'] = '-'.join([e, f, md, wd, h, m])
# Profiles with version 0 could have a UUID as the value for the "includes" field
# It was used to make all of the InnoDB tables NOT matching the selection criteria on initial
# versions of MEB.
# On MEB 3.9.0 this became invalid as an error would be generated so
# If found in a profile, we need to report the invalid configuration on the UI.
profile_version = int(profile.read_value('meb_manager', 'version', False, "0"))
if profile_version == '0' and check_version_at_least(self.meb_command, 3, 9, 0):
include = profile.read_value('mysqlbackup', 'include', False, "")
if include:
expression='^[\dA-Fa-f]{8}-([\dA-Fa-f]{4}-){3}[\dA-Fa-f]{12}$'
compiled=re.compile(expression)
match=compiled.match(include)
if match:
profile_issues |= 2
command = profile.read_value('meb_manager', 'command', False, "")
if check_version_at_least(command, 3, 10, 0):
include = profile.read_value('mysqlbackup', 'include', False, "")
databases = profile.read_value('mysqlbackup', 'databases', False, "")
if include or databases:
profile_issues |= 8
else:
include = profile.read_value('mysqlbackup', 'include_tables', False, "")
exclude = profile.read_value('mysqlbackup', 'exclude_tables', False, "")
if include or exclude:
profile_issues |= 4
# The VALID item will cintain a numeric valid describing the issues encountered on the profile
# Validation. Each issue should be assigned a value of 2^x so the different issues can be joined
# using bitwise operations
# 1 : Indicates the backup folder is not valid to store the backups.
# 2 : Indicates a partial backup profile using a regular expression on the include parameter.
data['VALID'] = profile_issues
master_data[filename] = data
# If any, prints the profile data
if master_data:
self.write_output(json.dumps(master_data))
return 0
else:
self.print_usage()
return 1
def get_available_space(self, total, available):
suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB']
limit = float(1024)
index = 0
while total > limit:
total /= limit
available /= limit
index +=1
return "%s%s of %s%s available" % ('{0:.2f}'.format(available), suffixes[index], '{0:.2f}'.format(total), suffixes[index])
return 0
class MEBHelperVersion(MEBCommand):
def __init__(self, params = None, output_handler = None):
super(MEBHelperVersion, self).__init__(params, output_handler)
def execute(self):
try:
import hashlib
file = open (__file__, 'r')
data = file.read()
md5 = hashlib.md5(data)
self.write_output(md5.hexdigest())
except Exception, e:
logging.error('MEBHelperVersion error ' % str(e))
return 1
return 0
if __name__ == "__main__":
# The parameters passed to the command will have an offset when called
# form the script, this is the script name and the command name
is_library = False
# Setups some environment variables
this_file_path = os.path.realpath(__file__)
os.environ['WBMEB_BACKUPSHOME'] = os.path.dirname(this_file_path)
# Initializes the logging module
log_file = '%s.log' % __file__[:-3]
logging.basicConfig(filename=log_file, level=logging.DEBUG)
logging.basicConfig(format='[%(asctime)s] %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p')
processor = MEBCommandProcessor(sys.argv)
sys.exit(processor.execute())
|