Commit ae88482d15d37bc5d2effd5b796d606c59a81ba0
1 parent
742fdfb6
Exists in
master
and in
79 other branches
Noosfero: apply patch with backup and restore task
Showing
2 changed files
with
140 additions
and
1 deletions
Show diff stats
patches/noosfero/0001-Enhance-existing-backup-task-and-add-a-restore-one.patch
0 → 100644
@@ -0,0 +1,137 @@ | @@ -0,0 +1,137 @@ | ||
1 | +From c1918dc6090c011a297bc470c04d4158410e61b8 Mon Sep 17 00:00:00 2001 | ||
2 | +From: Antonio Terceiro <terceiro@colivre.coop.br> | ||
3 | +Date: Wed, 11 Feb 2015 19:55:30 -0200 | ||
4 | +Subject: [PATCH] Enhance existing backup task and add a restore one | ||
5 | + | ||
6 | +- `rake backup` will now create a tarball with everything that needs to | ||
7 | + be backed up (files + database dump) | ||
8 | + | ||
9 | +- `rake restore BACKUP=/path/to/(...).tar.gz` will restore that backup | ||
10 | + over the current Noosfero instance. | ||
11 | + | ||
12 | +Multi-tenant setups are not supported yet. | ||
13 | +--- | ||
14 | + lib/tasks/backup.rake | 111 +++++++++++++++++++++++++++++++++++++++++++++++--- | ||
15 | + 1 file changed, 105 insertions(+), 6 deletions(-) | ||
16 | + | ||
17 | +diff --git a/lib/tasks/backup.rake b/lib/tasks/backup.rake | ||
18 | +index 849d4d6..7e64d17 100644 | ||
19 | +--- a/lib/tasks/backup.rake | ||
20 | ++++ b/lib/tasks/backup.rake | ||
21 | +@@ -1,8 +1,107 @@ | ||
22 | +-desc "Creates a backup of the user files stored in public/" | ||
23 | +-task :backup do | ||
24 | +- dirs = Dir.glob('public/images/[0-9][0-9][0-9][0-9]') + ['public/articles', 'public/thumbnails', 'public/user_themes'].select { |d| File.exists?(d) } | ||
25 | +- tarball = 'backups/files-' + Time.now.strftime('%Y-%m-%d-%R') + '.tar' | ||
26 | ++task :load_backup_config do | ||
27 | ++ $config = YAML.load_file('config/database.yml') | ||
28 | ++end | ||
29 | ++ | ||
30 | ++task :check_backup_support => :load_backup_config do | ||
31 | ++ if $config['production']['adapter'] != 'postgresql' | ||
32 | ++ fail("Only PostgreSQL is supported for backups at the moment") | ||
33 | ++ end | ||
34 | ++end | ||
35 | ++ | ||
36 | ++backup_dirs = [ | ||
37 | ++ 'public/image_uploads', | ||
38 | ++ 'public/articles', | ||
39 | ++ 'public/thumbnails', | ||
40 | ++ 'public/user_themes', | ||
41 | ++] | ||
42 | ++ | ||
43 | ++desc "Creates a backup of the database and uploaded files" | ||
44 | ++task :backup => :check_backup_support do | ||
45 | ++ dirs = backup_dirs.select { |d| File.exists?(d) } | ||
46 | ++ | ||
47 | ++ backup_name = Time.now.strftime('%Y-%m-%d-%R') | ||
48 | ++ backup_file = File.join('tmp/backup', backup_name) + '.tar.gz' | ||
49 | ++ mkdir_p 'tmp/backup' | ||
50 | ++ dump = File.join('tmp/backup', backup_name) + '.sql' | ||
51 | ++ | ||
52 | ++ database = $config['production']['database'] | ||
53 | ++ sh "pg_dump #{database} > #{dump}" | ||
54 | ++ | ||
55 | ++ sh 'tar', 'caf', backup_file, dump, *dirs | ||
56 | ++ rm_f dump | ||
57 | ++ | ||
58 | ++ puts "****************************************************" | ||
59 | ++ puts "Backup in #{backup_file} !" | ||
60 | ++ puts | ||
61 | ++ puts "To restore, use:" | ||
62 | ++ puts "$ rake restore BACKUP=#{backup_file}" | ||
63 | ++ puts "****************************************************" | ||
64 | ++end | ||
65 | ++ | ||
66 | ++def invalid_backup!(message, items=[]) | ||
67 | ++ puts "E: #{message}" | ||
68 | ++ items.each do |i| | ||
69 | ++ puts "E: - #{i}" | ||
70 | ++ end | ||
71 | ++ puts "E: Is this a backup archive created by Noosfero with \`rake backup\`?" | ||
72 | ++ exit 1 | ||
73 | ++end | ||
74 | ++ | ||
75 | ++desc "Restores a backup created previousy with \`rake backup\`" | ||
76 | ++task :restore => :check_backup_support do | ||
77 | ++ backup = ENV["BACKUP"] | ||
78 | ++ unless backup | ||
79 | ++ puts "usage: rake restore BACKUP=/path/to/backup" | ||
80 | ++ exit 1 | ||
81 | ++ end | ||
82 | ++ | ||
83 | ++ files = `tar taf #{backup}`.split | ||
84 | ++ | ||
85 | ++ # validate files in the backup | ||
86 | ++ invalid_files = [] | ||
87 | ++ files.each do |f| | ||
88 | ++ if f !~ /tmp\/backup\// && (backup_dirs.none? { |d| f =~ /^#{d}\// }) | ||
89 | ++ invalid_files << f | ||
90 | ++ end | ||
91 | ++ end | ||
92 | ++ if invalid_files.size > 0 | ||
93 | ++ invalid_backup!("Invalid files found in the backup archive", invalid_files) | ||
94 | ++ end | ||
95 | ++ | ||
96 | ++ # find database dump in the archive | ||
97 | ++ dumps = files.select do |f| | ||
98 | ++ File.dirname(f) == 'tmp/backup' && f =~ /\.sql$/ | ||
99 | ++ end | ||
100 | ++ if dumps.size == 0 | ||
101 | ++ invalid_backup!("Could not find a database dump in the archive.") | ||
102 | ++ elsif dumps.size > 1 | ||
103 | ++ invalid_backup!("Multiple database dumps found in the archive:", dumps) | ||
104 | ++ end | ||
105 | ++ dump = dumps.first | ||
106 | ++ | ||
107 | ++ database = $config['production']['database'] | ||
108 | ++ username = $config['production']['username'] | ||
109 | ++ | ||
110 | ++ puts "WARNING: backups should be restored to an empty database, otherwise" | ||
111 | ++ puts "data from the backup may not be loaded properly." | ||
112 | ++ puts | ||
113 | ++ puts 'You can remove the existing database and create a new one with:' | ||
114 | ++ puts | ||
115 | ++ puts "$ sudo -u postgres dropdb #{database}" | ||
116 | ++ puts "$ sudo -u postgres createdb #{database} --owner #{username}" | ||
117 | ++ puts | ||
118 | ++ print "Are you sure you want to continue (y/N)? " | ||
119 | ++ response = $stdin.gets.strip | ||
120 | ++ unless ['y', 'yes'].include?(response.downcase) | ||
121 | ++ puts "*** ABORTED." | ||
122 | ++ exit 1 | ||
123 | ++ end | ||
124 | ++ | ||
125 | ++ sh 'tar', 'xaf', backup | ||
126 | ++ sh "rails dbconsole production < #{dump}" | ||
127 | ++ rm_f dump | ||
128 | + | ||
129 | +- mkdir_p(File.dirname(tarball)) | ||
130 | +- sh('tar', 'cf', tarball, *dirs) | ||
131 | ++ puts "****************************************************" | ||
132 | ++ puts "Backup restored!" | ||
133 | ++ puts "****************************************************" | ||
134 | + end | ||
135 | +-- | ||
136 | +2.1.4 | ||
137 | + |
specs/noosfero/noosfero.spec
1 | %define writable_dirs javascripts/cache stylesheets/cache articles image_uploads thumbnails | 1 | %define writable_dirs javascripts/cache stylesheets/cache articles image_uploads thumbnails |
2 | 2 | ||
3 | Name: noosfero | 3 | Name: noosfero |
4 | -Version: 1.1~rc2.3 | 4 | +Version: 1.1~rc2.4 |
5 | Release: 2%{?dist} | 5 | Release: 2%{?dist} |
6 | Summary: Social Networking Platform | 6 | Summary: Social Networking Platform |
7 | Group: Applications/Publishing | 7 | Group: Applications/Publishing |
@@ -9,6 +9,7 @@ License: AGPLv3 | @@ -9,6 +9,7 @@ License: AGPLv3 | ||
9 | URL: http://noosfero.org | 9 | URL: http://noosfero.org |
10 | Source0: %{name}-%{version}.tar.gz | 10 | Source0: %{name}-%{version}.tar.gz |
11 | Patch0: 0001-Use-as-placeholder-for-current-user-in-URLs.patch | 11 | Patch0: 0001-Use-as-placeholder-for-current-user-in-URLs.patch |
12 | +Patch1: 0001-Enhance-existing-backup-task-and-add-a-restore-one.patch | ||
12 | BuildArch: noarch | 13 | BuildArch: noarch |
13 | BuildRequires: noosfero-deps, gettext, po4a | 14 | BuildRequires: noosfero-deps, gettext, po4a |
14 | Requires: noosfero-deps, po4a, tango-icon-theme, memcached | 15 | Requires: noosfero-deps, po4a, tango-icon-theme, memcached |
@@ -23,6 +24,7 @@ participate and contribute to this free software project! | @@ -23,6 +24,7 @@ participate and contribute to this free software project! | ||
23 | %setup -q | 24 | %setup -q |
24 | 25 | ||
25 | %patch0 -p1 | 26 | %patch0 -p1 |
27 | +%patch1 -p1 | ||
26 | 28 | ||
27 | %build | 29 | %build |
28 | 30 |