From 5de325e7dec7a6f7ea6d177c47897f4c4866e376 Mon Sep 17 00:00:00 2001 From: rorp Date: Tue, 4 Jan 2022 07:04:14 -0800 Subject: [PATCH] Exclude seeds from backup (#3950) --- .../bitcoins/server/BitcoinSAppConfig.scala | 33 ++++++++++++------- .../org/bitcoins/server/CoreRoutes.scala | 7 ++-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/app/server/src/main/scala/org/bitcoins/server/BitcoinSAppConfig.scala b/app/server/src/main/scala/org/bitcoins/server/BitcoinSAppConfig.scala index 4ac8814072..8320ad133c 100644 --- a/app/server/src/main/scala/org/bitcoins/server/BitcoinSAppConfig.scala +++ b/app/server/src/main/scala/org/bitcoins/server/BitcoinSAppConfig.scala @@ -18,10 +18,12 @@ import org.bitcoins.rpc.config.BitcoindRpcAppConfig import org.bitcoins.tor.config.TorAppConfig import org.bitcoins.wallet.config.WalletAppConfig +import java.io.IOException import java.nio.file.{Files, Path, Paths} import java.util.concurrent.TimeUnit import scala.concurrent.Future import scala.concurrent.duration.{DurationInt, FiniteDuration} +import scala.util.Try /** A unified config class for all submodules of Bitcoin-S * that accepts configuration. Thanks to implicit definitions @@ -155,7 +157,7 @@ case class BitcoinSAppConfig( /** Zips $HOME/.bitcoin-s */ - def zipDatadir(target: Path): Path = + def zipDatadir(target: Path): Try[Path] = BitcoinSAppConfig.zipDatadir(directory, target) } @@ -259,21 +261,30 @@ object BitcoinSAppConfig extends Logging { conf.bitcoindRpcConf } - def zipDatadir(source: Path, target: Path): Path = { + def zipDatadir(source: Path, target: Path): Try[Path] = Try { + if (Files.exists(target)) { + throw new IOException( + s"Cannot zip datadir. Target file already exists: $target") + } val temp = Files.createTempDirectory(source, "backup") try { // we don't want to store chaindb.sqlite as these databases are huge // skip logs and binaries as these can be large as well - val tempRE = (".*" + temp.getFileName + "$").r + val tempRE = (".*/" + temp.getFileName + "/.*").r - FileUtil.copyDirectory(source = source, - target = temp, - fileNameFilter = Vector(".*.sqlite$".r, - ".*bitcoin-s.log$".r, - ".*/tor/.*".r, - ".*/binaries/.*".r, - ".*.zip$".r, - tempRE)) + FileUtil.copyDirectory( + source = source, + target = temp, + fileNameFilter = Vector(".*.sqlite$".r, + ".*.sqlite-shm$".r, + ".*.sqlite-wal$".r, + ".*bitcoin-s.log$".r, + ".*/seeds/.*".r, + ".*/tor/.*".r, + ".*/binaries/.*".r, + ".*.zip$".r, + tempRE) + ) SQLiteUtil.backupDirectory(source = source, target = temp, diff --git a/app/server/src/main/scala/org/bitcoins/server/CoreRoutes.scala b/app/server/src/main/scala/org/bitcoins/server/CoreRoutes.scala index b8287a63dc..3276d77dcd 100644 --- a/app/server/src/main/scala/org/bitcoins/server/CoreRoutes.scala +++ b/app/server/src/main/scala/org/bitcoins/server/CoreRoutes.scala @@ -19,6 +19,7 @@ import upickle.default._ import scala.collection.mutable import scala.concurrent.Future +import scala.util.{Failure, Success} case class CoreRoutes()(implicit system: ActorSystem, config: BitcoinSAppConfig) extends ServerRoute { @@ -225,8 +226,10 @@ case class CoreRoutes()(implicit system: ActorSystem, config: BitcoinSAppConfig) withValidServerCommand(ZipDataDir.fromJsArr(arr)) { case ZipDataDir(path) => complete { - config.zipDatadir(path) - Server.httpSuccess(ujson.Null) + config.zipDatadir(path) match { + case Success(_) => Server.httpSuccess(ujson.Null) + case Failure(ex) => Server.httpError(ex.getMessage) + } } } }