Skip to content

Instantly share code, notes, and snippets.

@ryanschwartz
Created November 6, 2015 15:44
Show Gist options
  • Save ryanschwartz/b9afae6158ed2cebca25 to your computer and use it in GitHub Desktop.
Save ryanschwartz/b9afae6158ed2cebca25 to your computer and use it in GitHub Desktop.
Diff of things I added to mysql_snapback (-q for silent running, -t for formatting time_snapshot)
--- datap02 2015-11-06 15:28:07.800529694 +0000
+++ mysql_snapback.py 2014-02-07 18:42:31.103289360 +0000
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/opt/local/bin/python
####################################################################
# FILENAME: mysql_snapback.py
# PROJECT: Miscellaneous Tools
@@ -51,10 +51,14 @@
### Find the configuration file
-_args_parser = OptionParser(usage="%prog --filename snapback.cfg", version=_txt_program_name + " " + _txt_program_revision)
+_args_parser = OptionParser(usage="%prog --filename snapback.cfg --timestamp %Y%m%d%H%M%S", version=_txt_program_name + " " + _txt_program_revision)
_args_parser.add_option("-f", "--filename", dest="FN_CONFIG",help="Configuration file containing MySQL and ZFS settings to use for backup run.")
+_args_parser.add_option("-t", "--timestamp",dest="TS_CONFIG",help="Argument describing the timestamp used for snapshots.")
+_args_parser.add_option("-q", "--quiet",dest="quiet",action="store_true",help="Quiet output.")
_arg_program_options, _arg_leftover = _args_parser.parse_args()
+_time_format = _arg_program_options.TS_CONFIG
+_quiet = _arg_program_options.quiet
if _arg_program_options.FN_CONFIG==None:
print "\nMust supply a configuration filename. Please use --help for syntax."
@@ -104,66 +108,94 @@
if not re.compile(_fn_zfs_log_fs).search(_zfs_list_output, 1):
print "No ZFS Log filesystem named " + _fn_zfs_log_fs + " exists. Please check your configuration and try again."
sys.exit(-4)
-print "\nAll specified ZFS filesystems are present."
+if not _quiet:
+ print "\nAll specified ZFS filesystems are present."
### Connect to MySQL
-print "\n\nSnapBack is connecting to MySQL:"
+if not _quiet:
+ print "\n\nSnapBack is connecting to MySQL:"
try:
_db_backup = MySQLdb.connect(host=_mysql_server, user=_mysql_user, passwd=_mysql_pass)
except MySQLdb.OperationalError:
print "Unable to connect to MySQL server " + _mysql_server + ". Please check the configuration file and try again."
sys.exit(-5)
-print "\tConnected to " + _mysql_server + "."
+if not _quiet:
+ print "\tConnected to " + _mysql_server + "."
_cu_backup = _db_backup.cursor()
-_time_snapshot = time.strftime("%Y%m%d%H%M%S")
+if _time_format is not None:
+ _time_snapshot = time.strftime(_time_format)
+else:
+ _time_snapshot = time.strftime("%Y%m%d%H%M%S")
# Make operation InnoDB safe/consistent.
_cu_backup.execute("SET AUTOCOMMIT=0;")
-print "\tPrepared MySQL to ensure InnoDB consistency."
+if not _quiet:
+ print "\tPrepared MySQL to ensure InnoDB consistency."
### Pull ZFS snapshots.
-print "\n\nSnapBack is commencing snapshot run:"
+if not _quiet:
+ print "\n\nSnapBack is commencing snapshot run:"
+if not _quiet:
+ print "\tStopping slave (see http://bugs.mysql.com/bug.php?id=68460)."
+_cu_backup.execute("STOP SLAVE;")
+if not _quiet:
+ print "\tSlave stopped."
print "\tLocking tables (if this is taking awhile please check the MySQL process list)."
_cu_backup.execute("FLUSH TABLES WITH READ LOCK;")
-print "\tTables locked."
-_cu_backup.execute("SHOW MASTER STATUS;")
+if not _quiet:
+ print "\tTables locked."
+_cu_backup.execute("SHOW SLAVE STATUS;")
_rows_master_status = _cu_backup.fetchone()
# Notate current master log file and position if master_naming is on
if _bool_master_naming == True:
- _txt_master_status = "_" + str(_rows_master_status[0]) + "_" + str(_rows_master_status[1])
+ _txt_master_status = "_" + str(_rows_master_status[5]) + "_" + str(_rows_master_status[6])
else:
_txt_master_status = ""
-print "\tMaster status retrieved."
+if not _quiet:
+ print "\tMaster status retrieved."
try:
# Snapshot DB
- print "\tSnapping ZFS DB filesystem."
+ if not _quiet:
+ print "\tSnapping ZFS DB filesystem."
_zfs_dbsnap_status,_zfs_dbsnap_output = commands.getstatusoutput(_fn_zfs_cmd + " snapshot " + _fn_zfs_db_fs + "@" + _time_snapshot + _txt_master_status)
if str(_zfs_dbsnap_status) != "0":
- print "An error occurred while executing the ZFS snapshot on " + _fn_zfs_db_fs + ". Unlocking tables and quitting."
+ print "An error occurred while executing the ZFS snapshot on " + _fn_zfs_db_fs + ". Unlocking tables, re-starting slave, and quitting."
_cu_backup.execute("UNLOCK TABLES;")
+ _cu_backup.execute("START SLAVE;")
sys.exit(-7)
# Snapshot Logs (if specified)
if _fn_zfs_db_fs != _fn_zfs_log_fs:
- print "\tSnapping ZFS Log filesystem."
+ if not _quiet:
+ print "\tSnapping ZFS Log filesystem."
_zfs_logsnap_status,_zfs_logsnap_output = commands.getstatusoutput(_fn_zfs_cmd + " snapshot " + _fn_zfs_log_fs + "@" + _time_snapshot + _txt_master_status)
if str(_zfs_logsnap_status) != "0":
- print "An error occurred while executing the ZFS snapshot on " + _fn_zfs_log_fs + ". Unlocking tables and quitting."
+ print "An error occurred while executing the ZFS snapshot on " + _fn_zfs_log_fs + ". Unlocking tables, re-starting slave, and quitting."
_cu_backup.execute("UNLOCK TABLES;")
+ _cu_backup.execute("START SLAVE;")
sys.exit(-7)
except:
_cu_backup.execute("UNLOCK TABLES;")
- print "\tAn unrecoverable error occurred while trying to pull the snapshots. We have unlocked the tables. Please check your system logs for details as to the ZFS error."
+ _cu_backup.execute("START SLAVE;")
+ print "\tAn unrecoverable error occurred while trying to pull the snapshots. We have unlocked the tables and re-started the slave. Please check your system logs for details as to the ZFS error."
sys.exit(-7)
-print "\tUnlocking tables."
+if not _quiet:
+ print "\tUnlocking tables."
_cu_backup.execute("UNLOCK TABLES;")
-print "\tTables unlocked."
-print "SnapBack snapshot run completed."
-
-print "\n\nSnapshots created:"
-print "\t" + _fn_zfs_db_fs + "@" + _time_snapshot + _txt_master_status
+if not _quiet:
+ print "\tTables unlocked."
+if not _quiet:
+ print "\tRe-starting slave."
+_cu_backup.execute("START SLAVE;")
+if not _quiet:
+ print "\tSlave restarted."
+ print "SnapBack snapshot run completed."
+if not _quiet:
+ print "\n\nSnapshots created:"
+ print _fn_zfs_db_fs + "@" + _time_snapshot + _txt_master_status
if _fn_zfs_db_fs != _fn_zfs_log_fs:
- print "\t" + _fn_zfs_log_fs + "@" + _time_snapshot + _txt_master_status
+ if not _quiet:
+ print "\t" + _fn_zfs_log_fs + "@" + _time_snapshot + _txt_master_status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment