2013-11-28 02:17:14 +08:00
|
|
|
#!/usr/bin/env ruby
|
2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
2013-11-28 02:17:14 +08:00
|
|
|
|
|
|
|
require "thor"
|
|
|
|
|
|
|
|
class DiscourseCLI < Thor
|
2021-10-22 03:01:05 +08:00
|
|
|
def self.exit_on_failure?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2021-05-21 09:43:47 +08:00
|
|
|
desc "remap [--global,--regex] FROM TO", "Remap a string sequence across all tables"
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
long_desc <<-TEXT
|
2017-01-31 06:27:45 +08:00
|
|
|
Replace a string sequence FROM with TO across all tables.
|
|
|
|
|
|
|
|
With --global option, the remapping is run on ***ALL***
|
|
|
|
databases. Instead of just running on the current database, run on
|
|
|
|
every database on this machine. This option is useful for
|
|
|
|
multi-site setups.
|
|
|
|
|
|
|
|
With --regex option, use PostgreSQL function regexp_replace to do
|
|
|
|
the remapping. Enabling this interprets FROM as a PostgreSQL
|
|
|
|
regular expression. TO can contain references to captures in the
|
|
|
|
FROM match. See the "Regular Expression Details" section and
|
|
|
|
"regexp_replace" documentation in the PostgreSQL manual for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
discourse remap talk.foo.com talk.bar.com # renaming a Discourse domain name
|
|
|
|
|
|
|
|
discourse remap --regex "\[\/?color(=[^\]]*)*]" "" # removing "color" bbcodes
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
TEXT
|
2017-01-20 01:33:49 +08:00
|
|
|
option :global, type: :boolean
|
|
|
|
option :regex, type: :boolean
|
|
|
|
def remap(from, to)
|
2014-01-31 11:53:25 +08:00
|
|
|
load_rails
|
2019-07-30 00:43:40 +08:00
|
|
|
require 'db_helper'
|
2014-01-31 11:53:25 +08:00
|
|
|
|
2017-01-20 01:33:49 +08:00
|
|
|
if options[:regex]
|
2021-06-30 22:38:14 +08:00
|
|
|
puts "Rewriting all occurrences of #{from} to #{to} using regexp_replace"
|
2017-01-20 01:33:49 +08:00
|
|
|
else
|
2021-06-30 22:38:14 +08:00
|
|
|
puts "Rewriting all occurrences of #{from} to #{to}"
|
2017-01-20 01:33:49 +08:00
|
|
|
end
|
2014-01-31 11:53:25 +08:00
|
|
|
puts "THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES)"
|
2017-01-20 01:33:49 +08:00
|
|
|
puts "WILL RUN ON ALL #{RailsMultisite::ConnectionManagement.all_dbs.length} DBS" if options[:global]
|
2014-01-31 13:37:05 +08:00
|
|
|
text = STDIN.gets
|
2022-03-26 04:14:41 +08:00
|
|
|
if text.strip.upcase != "YES"
|
2014-01-31 11:53:25 +08:00
|
|
|
puts "aborting."
|
2021-04-28 21:10:35 +08:00
|
|
|
exit 1
|
2014-01-31 11:53:25 +08:00
|
|
|
end
|
|
|
|
|
2017-01-20 01:33:49 +08:00
|
|
|
if options[:global]
|
2015-07-23 12:39:38 +08:00
|
|
|
RailsMultisite::ConnectionManagement.each_connection do |db|
|
|
|
|
puts "", "Remapping tables on #{db}...", ""
|
2017-01-20 01:33:49 +08:00
|
|
|
do_remap(from, to, options[:regex])
|
2014-09-09 02:21:21 +08:00
|
|
|
end
|
2015-07-23 12:39:38 +08:00
|
|
|
else
|
2017-01-20 01:33:49 +08:00
|
|
|
do_remap(from, to, options[:regex])
|
2014-01-31 11:53:25 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-13 12:38:47 +08:00
|
|
|
desc "backup", "Backup a discourse forum"
|
2014-02-19 22:25:31 +08:00
|
|
|
def backup(filename = nil)
|
2014-01-17 09:27:47 +08:00
|
|
|
load_rails
|
2019-10-02 12:01:53 +08:00
|
|
|
require "backup_restore"
|
2014-10-11 02:04:07 +08:00
|
|
|
require "backup_restore/backuper"
|
2014-02-13 12:38:47 +08:00
|
|
|
|
2018-10-18 00:32:07 +08:00
|
|
|
store = BackupRestore::BackupStore.create
|
|
|
|
|
|
|
|
if filename
|
|
|
|
destination_directory = File.dirname(filename).sub(/^\.$/, '')
|
|
|
|
|
|
|
|
if destination_directory.present? && store.remote?
|
|
|
|
puts "Only local backup storage supports paths."
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
filename_without_extension = File.basename(filename).sub(/\.(sql\.)?(tar\.gz|t?gz)$/i, '')
|
|
|
|
end
|
|
|
|
|
2014-10-11 02:04:07 +08:00
|
|
|
puts "Starting backup..."
|
2018-10-18 00:32:07 +08:00
|
|
|
backuper = BackupRestore::Backuper.new(Discourse.system_user.id, filename: filename_without_extension)
|
2018-10-15 09:43:31 +08:00
|
|
|
backup_filename = backuper.run
|
2018-10-18 00:32:07 +08:00
|
|
|
exit(1) unless backuper.success
|
2018-10-15 09:43:31 +08:00
|
|
|
|
2018-10-18 00:32:07 +08:00
|
|
|
puts "Backup done."
|
2018-10-15 09:43:31 +08:00
|
|
|
|
|
|
|
if store.remote?
|
|
|
|
location = BackupLocationSiteSetting.values.find { |v| v[:value] == SiteSetting.backup_location }
|
|
|
|
location = I18n.t("admin_js.#{location[:name]}") if location
|
|
|
|
puts "Output file is stored on #{location} as #{backup_filename}", ""
|
|
|
|
else
|
|
|
|
backup = store.file(backup_filename, include_download_source: true)
|
2014-08-04 23:55:09 +08:00
|
|
|
|
2018-10-18 00:32:07 +08:00
|
|
|
if destination_directory.present?
|
|
|
|
puts "Moving backup file..."
|
|
|
|
backup_path = File.join(destination_directory, backup_filename)
|
|
|
|
FileUtils.mv(backup.source, backup_path)
|
|
|
|
else
|
|
|
|
backup_path = backup.source
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Output file is in: #{backup_path}", ""
|
|
|
|
end
|
2014-01-17 09:27:47 +08:00
|
|
|
end
|
|
|
|
|
2014-02-13 12:38:47 +08:00
|
|
|
desc "export", "Backup a Discourse forum"
|
|
|
|
def export
|
|
|
|
backup
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "restore", "Restore a Discourse backup"
|
2019-03-08 04:48:26 +08:00
|
|
|
option :disable_emails, type: :boolean, default: true
|
2021-02-09 23:02:44 +08:00
|
|
|
option :location, type: :string, enum: ["local", "s3"], desc: "Override the backup location"
|
2016-07-13 00:30:39 +08:00
|
|
|
def restore(filename = nil)
|
|
|
|
|
2017-10-04 05:09:32 +08:00
|
|
|
if File.exist?('/usr/local/bin/discourse')
|
|
|
|
discourse = 'discourse'
|
|
|
|
else
|
|
|
|
discourse = './script/discourse'
|
|
|
|
end
|
|
|
|
|
2018-10-15 09:43:31 +08:00
|
|
|
load_rails
|
2019-10-02 12:01:53 +08:00
|
|
|
require "backup_restore"
|
2018-10-15 09:43:31 +08:00
|
|
|
require "backup_restore/restorer"
|
|
|
|
require "backup_restore/backup_store"
|
|
|
|
|
2016-07-13 00:30:39 +08:00
|
|
|
if !filename
|
|
|
|
puts "You must provide a filename to restore. Did you mean one of the following?\n\n"
|
|
|
|
|
2021-02-09 23:02:44 +08:00
|
|
|
store = BackupRestore::BackupStore.create(location: options[:location])
|
2018-10-15 09:43:31 +08:00
|
|
|
store.files.each do |file|
|
|
|
|
puts "#{discourse} restore #{file.filename}"
|
2016-07-13 00:30:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-01-17 09:27:47 +08:00
|
|
|
begin
|
2014-02-19 22:25:31 +08:00
|
|
|
puts "Starting restore: #{filename}"
|
2019-03-08 04:48:26 +08:00
|
|
|
restorer = BackupRestore::Restorer.new(
|
2020-01-13 07:12:27 +08:00
|
|
|
user_id: Discourse.system_user.id,
|
2019-03-08 04:48:26 +08:00
|
|
|
filename: filename,
|
2020-01-13 07:12:27 +08:00
|
|
|
disable_emails: options[:disable_emails],
|
2021-02-09 23:02:44 +08:00
|
|
|
location: options[:location],
|
2020-01-13 07:12:27 +08:00
|
|
|
factory: BackupRestore::Factory.new(user_id: Discourse.system_user.id)
|
2019-03-08 04:48:26 +08:00
|
|
|
)
|
2014-10-11 02:04:07 +08:00
|
|
|
restorer.run
|
2014-02-19 22:25:31 +08:00
|
|
|
puts 'Restore done.'
|
2014-10-11 02:04:07 +08:00
|
|
|
rescue BackupRestore::FilenameMissingError
|
2014-01-17 09:27:47 +08:00
|
|
|
puts '', 'The filename argument was missing.', ''
|
|
|
|
usage
|
2014-10-11 02:04:07 +08:00
|
|
|
rescue BackupRestore::RestoreDisabledError
|
2017-10-04 05:09:32 +08:00
|
|
|
puts '', 'Restores are not allowed.', 'An admin needs to set allow_restore to true in the site settings before restores can be run.'
|
|
|
|
puts "Enable now with", '', "#{discourse} enable_restore", ''
|
2014-02-19 22:25:31 +08:00
|
|
|
puts 'Restore cancelled.', ''
|
2014-01-17 09:27:47 +08:00
|
|
|
end
|
2014-08-04 23:55:09 +08:00
|
|
|
|
2014-10-11 02:04:07 +08:00
|
|
|
exit(1) unless restorer.try(:success)
|
2014-01-17 09:27:47 +08:00
|
|
|
end
|
|
|
|
|
2014-02-13 12:38:47 +08:00
|
|
|
desc "import", "Restore a Discourse backup"
|
|
|
|
def import(filename)
|
|
|
|
restore(filename)
|
|
|
|
end
|
|
|
|
|
2014-02-21 23:17:00 +08:00
|
|
|
desc "rollback", "Rollback to the previous working state"
|
|
|
|
def rollback
|
|
|
|
load_rails
|
|
|
|
require "backup_restore"
|
2013-11-28 02:17:14 +08:00
|
|
|
|
2014-02-21 23:17:00 +08:00
|
|
|
puts 'Rolling back if needed..'
|
|
|
|
BackupRestore.rollback!
|
|
|
|
puts 'Done.'
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "enable_restore", "Allow restore operations"
|
|
|
|
def enable_restore
|
|
|
|
load_rails
|
|
|
|
require "site_setting"
|
|
|
|
|
|
|
|
SiteSetting.allow_restore = true
|
|
|
|
puts 'Restore are now permitted. Disable them with `disable_restore`'
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "disable_restore", "Forbid restore operations"
|
|
|
|
def disable_restore
|
|
|
|
load_rails
|
|
|
|
require "site_setting"
|
2013-11-28 02:17:14 +08:00
|
|
|
|
2014-02-21 23:17:00 +08:00
|
|
|
SiteSetting.allow_restore = false
|
|
|
|
puts 'Restore are now forbidden. Enable them with `enable_restore`'
|
2013-11-28 02:17:14 +08:00
|
|
|
end
|
|
|
|
|
2014-02-22 00:10:53 +08:00
|
|
|
desc "enable_readonly", "Enable the readonly mode"
|
|
|
|
def enable_readonly
|
|
|
|
load_rails
|
|
|
|
|
|
|
|
Discourse.enable_readonly_mode
|
|
|
|
puts 'The site is now in readonly mode.'
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "disable_readonly", "Disable the readonly mode"
|
|
|
|
def disable_readonly
|
|
|
|
load_rails
|
|
|
|
|
|
|
|
Discourse.disable_readonly_mode
|
|
|
|
puts 'The site is now fully operable.'
|
|
|
|
end
|
|
|
|
|
2014-03-07 12:26:34 +08:00
|
|
|
desc "request_refresh", "Ask all clients to refresh the browser"
|
|
|
|
def request_refresh
|
|
|
|
load_rails
|
|
|
|
|
|
|
|
Discourse.request_refresh!
|
|
|
|
puts 'Requests sent. Clients will refresh on next navigation.'
|
|
|
|
end
|
|
|
|
|
2017-11-11 21:22:22 +08:00
|
|
|
desc "export_categories", "Export categories, all its topics, and all users who posted in those topics"
|
|
|
|
def export_categories(*category_ids)
|
|
|
|
puts "Starting export of categories...", ""
|
2016-01-26 02:37:43 +08:00
|
|
|
load_rails
|
|
|
|
load_import_export
|
2017-11-11 21:22:22 +08:00
|
|
|
ImportExport.export_categories(category_ids)
|
2016-01-26 02:37:43 +08:00
|
|
|
puts "", "Done", ""
|
|
|
|
end
|
|
|
|
|
2017-11-11 21:22:22 +08:00
|
|
|
desc "export_category", "Export a category, all its topics, and all users who posted in those topics"
|
|
|
|
def export_category(category_id)
|
|
|
|
raise "Category id argument is missing!" unless category_id
|
|
|
|
|
|
|
|
export_categories([category_id])
|
|
|
|
end
|
|
|
|
|
2016-01-26 02:37:43 +08:00
|
|
|
desc "import_category", "Import a category, its topics and the users from the output of the export_category command"
|
|
|
|
def import_category(filename)
|
|
|
|
raise "File name argument missing!" unless filename
|
|
|
|
|
|
|
|
puts "Starting import from #{filename}..."
|
|
|
|
load_rails
|
|
|
|
load_import_export
|
2017-10-27 04:29:36 +08:00
|
|
|
ImportExport.import(filename)
|
2016-01-26 02:37:43 +08:00
|
|
|
puts "", "Done", ""
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "export_topics", "Export topics and all users who posted in that topic. Accepts multiple topic id's"
|
|
|
|
def export_topics(*topic_ids)
|
|
|
|
puts "Starting export of topics...", ""
|
|
|
|
load_rails
|
|
|
|
load_import_export
|
|
|
|
ImportExport.export_topics(topic_ids)
|
|
|
|
puts "", "Done", ""
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "import_topics", "Import topics and their users from the output of the export_topic command"
|
|
|
|
def import_topics(filename)
|
|
|
|
raise "File name argument missing!" unless filename
|
|
|
|
|
|
|
|
puts "Starting import from #{filename}..."
|
|
|
|
load_rails
|
|
|
|
load_import_export
|
2017-10-27 04:29:36 +08:00
|
|
|
ImportExport.import(filename)
|
2016-01-26 02:37:43 +08:00
|
|
|
puts "", "Done", ""
|
|
|
|
end
|
|
|
|
|
2013-11-28 02:17:14 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def load_rails
|
|
|
|
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
|
|
|
|
end
|
|
|
|
|
2016-01-26 02:37:43 +08:00
|
|
|
def load_import_export
|
2019-10-23 14:27:14 +08:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + "/../lib/import_export")
|
2016-01-26 02:37:43 +08:00
|
|
|
end
|
|
|
|
|
2017-01-20 01:33:49 +08:00
|
|
|
def do_remap(from, to, regex = false)
|
2018-08-15 05:29:43 +08:00
|
|
|
begin
|
2019-07-31 23:30:08 +08:00
|
|
|
regex ? DbHelper.regexp_replace(from, to, verbose: true) : DbHelper.remap(from, to, verbose: true)
|
2018-08-15 05:29:43 +08:00
|
|
|
puts 'Done', ''
|
|
|
|
rescue => ex
|
|
|
|
puts "Error: #{ex}"
|
|
|
|
puts "The remap has only been partially applied due to the error above. Please re-run the script again."
|
|
|
|
exit(1)
|
2015-07-23 12:39:38 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-28 02:17:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
DiscourseCLI.start(ARGV)
|