forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscourse
executable file
·284 lines (228 loc) · 8.17 KB
/
discourse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env ruby
# frozen_string_literal: true
require "thor"
class DiscourseCLI < Thor
desc "remap [--global,--regex] FROM TO", "Remap a string sequence accross all tables"
long_desc <<-LONGDESC
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
LONGDESC
option :global, type: :boolean
option :regex, type: :boolean
def remap(from, to)
load_rails
require 'db_helper'
if options[:regex]
puts "Rewriting all occurences of #{from} to #{to} using regexp_replace"
else
puts "Rewriting all occurences of #{from} to #{to}"
end
puts "THIS TASK WILL REWRITE DATA, ARE YOU SURE (type YES)"
puts "WILL RUN ON ALL #{RailsMultisite::ConnectionManagement.all_dbs.length} DBS" if options[:global]
text = STDIN.gets
if text.strip != "YES"
puts "aborting."
exit
end
if options[:global]
RailsMultisite::ConnectionManagement.each_connection do |db|
puts "", "Remapping tables on #{db}...", ""
do_remap(from, to, options[:regex])
end
else
do_remap(from, to, options[:regex])
end
end
desc "backup", "Backup a discourse forum"
def backup(filename = nil)
load_rails
require "backup_restore"
require "backup_restore/backuper"
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
puts "Starting backup..."
backuper = BackupRestore::Backuper.new(Discourse.system_user.id, filename: filename_without_extension)
backup_filename = backuper.run
exit(1) unless backuper.success
puts "Backup done."
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)
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
end
desc "export", "Backup a Discourse forum"
def export
backup
end
desc "restore", "Restore a Discourse backup"
option :disable_emails, type: :boolean, default: true
def restore(filename = nil)
if File.exist?('/usr/local/bin/discourse')
discourse = 'discourse'
else
discourse = './script/discourse'
end
load_rails
require "backup_restore"
require "backup_restore/restorer"
require "backup_restore/backup_store"
if !filename
puts "You must provide a filename to restore. Did you mean one of the following?\n\n"
store = BackupRestore::BackupStore.create
store.files.each do |file|
puts "#{discourse} restore #{file.filename}"
end
return
end
begin
puts "Starting restore: #{filename}"
restorer = BackupRestore::Restorer.new(
user_id: Discourse.system_user.id,
filename: filename,
disable_emails: options[:disable_emails],
factory: BackupRestore::Factory.new(user_id: Discourse.system_user.id)
)
restorer.run
puts 'Restore done.'
rescue BackupRestore::FilenameMissingError
puts '', 'The filename argument was missing.', ''
usage
rescue BackupRestore::RestoreDisabledError
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", ''
puts 'Restore cancelled.', ''
end
exit(1) unless restorer.try(:success)
end
desc "import", "Restore a Discourse backup"
def import(filename)
restore(filename)
end
desc "rollback", "Rollback to the previous working state"
def rollback
load_rails
require "backup_restore"
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"
SiteSetting.allow_restore = false
puts 'Restore are now forbidden. Enable them with `enable_restore`'
end
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
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
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...", ""
load_rails
load_import_export
ImportExport.export_categories(category_ids)
puts "", "Done", ""
end
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
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
ImportExport.import(filename)
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
ImportExport.import(filename)
puts "", "Done", ""
end
private
def load_rails
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
end
def load_import_export
require File.expand_path(File.dirname(__FILE__) + "/../lib/import_export")
end
def do_remap(from, to, regex = false)
begin
regex ? DbHelper.regexp_replace(from, to, verbose: true) : DbHelper.remap(from, to, verbose: true)
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)
end
end
end
DiscourseCLI.start(ARGV)