Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. $missing = <<<MISSING
  5. Config file not found (/etc/inno_backup.conf)
  6.  
  7. Create a new file with the following keys/values:
  8.  
  9. user = backups
  10. pass = password1234
  11. backup_dir = /backups
  12. data_dir = /var/lib/mysql
  13. MISSING;
  14.  
  15. if( ! file_exists('/etc/inno_backup.conf') ) {
  16. exit($missing."\n");
  17. }
  18.  
  19. $conf = parse_ini_file('/etc/inno_backup.conf');
  20.  
  21. $user = $conf['user'];
  22. $pass = $conf['pass'];
  23. $data_dir = $conf['data_dir'];
  24. $backup_dir = $conf['backup_dir'];
  25.  
  26. $usage = <<<USAGE
  27. USAGE: inno_backup [option]
  28.  
  29. Options:
  30. full : Start a full backup. This will delete ALL incrementals and the current full snapshot.
  31. incremental : Start an incremental. If no full backup exists, the script will fall back to a full backup.
  32. prepare : Glues all incrementals to the full snapshot in preparation for a restore.
  33. restore : Shuts down MySQL, removes content of datadir, replaces it with the backup. Dangerous.
  34. reset : THIS WILL NUKE THE BACKUP DIRECTORY AND ALL CONTENT WITHIN! Only use to start clean.
  35. USAGE;
  36.  
  37. $arg = ( isset($argv[1]) ) ? $argv[1] : "";
  38.  
  39. $base_args = "--user={$user} --password={$pass}";
  40.  
  41. switch( strtolower($arg) ) {
  42. case "full":
  43. full();
  44. break;
  45.  
  46. case "incremental":
  47. incremental();
  48. break;
  49.  
  50. case "prepare":
  51. prepare();
  52. break;
  53.  
  54. case "restore":
  55. // This is dangerous, yo. Gotta think of a good way to fool-proof it.
  56. restore();
  57. break;
  58.  
  59. case "reset":
  60. exec("rm -rf {$backup_dir}/logs/* {$backup_dir}/incremental/* {$backup_dir}/full/* {$backup_dir}/last_incremental");
  61. echo "Backup directory cleaned.\n";
  62. break;
  63.  
  64. default:
  65. echo "No option supplied. See usage below:\n\n";
  66. echo $usage."\n";
  67. exit();
  68. }
  69.  
  70. ## Functions!
  71.  
  72. function full() {
  73. global $user, $pass, $backup_dir, $base_args;
  74.  
  75. if( ! file_exists($backup_dir) ) {
  76. echo "Cannot find backup directory : {$backup_dir}\n";
  77. exit();
  78. }
  79.  
  80. if( file_exists($backup_dir."/full") ) {
  81. echo "Found existing full backup directory. Removing it and all incrementals.\n";
  82. exec("rm -rf {$backup_dir}/full {$backup_dir}/incremental/* {$backup_dir}/last_incremental");
  83. }
  84.  
  85. echo "Starting backup -- this takes an average of 3 to 3.5 hours.\n";
  86. exec("innobackupex {$base_args} --no-timestamp {$backup_dir}/full &>{$backup_dir}/logs/full.log");
  87. if( check_log($backup_dir."/logs/full.log") ) {
  88. echo "Backup complete.\n";
  89. exec("rm -f {$backup_dir}/logs/full.log");
  90. } else {
  91. echo "Backup failed. Review {$backup_dir}/logs/full.log\n";
  92. }
  93. }
  94.  
  95. function incremental() {
  96. global $user, $pass, $backup_dir, $base_args;
  97.  
  98. echo "Starting incremental backup.\n";
  99. echo "Checking for existing full backup.\n";
  100. if( ! file_exists($backup_dir."/full/xtrabackup_checkpoints") ) {
  101. echo "There is no full backup. Please execute: inno_backup full\n";
  102. exit();
  103. }
  104. echo "Found existing full backup. Kicking off incremental backup.\n";
  105.  
  106. if( ! file_exists($backup_dir."/last_incremental") ) {
  107. $inc = 1;
  108. file_put_contents($backup_dir."/last_incremental", $inc);
  109. } else {
  110. echo "Found last_incremental file\n";
  111. $inc = (int) file_get_contents($backup_dir."/last_incremental");
  112. echo "Last incremental: #{$inc}\n";
  113. $inc++;
  114. }
  115.  
  116. if( $inc == 1 ) {
  117. exec("innobackupex {$base_args} --no-timestamp --incremental {$backup_dir}/incremental/{$inc} --incremental-basedir={$backup_dir}/full &>{$backup_dir}/logs/incremental.log");
  118. } else {
  119. $inc_prev = $inc-1;
  120. exec("innobackupex {$base_args} --no-timestamp --incremental {$backup_dir}/incremental/{$inc} --incremental-basedir={$backup_dir}/incremental/{$inc_prev} &>{$backup_dir}/logs/incremental.log");
  121. }
  122.  
  123. if( check_log($backup_dir."/logs/incremental.log") ) {
  124. echo "Incremental complete.\n";
  125. exec("rm -f {$backup_dir}/logs/incremental.log");
  126. } else {
  127. echo "Backup failed. Review {$backup_dir}/logs/incremental.log\n";
  128. exit();
  129. }
  130.  
  131. file_put_contents($backup_dir."/last_incremental", $inc++);
  132. }
  133.  
  134. function prepare() {
  135. global $user, $pass, $backup_dir, $base_args;
  136.  
  137. if( ! file_exists($backup_dir."/last_incremental") ) {
  138. exit("Missing {$backup_dir}/last_incremental.\n");
  139. }
  140.  
  141. echo "Rebuilding full backup from incrementals for restoration. This may take a while ...\n";
  142. $inc = (int) file_get_contents($backup_dir."/last_incremental");
  143.  
  144. exec("innobackupex {$base_args} --no-timestamp --use-memory=500M --apply-log {$backup_dir}/full &>{$backup_dir}/logs/prepare.log");
  145.  
  146. if( ! check_log($backup_dir."/logs/prepare.log") ) {
  147. echo "Failed to prepare backup for restoration. Check {$backup_dir}/logs/prepare.log\n";
  148. exit();
  149. }
  150.  
  151. for($i = 1; $i <= $inc; $i++) {
  152. echo "Processing incremental: {$i}\n";
  153. exec("innobackupex {$base_args} --apply-log --redo-only --incremental /backups/full/ --incremental-basedir=/backups/incremental/{$i} &>{$backup_dir}/logs/prepare.log");
  154. if( ! check_log($backup_dir."/logs/prepare.log") ) {
  155. echo "Failed to apply incremental #{$i} to full backup. Check {$backup_dir}/logs/prepare.log\n";
  156. exit();
  157. }
  158. }
  159.  
  160. echo "Final preparation running ...\n";
  161.  
  162. exec("innobackupex {$base_args} --apply-log {$backup_dir}/full --use-memory=500M &>{$backup_dir}/logs/prepare.log");
  163. if( check_log($backup_dir."/logs/prepare.log") ) {
  164. echo "Preparation has completed successfully. You may now attempt: inno_backup restore\n";
  165. } else {
  166. exit("Final preparation has failed. Review {$backup_dir}/logs/prepare.log\n");
  167. exec("rm -rf {$backup_dir}/last_incremental {$backup_dir}/incremental/* {$backup_dir}/logs/prepare.log");
  168.  
  169. }
  170. }
  171.  
  172. function restore() {
  173. global $user, $pass, $backup_dir, $base_args;
  174.  
  175. echo "This is a highly destructive process. Do not run this unless absolutely sure.\n";
  176. echo "Are you sure you want to run the restore process? (y/N): ";
  177.  
  178. $handle = fopen("php://stdin", "r");
  179. $line = fgets($handle);
  180. if( strtolower(trim($line)) != 'y' ) {
  181. exit("Aborting. Good choice, mortal.\n");
  182. }
  183.  
  184. echo "Just kidding. This isn't implemented yet, meat-bag.\n";
  185.  
  186. }
  187.  
  188. function check_log($file) {
  189. $res = exec("tail -n 1 {$file}");
  190. if( strstr($res, "OK!" ) ) {
  191. return true;
  192. }
  193.  
  194. return false;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement