migrate 6.5 KB

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