migrate.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. // change this, if your php files and data is outside of your webservers document root
  3. define('PATH', '..' . DIRECTORY_SEPARATOR);
  4. define('PUBLIC_PATH', __DIR__ . DIRECTORY_SEPARATOR);
  5. require PATH . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
  6. use PrivateBin\Configuration;
  7. use PrivateBin\Model;
  8. $longopts = array(
  9. "delete-after",
  10. "delete-during"
  11. );
  12. $opts_arr = getopt("fhnv", $longopts, $rest);
  13. if ($opts_arr === false) {
  14. dieerr("Erroneous command line options. Please use -h");
  15. }
  16. if (array_key_exists("h", $opts_arr)) {
  17. helpexit();
  18. }
  19. $delete_after = array_key_exists("delete-after", $opts_arr);
  20. $delete_during = array_key_exists("delete-during", $opts_arr);
  21. $force_overwrite = array_key_exists("f", $opts_arr);
  22. $dryrun = array_key_exists("n", $opts_arr);
  23. $verbose = array_key_exists("v", $opts_arr);
  24. if ($rest >= $argc) {
  25. dieerr("Missing source configuration directory");
  26. }
  27. if ($delete_after && $delete_during) {
  28. dieerr("--delete-after and --delete-during are mutually exclusive");
  29. }
  30. $srcconf = getConfig("source", $argv[$rest]);
  31. $rest++;
  32. $dstconf = getConfig("destination", ($rest < $argc ? $argv[$rest] : ""));
  33. if (($srcconf->getSection("model") == $dstconf->getSection("model")) &&
  34. ($srcconf->getSection("model_options") == $dstconf->getSection("model_options"))) {
  35. dieerr("Source and destination storage configurations are identical");
  36. }
  37. $srcmodel = new Model($srcconf);
  38. $srcstore = $srcmodel->getStore();
  39. $dstmodel = new Model($dstconf);
  40. $dststore = $dstmodel->getStore();
  41. $ids = $srcstore->getAllPastes();
  42. foreach ($ids as $id) {
  43. debug("Reading paste id " . $id);
  44. $paste = $srcstore->read($id);
  45. $comments = $srcstore->readComments($id);
  46. savePaste($force_overwrite, $dryrun, $id, $paste, $dststore);
  47. foreach ($comments as $comment) {
  48. saveComment($force_overwrite, $dryrun, $id, $comment, $dststore);
  49. }
  50. if ($delete_during) {
  51. deletePaste($dryrun, $id, $srcstore);
  52. }
  53. }
  54. if ($delete_after) {
  55. foreach ($ids as $id) {
  56. deletePaste($dryrun, $id, $srcstore);
  57. }
  58. }
  59. debug("Done.");
  60. function deletePaste($dryrun, $pasteid, $srcstore)
  61. {
  62. if (!$dryrun) {
  63. debug("Deleting paste id " . $pasteid);
  64. $srcstore->delete($pasteid);
  65. } else {
  66. debug("Would delete paste id " . $pasteid);
  67. }
  68. }
  69. function saveComment ($force_overwrite, $dryrun, $pasteid, $comment, $dststore)
  70. {
  71. $parentid = $comment["parentid"];
  72. $commentid = $comment["id"];
  73. if (!$dststore->existsComment($pasteid, $parentid, $commentid)) {
  74. if (!$dryrun) {
  75. debug("Saving paste id " . $pasteid . ", parent id " .
  76. $parentid . ", comment id " . $commentid);
  77. $dststore->createComment($pasteid, $parentid, $commentid, $comment);
  78. } else {
  79. debug("Would save paste id " . $pasteid . ", parent id " .
  80. $parentid . ", comment id " . $commentid);
  81. }
  82. } else if ($force_overwrite) {
  83. if (!$dryrun) {
  84. debug("Overwriting paste id " . $pasteid . ", parent id " .
  85. $parentid . ", comment id " . $commentid);
  86. $dststore->createComment($pasteid, $parentid, $commentid, $comment);
  87. } else {
  88. debug("Would overwrite paste id " . $pasteid . ", parent id " .
  89. $parentid . ", comment id " . $commentid);
  90. }
  91. } else {
  92. if (!$dryrun) {
  93. dieerr("Not overwriting paste id " . $pasteid . ", parent id " .
  94. $parentid . ", comment id " . $commentid);
  95. } else {
  96. dieerr("Would not overwrite paste id " . $pasteid . ", parent id " .
  97. $parentid . ", comment id " . $commentid);
  98. }
  99. }
  100. }
  101. function savePaste ($force_overwrite, $dryrun, $pasteid, $paste, $dststore)
  102. {
  103. if (!$dststore->exists($pasteid)) {
  104. if (!$dryrun) {
  105. debug("Saving paste id " . $pasteid);
  106. $dststore->create($pasteid, $paste);
  107. } else {
  108. debug("Would save paste id " . $pasteid);
  109. }
  110. } else if ($force_overwrite) {
  111. if (!$dryrun) {
  112. debug("Overwriting paste id " . $pasteid);
  113. $dststore->create($pasteid, $paste);
  114. } else {
  115. debug("Would overwrite paste id " . $pasteid);
  116. }
  117. } else {
  118. if (!$dryrun) {
  119. dieerr("Not overwriting paste id " . $pasteid);
  120. } else {
  121. dieerr("Would not overwrite paste id " . $pasteid);
  122. }
  123. }
  124. }
  125. function getConfig ($target, $confdir)
  126. {
  127. debug("Trying to load " . $target . " conf.php" .
  128. ($confdir === "" ? "" : " from " . $confdir));
  129. putenv("CONFIG_PATH=" . $confdir);
  130. $conf = new Configuration;
  131. putenv("CONFIG_PATH=");
  132. return $conf;
  133. }
  134. function dieerr ($text)
  135. {
  136. fprintf(STDERR, "ERROR: %s" . PHP_EOL, $text);
  137. die(1);
  138. }
  139. function debug ($text) {
  140. if ($GLOBALS["verbose"]) {
  141. printf("DEBUG: %s" . PHP_EOL, $text);
  142. }
  143. }
  144. function helpexit ()
  145. {
  146. print("migrate.php - Copy data between PrivateBin backends
  147. Usage:
  148. php migrate.php [--delete-after] [--delete-during] [-f] [-n] [-v] srcconfdir
  149. [<dstconfdir>]
  150. php migrate.php [-h]
  151. Options:
  152. --delete-after delete data from source after all pastes and comments have
  153. successfully been copied to the destination
  154. --delete-during delete data from source after the current paste and its
  155. comments have successfully been copied to the destination
  156. -f forcefully overwrite data which already exists at the
  157. destination
  158. -n dry run, do not copy data
  159. -v be verbose
  160. <srcconfdir> use storage backend configration from conf.php found in
  161. this directory as source
  162. <dstconfdir> optionally, use storage backend configration from conf.php
  163. found in this directory as destination; defaults to:
  164. " . PATH . "cfg" . DIRECTORY_SEPARATOR . "conf.php
  165. ");
  166. exit();
  167. }