migrate 6.1 KB

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