| 1 | diff --git a/src/Admin/Migrations/Backup.php b/src/Admin/Migrations/Backup.php |
| 2 | new file mode 100644 |
| 3 | index 0000000..79acac6 |
| 4 | --- /dev/null |
| 5 | +++ b/src/Admin/Migrations/Backup.php |
| 6 | @@ -0,0 +1,79 @@ |
| 7 | +<?php |
| 8 | +/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 9 | +/* |
| 10 | +# ***** BEGIN LICENSE BLOCK ***** |
| 11 | +# This file is part of Plume Framework, a simple PHP Application Framework. |
| 12 | +# Copyright (C) 2001-2009 Loic d'Anterroches and contributors. |
| 13 | +# |
| 14 | +# Plume Framework is free software; you can redistribute it and/or modify |
| 15 | +# it under the terms of the GNU Lesser General Public License as published by |
| 16 | +# the Free Software Foundation; either version 2.1 of the License, or |
| 17 | +# (at your option) any later version. |
| 18 | +# |
| 19 | +# Plume Framework is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU Lesser General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU Lesser General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 27 | +# |
| 28 | +# ***** END LICENSE BLOCK ***** */ |
| 29 | + |
| 30 | +/** |
| 31 | + * Backup of the Plume Framework. |
| 32 | + * |
| 33 | + * Backup all the data of the framework models. By default the backup |
| 34 | + * name will be the date YYYY-MM-DD. |
| 35 | + * |
| 36 | + * @param string Path to the folder where to store the backup |
| 37 | + * @param string Name of the backup (null) |
| 38 | + * @return int The backup was correctly written |
| 39 | + */ |
| 40 | +function Admin_Migrations_Backup_run($folder, $name=null) |
| 41 | +{ |
| 42 | + $db = Pluf::db(); |
| 43 | + $models = array( |
| 44 | + 'Admin_Log', |
| 45 | + ); |
| 46 | + |
| 47 | + // Now, for each table, we dump the content in json, this is a |
| 48 | + // memory intensive operation |
| 49 | + $to_json = array(); |
| 50 | + foreach ($models as $model) { |
| 51 | + $to_json[$model] = Admin_Test_Fixture::dump($model, false); |
| 52 | + } |
| 53 | + if (null == $name) { |
| 54 | + $name = date('Y-m-d'); |
| 55 | + } |
| 56 | + return file_put_contents(sprintf('%s/%s-Admin.json', $folder, $name), |
| 57 | + json_encode($to_json), LOCK_EX); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Restore Pluf from a backup. |
| 62 | + * |
| 63 | + * @param string Path to the backup folder |
| 64 | + * @param string Backup name |
| 65 | + * @return bool Success |
| 66 | + */ |
| 67 | +function Admin_Migrations_Backup_restore($folder, $name) |
| 68 | +{ |
| 69 | + $models = array( |
| 70 | + 'Admin_Log', |
| 71 | + ); |
| 72 | + |
| 73 | + $db = Pluf::db(); |
| 74 | + $schema = new Admin_DB_Schema($db); |
| 75 | + foreach ($models as $model) { |
| 76 | + $schema->model = new $model(); |
| 77 | + $schema->createTables(); |
| 78 | + } |
| 79 | + |
| 80 | + $full_data = json_decode(file_get_contents(sprintf('%s/%s-Admin.json', $folder, $name)), true); |
| 81 | + foreach ($full_data as $model => $data) { |
| 82 | + Admin_Test_Fixture::load($data, false); |
| 83 | + } |
| 84 | + return true; |
| 85 | +} |
| 86 | |