revertprovider.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package cmd
  15. import (
  16. "os"
  17. "github.com/rs/zerolog"
  18. "github.com/spf13/cobra"
  19. "github.com/spf13/viper"
  20. "github.com/drakkan/sftpgo/v2/internal/config"
  21. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  22. "github.com/drakkan/sftpgo/v2/internal/logger"
  23. "github.com/drakkan/sftpgo/v2/internal/util"
  24. )
  25. var (
  26. revertProviderTargetVersion int
  27. revertProviderCmd = &cobra.Command{
  28. Use: "revertprovider",
  29. Short: "Revert the configured data provider to a previous version",
  30. Long: `This command reads the data provider connection details from the specified
  31. configuration file and restore the provider schema and/or data to a previous version.
  32. This command is not supported for the memory provider.
  33. Please take a look at the usage below to customize the options.`,
  34. Run: func(_ *cobra.Command, _ []string) {
  35. logger.DisableLogger()
  36. logger.EnableConsoleLogger(zerolog.DebugLevel)
  37. if revertProviderTargetVersion != 23 {
  38. logger.WarnToConsole("Unsupported target version, 23 is the only supported one")
  39. os.Exit(1)
  40. }
  41. configDir = util.CleanDirInput(configDir)
  42. err := config.LoadConfig(configDir, configFile)
  43. if err != nil {
  44. logger.WarnToConsole("Unable to load configuration: %v", err)
  45. os.Exit(1)
  46. }
  47. kmsConfig := config.GetKMSConfig()
  48. err = kmsConfig.Initialize()
  49. if err != nil {
  50. logger.ErrorToConsole("unable to initialize KMS: %v", err)
  51. os.Exit(1)
  52. }
  53. providerConf := config.GetProviderConf()
  54. logger.InfoToConsole("Reverting provider: %q config file: %q target version %d", providerConf.Driver,
  55. viper.ConfigFileUsed(), revertProviderTargetVersion)
  56. err = dataprovider.RevertDatabase(providerConf, configDir, revertProviderTargetVersion)
  57. if err != nil {
  58. logger.WarnToConsole("Error reverting provider: %v", err)
  59. os.Exit(1)
  60. }
  61. logger.InfoToConsole("Data provider successfully reverted")
  62. },
  63. }
  64. )
  65. func init() {
  66. addConfigFlags(revertProviderCmd)
  67. revertProviderCmd.Flags().IntVar(&revertProviderTargetVersion, "to-version", 23, `23 means the version supported in v2.4.x`)
  68. rootCmd.AddCommand(revertProviderCmd)
  69. }