Class: Optimacms::BackupMetadata::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/optimacms/backup_metadata/backup.rb

Class Method Summary collapse

Class Method Details

.delete_backup(f) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/optimacms/backup_metadata/backup.rb', line 116

def self.delete_backup(f)
  path = File.join(dir_backups, f)

  File.delete(path)

  # delete folder
  require 'fileutils'
  d = File.join(dir_backups, File.basename(f, ".tar.gz"))
  FileUtils.rm_r d if Dir.exists?(d)

  return {res:1 , output: "deleted"}
rescue Exception => e
  return {res: 0, output: "error"}
end

.dir_backupsObject



240
241
242
243
# File 'lib/optimacms/backup_metadata/backup.rb', line 240

def self.dir_backups
  Optimacms.config.['backup_dir_base']

end

.generate_backup_nameObject



4
5
6
7
8
# File 'lib/optimacms/backup_metadata/backup.rb', line 4

def self.generate_backup_name
  d = Time.now.utc

  "#{d.strftime("%Y.%m.%d.%H.%M.%S")}"
end

.list_backupsObject



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
# File 'lib/optimacms/backup_metadata/backup.rb', line 88

def self.list_backups
  res = []

  #
  files = []

  Dir["#{dir_backups}/**/*.tar.gz"].each do |f|
    files << f
  end

  files = files.sort_by{ |x| File.mtime(x) }.reverse

  res = files.map do |f|
    r = {}
    r[:shortpath] = f.gsub /#{dir_backups}\//, ''
    r[:name] = File.basename r[:shortpath], '.tar.gz'
    r[:path] = f
    r[:size] = File.size(f)

    r
  end


  res
end

.make_backup_dir_path(dirname) ⇒ Object



138
139
140
# File 'lib/optimacms/backup_metadata/backup.rb', line 138

def self.make_backup_dir_path(dirname)
  Rails.root.join(dir_backups, dirname)
end

.make_backup_path(f_basename) ⇒ Object



133
134
135
# File 'lib/optimacms/backup_metadata/backup.rb', line 133

def self.make_backup_path(f_basename)
  Rails.root.join(dir_backups, f_basename)
end

.page_filename(row) ⇒ Object



195
196
197
198
199
200
201
202
203
204
# File 'lib/optimacms/backup_metadata/backup.rb', line 195

def self.page_filename(row)
  if row.parent_id && row.parent_id>0 && row.folder
    "#{row.folder.name}--#{row.name}" rescue "error-#{row.id}"
  elsif row.name
    row.name
  else
    "#{row.id}"
  end

end

.page_to_hash(row) ⇒ Object



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
# File 'lib/optimacms/backup_metadata/backup.rb', line 142

def self.page_to_hash(row)

  # translation
  translation = {}

  row.translations.each do |r|
    translation[r.lang] = {
        lang: r.lang,
        meta_title: r.meta_title,
        meta_keywords: r.meta_keywords,
        meta_description: r.meta_description,
    }
  end

  # return
  {
      id: row.id,
      name: row.name,
      title: row.title,

      url: row.url,
      url_parts_count: row.url_parts_count,
      url_vars_count: row.url_vars_count,
      parsed_url: row.parsed_url,

      #parent_id: row.parent_id,
      parent_name: (row.folder.name rescue nil),

      pos: row.pos,
      redir_url: row.redir_url,

      is_folder: row.is_folder,
      controller_action: row.controller_action,


      status: row.status,
      enabled: row.enabled,

      is_translated: row.is_translated,

      #template_id: row.is_translated,
      template_basepath: (row.template.basepath rescue nil),
      #layout_id: row.layout_id,
      layout_basepath: (row.layout.basepath rescue nil),



      translation: translation

  }

end

.perform_backupObject



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
# File 'lib/optimacms/backup_metadata/backup.rb', line 10

def self.perform_backup()
  d = dir_backups

  backup_name = generate_backup_name

  d_base = File.join(d, backup_name)
  d_pages = File.join(d_base, "pages/")
  unless Dir.exists?(d_pages)
    Optimacms::Fileutils::Fileutils.create_dir_if_not_exists(d_pages)
  end

  d_templates = File.join(d_base, "templates/")
  unless Dir.exists?(d_templates)
    Optimacms::Fileutils::Fileutils.create_dir_if_not_exists(d_templates)
  end

  # pages
  pages_all = Page.order("id asc").all

  pages_all.each do |p|
    s = JSON.pretty_generate(page_to_hash(p))

    pfname = page_filename(p)
    f = File.join(d_pages, pfname+".json")

    File.open(f, "w+") do |f|
      f.write(s)
    end
  end

  # templates
  templates_all = Template.order("id asc").all

  templates_all.each do |p|
    s = JSON.pretty_generate(template_to_hash(p))

    f = File.join(d_templates, template_filename(p)+".json")
    File.open(f, "w+") do |f|
      f.write(s)
    end
  end


  # archive
  f_basename = File.basename(d_base)
  `cd #{d} && tar cvzf #{f_basename}.tar.gz #{f_basename}`

  #
  output = ""
  return {res: 1, output: output}
rescue => e
  output = "exception. #{e.message}"
  return {res: 0, output: output}

end

.template_filename(row) ⇒ Object



209
210
211
# File 'lib/optimacms/backup_metadata/backup.rb', line 209

def self.template_filename(row)
  row.basepath.gsub /\//, "--"
end

.template_to_hash(row) ⇒ Object



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
# File 'lib/optimacms/backup_metadata/backup.rb', line 213

def self.template_to_hash(row)
  ancestry_path = (row.ancestors.map{|r| r.basepath}.join(",") rescue nil)

  #
  {
      id: row.id,
      title: row.title,
      basename: row.basename,
      basepath: row.basepath,
      basedirpath: row.basedirpath,

      #type_id: row.type_id,
      type_name: (row.type.name rescue nil),

      tpl_format: row.tpl_format,
      pos: row.pos,

      is_translated: row.is_translated,
      is_folder: row.is_folder,
      enabled: row.enabled,

      ancestry_path: ancestry_path

  }
end

.upload_backup(temp_file) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/optimacms/backup_metadata/backup.rb', line 67

def self.upload_backup(temp_file)
  # download file
  uploaded_file = temp_file
  #f_basename = File.basename(uploaded_file.original_filename)
  filename = make_backup_path uploaded_file.original_filename

  d = File.dirname(filename)
  unless Dir.exists?(d)
    Optimacms::Fileutils::Fileutils.create_dir_if_not_exists(filename.to_s)
  end

  File.open(filename, 'wb') do |f|
    f.write(uploaded_file.read)
  end

  # untar
  output = `cd #{d} && tar -xzvf #{File.basename(filename)}`

  return true
end