First commit
This commit is contained in:
commit
c6e2478c40
13918 changed files with 2303184 additions and 0 deletions
339
sites/all/modules/backup_migrate/LICENSE.txt
Normal file
339
sites/all/modules/backup_migrate/LICENSE.txt
Normal file
|
@ -0,0 +1,339 @@
|
|||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Lesser General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License.
|
89
sites/all/modules/backup_migrate/README.txt
Normal file
89
sites/all/modules/backup_migrate/README.txt
Normal file
|
@ -0,0 +1,89 @@
|
|||
|
||||
-------------------------------------------------------------------------------
|
||||
Backup and Migrate 3 for Drupal 7.x
|
||||
by Ronan Dowling, Gorton Studios - ronan (at) gortonstudios (dot) com
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
DESCRIPTION:
|
||||
This module makes the task of backing up your Drupal database, code and uploaded
|
||||
files and of migrating data from one Drupal install to another easier.
|
||||
|
||||
Database backup files are a list of SQL statements which can be executed with a
|
||||
tool such as phpMyAdmin or the command-line mysql client. File and code
|
||||
backup files are tarballs which can be restored by extracting them to the
|
||||
desired directory.
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
INSTALLATION:
|
||||
* Put the module in your Drupal modules directory and enable it in
|
||||
admin/modules.
|
||||
* Go to admin/people/permissions and grant permission to any roles that need to be
|
||||
able to backup or restore the database.
|
||||
* Configure and use the module at admin/config/system/backup_migrate
|
||||
|
||||
OPTIONAL:
|
||||
* Enable token.module to allow token replacement in backup file names.
|
||||
* To Backup to Amazon S3:
|
||||
- Download the S3 library from http://undesigned.org.za/2007/10/22/amazon-s3-php-class
|
||||
and place the file 'S3.php' in the includes directory in this module.
|
||||
The stable version (0.4.0 – 20th Jul 2009) works best with Backup and Migrate.
|
||||
|
||||
LIGHTTPD USERS:
|
||||
Add the following code to your lighttp.conf to secure your backup directories:
|
||||
$HTTP["url"] =~ "^/sites/default/files/backup_migrate/" {
|
||||
url.access-deny = ( "" )
|
||||
}
|
||||
You may need to adjust the path to reflect the actual path to the files.
|
||||
|
||||
IIS 7 USERS:
|
||||
Add the following code to your web.config code to secure your backup directories:
|
||||
<rule name="postinst-redirect" stopProcessing="true">
|
||||
<match url="sites/default/files/backup_migrate" />
|
||||
<action type="Rewrite" url=""/>
|
||||
</rule>
|
||||
You may need to adjust the path to reflect the actual path to the files.
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
VERY IMPORTANT SECURITY NOTE:
|
||||
Backup files may contain sensitive data and by default, are saved to your web
|
||||
server in a directory normally accessible by the public. This could lead to a
|
||||
very serious security vulnerability. Backup and Migrate attempts to protect
|
||||
backup files using a .htaccess file, but this is not guaranteed to work on all
|
||||
environments (and is guaranteed to fail on web servers that are not apache). You
|
||||
should test to see if your backup files are publicly accessible, and if in doubt
|
||||
do not save backups to the server, or use the destinations feature to save to a
|
||||
folder outside of your webroot.
|
||||
|
||||
OTHER WARNINGS:
|
||||
A failed restore can destroy your database and therefore your entire Drupal
|
||||
installation. ALWAYS TEST BACKUP FILES ON A TEST ENVIRONMENT FIRST. If in doubt
|
||||
do not use this module.
|
||||
|
||||
This module has only been tested with MySQL and does not work with any other dbms.
|
||||
If you have experiences with Postgres or any other dbms and are willing to help
|
||||
test and modify the module to work with it, please contact the developer at
|
||||
ronan (at) gortonstudios (dot) com.
|
||||
|
||||
Make sure your php timeout is set high enough to complete a backup or restore
|
||||
operation. Larger databases require more time. Also, while the module attempts
|
||||
to keep memory needs to a minimum, a backup or restore will require
|
||||
significantly more memory then most Drupal operations.
|
||||
|
||||
If your backup file contains the 'sessions' table all other users will be logged
|
||||
out after you run a restore. To avoid this, exclude the sessions table when
|
||||
creating your backups. Be aware though that you will need to recreate the
|
||||
sessions table if you use this backup on an empty database.
|
||||
|
||||
Do not change the file extension of backup files or the restore function will be
|
||||
unable to determine the compression type the file and will not function
|
||||
correctly.
|
||||
|
||||
IF A RESTORE FAILS:
|
||||
Don't panic, the restore file should work with phpMyAdmin's import function, or
|
||||
with the mysql command line tool. If it does not, then it is likely corrupt; you
|
||||
may panic now. MAKE SURE THAT THIS MODULE IS NOT YOUR ONLY FORM OF BACKUP.
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
|
67
sites/all/modules/backup_migrate/backup_migrate.css
Normal file
67
sites/all/modules/backup_migrate/backup_migrate.css
Normal file
|
@ -0,0 +1,67 @@
|
|||
.schedule-list-disabled
|
||||
{
|
||||
filter:alpha(opacity=50);
|
||||
-moz-opacity: .50;
|
||||
opacity: .50;
|
||||
}
|
||||
|
||||
.backup-migrate-cache-time {
|
||||
font-size: 0.85em;
|
||||
}
|
||||
|
||||
.backup-migrate-tables-checkboxes .form-item {
|
||||
width: 15em;
|
||||
float: left;
|
||||
overflow: hidden;
|
||||
white-space: nowrap;
|
||||
height: 1.75em;
|
||||
margin: .25em .25em 0 0;
|
||||
}
|
||||
.backup-migrate-tables-checkboxes .form-item label input {
|
||||
margin-right: .5em;
|
||||
}
|
||||
|
||||
div.backup-migrate-tables-checkboxes {
|
||||
height: 20em;
|
||||
overflow: auto;
|
||||
}
|
||||
div.backup-migrate-tags,
|
||||
div.backup-migrate-description,
|
||||
div.backup-migrate-date
|
||||
{
|
||||
font-size: smaller;
|
||||
}
|
||||
span.backup-migrate-label {
|
||||
font-weight: bold;
|
||||
}
|
||||
table.backup-migrate-listing td {
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
div.row-error {
|
||||
color: #FF0000;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.backup-migrate-listing {
|
||||
margin-bottom: 2em;
|
||||
}
|
||||
.backup-migrate-actions {
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
html.js #backup-migrate-ui-manual-quick-backup-form fieldset.collapsed,
|
||||
.backup-migrate-inline {
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
.backup-migrate-form-dependent {
|
||||
padding-left: 2em;
|
||||
padding-bottom: 1em;
|
||||
}
|
||||
.backup-migrate-form-dependent .form-item {
|
||||
margin-top: 0;
|
||||
}
|
||||
.backup-migrate-footer-message {
|
||||
margin-bottom: 1em;
|
||||
}
|
16
sites/all/modules/backup_migrate/backup_migrate.info
Normal file
16
sites/all/modules/backup_migrate/backup_migrate.info
Normal file
|
@ -0,0 +1,16 @@
|
|||
name = Backup and Migrate
|
||||
description = "Backup the Drupal database and files or migrate them to another environment."
|
||||
core = 7.x
|
||||
|
||||
files[] = includes/destinations.inc
|
||||
files[] = includes/profiles.inc
|
||||
files[] = includes/schedules.inc
|
||||
|
||||
configure = admin/config/system/backup_migrate
|
||||
|
||||
; Information added by Drupal.org packaging script on 2017-10-18
|
||||
version = "7.x-3.3"
|
||||
core = "7.x"
|
||||
project = "backup_migrate"
|
||||
datestamp = "1508339653"
|
||||
|
664
sites/all/modules/backup_migrate/backup_migrate.install
Normal file
664
sites/all/modules/backup_migrate/backup_migrate.install
Normal file
|
@ -0,0 +1,664 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Install hooks for Backup and Migrate.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of hook_requirements().
|
||||
*/
|
||||
function backup_migrate_requirements($phase) {
|
||||
$requirements = array();
|
||||
return $requirements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_schema().
|
||||
*/
|
||||
function backup_migrate_schema() {
|
||||
$schema['backup_migrate_profiles'] = array(
|
||||
'export' => array(
|
||||
'key' => 'machine_name',
|
||||
'key name' => 'Profile ID',
|
||||
'admin_title' => 'name',
|
||||
'primary key' => 'profile_id',
|
||||
'identifier' => 'item', // Exports will be defined as $preset
|
||||
'default hook' => 'exportables_backup_migrate_profiles', // Function hook name.
|
||||
'api' => array(
|
||||
'owner' => 'backup_migrate',
|
||||
'api' => 'backup_migrate_exportables', // Base name for api include files.
|
||||
'minimum_version' => 1,
|
||||
'current_version' => 1,
|
||||
),
|
||||
),
|
||||
'fields' => array(
|
||||
'profile_id' => array(
|
||||
'type' => 'serial',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
|
||||
'no export' => TRUE, // Do not export database-only keys.
|
||||
),
|
||||
'machine_name' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '0',
|
||||
'description' => 'The primary identifier for a profile.',
|
||||
),
|
||||
'name' => array(
|
||||
'description' => 'The name of the profile.',
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE
|
||||
),
|
||||
'filename' => array(
|
||||
'description' => 'The name of the profile.',
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE
|
||||
),
|
||||
'append_timestamp' => array(
|
||||
'description' => 'Append a timestamp to the filename.',
|
||||
'type' => 'int',
|
||||
'size' => 'tiny',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'default' => 0
|
||||
),
|
||||
'timestamp_format' => array(
|
||||
'description' => 'The format of the timestamp.',
|
||||
'type' => 'varchar',
|
||||
'length' => 14,
|
||||
'not null' => TRUE
|
||||
),
|
||||
'filters' => array(
|
||||
'description' => 'The filter settings for the profile.',
|
||||
'type' => 'text',
|
||||
'not null' => TRUE,
|
||||
'serialize' => TRUE,
|
||||
'serialized default' => 'a:0:{}',
|
||||
),
|
||||
),
|
||||
'primary key' => array('profile_id'),
|
||||
);
|
||||
$schema['backup_migrate_destinations'] = array(
|
||||
'export' => array(
|
||||
'key' => 'machine_name',
|
||||
'key name' => 'Destination ID',
|
||||
'admin_title' => 'name',
|
||||
'primary key' => 'destination_id',
|
||||
'identifier' => 'item', // Exports will be defined as $preset
|
||||
'default hook' => 'exportables_backup_migrate_destinations', // Function hook name.
|
||||
'api' => array(
|
||||
'owner' => 'backup_migrate',
|
||||
'api' => 'backup_migrate_exportables', // Base name for api include files.
|
||||
'minimum_version' => 1,
|
||||
'current_version' => 1,
|
||||
),
|
||||
),
|
||||
'fields' => array(
|
||||
'destination_id' => array(
|
||||
'type' => 'serial',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
|
||||
'no export' => TRUE, // Do not export database-only keys.
|
||||
),
|
||||
'machine_name' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '0',
|
||||
'description' => 'The primary identifier for a destination.',
|
||||
),
|
||||
'name' => array(
|
||||
'description' => 'The name of the destination.',
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE
|
||||
),
|
||||
'subtype' => array(
|
||||
'description' => 'The type of the destination.',
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'not null' => TRUE
|
||||
),
|
||||
'location' => array(
|
||||
'description' => 'The the location string of the destination.',
|
||||
'type' => 'text',
|
||||
'not null' => TRUE
|
||||
),
|
||||
'settings' => array(
|
||||
'description' => 'Other settings for the destination.',
|
||||
'type' => 'text',
|
||||
'not null' => TRUE,
|
||||
'serialize' => TRUE,
|
||||
'serialized default' => 'a:0:{}',
|
||||
),
|
||||
),
|
||||
'primary key' => array('destination_id'),
|
||||
);
|
||||
$schema['backup_migrate_sources'] = array(
|
||||
'export' => array(
|
||||
'key' => 'machine_name',
|
||||
'key name' => 'Source ID',
|
||||
'admin_title' => 'name',
|
||||
'primary key' => 'source_id',
|
||||
'identifier' => 'item', // Exports will be defined as $preset
|
||||
'default hook' => 'exportables_backup_migrate_sources', // Function hook name.
|
||||
'api' => array(
|
||||
'owner' => 'backup_migrate',
|
||||
'api' => 'backup_migrate_exportables', // Base name for api include files.
|
||||
'minimum_version' => 1,
|
||||
'current_version' => 1,
|
||||
),
|
||||
),
|
||||
'fields' => array(
|
||||
'source_id' => array(
|
||||
'type' => 'serial',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
|
||||
'no export' => TRUE, // Do not export database-only keys.
|
||||
),
|
||||
'machine_name' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '0',
|
||||
'description' => 'The primary identifier for a source.',
|
||||
),
|
||||
'name' => array(
|
||||
'description' => 'The name of the source.',
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE
|
||||
),
|
||||
'subtype' => array(
|
||||
'description' => 'The type of the source.',
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'not null' => TRUE
|
||||
),
|
||||
'location' => array(
|
||||
'description' => 'The the location string of the source.',
|
||||
'type' => 'text',
|
||||
'not null' => TRUE
|
||||
),
|
||||
'settings' => array(
|
||||
'description' => 'Other settings for the source.',
|
||||
'type' => 'text',
|
||||
'not null' => TRUE,
|
||||
'serialize' => TRUE,
|
||||
'serialized default' => 'a:0:{}',
|
||||
),
|
||||
),
|
||||
'primary key' => array('source_id'),
|
||||
);
|
||||
|
||||
$schema['backup_migrate_schedules'] = array(
|
||||
'export' => array(
|
||||
'key' => 'machine_name',
|
||||
'key name' => 'Source ID',
|
||||
'admin_title' => 'name',
|
||||
'primary key' => 'schedule_id',
|
||||
'identifier' => 'item', // Exports will be defined as $preset
|
||||
'default hook' => 'exportables_backup_migrate_schedules', // Function hook name.
|
||||
'api' => array(
|
||||
'owner' => 'backup_migrate',
|
||||
'api' => 'backup_migrate_exportables', // Base name for api include files.
|
||||
'minimum_version' => 1,
|
||||
'current_version' => 1,
|
||||
),
|
||||
),
|
||||
'fields' => array(
|
||||
'schedule_id' => array(
|
||||
'type' => 'serial',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
|
||||
'no export' => TRUE, // Do not export database-only keys.
|
||||
),
|
||||
'machine_name' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '0',
|
||||
'description' => 'The primary identifier for a profile.',
|
||||
),
|
||||
'name' => array(
|
||||
'description' => 'The name of the profile.',
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE
|
||||
),
|
||||
'source_id' => array(
|
||||
'description' => 'The {backup_migrate_destination}.destination_id of the source to backup from.',
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'default' => 'db',
|
||||
'not null' => TRUE
|
||||
),
|
||||
'destination_id' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '0',
|
||||
'description' => 'The {backup_migrate_destination}.destination_id of the destination to back up to.',
|
||||
),
|
||||
'copy_destination_id' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'not null' => TRUE,
|
||||
'default' => '0',
|
||||
'description' => 'A second {backup_migrate_destination}.destination_id of the destination to copy the backup to.',
|
||||
),
|
||||
'profile_id' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '0',
|
||||
'description' => 'The primary identifier for a profile.',
|
||||
),
|
||||
'keep' => array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
'description' => 'The number of backups to keep.',
|
||||
),
|
||||
'period' => array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
'description' => 'The number of seconds between backups.',
|
||||
),
|
||||
'enabled' => array(
|
||||
'description' => 'Whether the schedule is enabled.',
|
||||
'type' => 'int',
|
||||
'size' => 'tiny',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'default' => 0
|
||||
),
|
||||
'cron' => array(
|
||||
'description' => 'Whether the schedule should be run during cron.',
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'not null' => TRUE,
|
||||
'default' => 'builtin',
|
||||
),
|
||||
'cron_schedule' => array(
|
||||
'description' => 'The cron schedule to run on.',
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '0 4 * * *',
|
||||
),
|
||||
),
|
||||
'primary key' => array('schedule_id'),
|
||||
);
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_install().
|
||||
*/
|
||||
function backup_migrate_install() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove variables on uninstall.
|
||||
*/
|
||||
function backup_migrate_uninstall() {
|
||||
db_query("DELETE FROM {variable} WHERE name LIKE 'backup_migrate_%'");
|
||||
db_query("DELETE FROM {variable} WHERE name LIKE 'nodesquirrel_%'");
|
||||
cache_clear_all('variables', 'cache');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update from 1.x to 2.x.
|
||||
*/
|
||||
function backup_migrate_update_2000() {
|
||||
_backup_migrate_setup_database_defaults();
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adding filter field for dev release of 2009-01-28
|
||||
*/
|
||||
function backup_migrate_update_2001() {
|
||||
$ret = array();
|
||||
$schema = drupal_get_schema_unprocessed('backup_migrate', 'backup_migrate_profiles');
|
||||
|
||||
// Add the filters field to the db.
|
||||
if (!db_field_exists('backup_migrate_profiles', 'filters')) {
|
||||
db_add_field('backup_migrate_profiles', 'filters', array('description' => t('The filter settings for the profile.'),'type' => 'text', 'not null' => TRUE));
|
||||
}
|
||||
// Add the source field
|
||||
if (!db_field_exists('backup_migrate_profiles', 'source_id')) {
|
||||
db_add_field('backup_migrate_profiles', 'source_id', array('description' => t('The {backup_migrate_destination}.destination_id of the source to backup from.'), 'type' => 'varchar', 'length' => 255, 'default' => 'db', 'not null' => TRUE));
|
||||
}
|
||||
// Remove the compression field.
|
||||
if (db_field_exists('backup_migrate_profiles', 'compression')) {
|
||||
db_drop_field('backup_migrate_profiles', 'compression');
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clearing the cache because there was a menu structure change in the dev of 2009-05-31
|
||||
*/
|
||||
function backup_migrate_update_2002() {
|
||||
// Cache should clear automatically. Nothing to do here.
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Allowing non-int profile ids in schedules 2009-05-31
|
||||
*/
|
||||
function backup_migrate_update_2003() {
|
||||
$ret = array();
|
||||
$spec = array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '0',
|
||||
'description' => 'The primary identifier for a profile.',
|
||||
);
|
||||
|
||||
db_change_field('backup_migrate_schedules', 'profile_id', 'profile_id', $spec);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allowing non-int profile ids 2009-07-01
|
||||
*/
|
||||
function backup_migrate_update_2004() {
|
||||
$ret = array();
|
||||
$spec = array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '0',
|
||||
);
|
||||
|
||||
$spec['description'] = 'The primary identifier for a destination.';
|
||||
db_change_field('backup_migrate_destinations', 'destination_id', 'destination_id', $spec);
|
||||
|
||||
$spec['description'] = 'The primary identifier for a profile.';
|
||||
db_change_field('backup_migrate_profiles', 'profile_id', 'profile_id', $spec);
|
||||
|
||||
$spec['description'] = 'The primary identifier for a schedule.';
|
||||
db_change_field('backup_migrate_schedules', 'schedule_id', 'schedule_id', $spec);
|
||||
|
||||
// Drop the user/pass fields as they weren't being used.
|
||||
if (db_field_exists('backup_migrate_destinations', 'username')) {
|
||||
db_drop_field('backup_migrate_destinations', 'username');
|
||||
}
|
||||
if (db_field_exists('backup_migrate_destinations', 'password')) {
|
||||
db_drop_field('backup_migrate_destinations', 'password');
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the default database id to something friendlier 2009-08-08
|
||||
*/
|
||||
function backup_migrate_update_2005() {
|
||||
require_once './'. drupal_get_path('module', 'backup_migrate') .'/includes/crud.inc';
|
||||
require_once './'. drupal_get_path('module', 'backup_migrate') .'/includes/profiles.inc';
|
||||
|
||||
$ret = array();
|
||||
// Change the destination ids of the defined database sources mostly to make using them with drush friendlier.
|
||||
|
||||
// Change the db_url:default id to simply 'db'
|
||||
$ret[] = db_query("UPDATE {backup_migrate_profiles} SET source_id = 'db' WHERE source_id = 'db_url:default'");
|
||||
$ret[] = db_query("UPDATE {backup_migrate_schedules} SET destination_id = 'db' WHERE destination_id = 'db_url:default'");
|
||||
|
||||
// Change the defined db keys from db_url:key to db:key.
|
||||
$ret[] = db_query("UPDATE {backup_migrate_profiles} SET source_id = REPLACE(source_id, 'db_url:', 'db:')");
|
||||
$ret[] = db_query("UPDATE {backup_migrate_schedules} SET destination_id = REPLACE(destination_id, 'db_url:', 'db:')");
|
||||
|
||||
// Add the source field to the schedule
|
||||
if (!db_field_exists('backup_migrate_schedules', 'source_id')) {
|
||||
db_add_field('backup_migrate_schedules', 'source_id', array('description' => t('The db source to backup from.'), 'type' => 'varchar', 'length' => 255, 'default' => 'db', 'not null' => TRUE));
|
||||
}
|
||||
|
||||
// Copy source data from profiles to schedules.
|
||||
$result = db_query('SELECT p.source_id, s.schedule_id FROM {backup_migrate_schedules} s LEFT JOIN {backup_migrate_profiles} p ON s.profile_id = p.profile_id', array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
foreach ($result as $schedule) {
|
||||
if (!$schedule['source_id']) {
|
||||
$schedule['source_id'] = 'db';
|
||||
}
|
||||
$ret[] = db_query("UPDATE {backup_migrate_schedules} SET source_id = '". $schedule['source_id'] ."' WHERE schedule_id = '". $schedule['profile_id'] ."'");
|
||||
}
|
||||
|
||||
if (db_field_exists('backup_migrate_profiles', 'source_id')) {
|
||||
db_drop_field('backup_migrate_profiles', 'source_id');
|
||||
}
|
||||
|
||||
// Copy the no-data and exclude tables settings into the 'filter' field.
|
||||
$result = db_query('SELECT * FROM {backup_migrate_profiles}', array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
foreach ($result as $item) {
|
||||
if (isset($item['nodata_tables']) && isset($item['exclude_tables'])) {
|
||||
$profile = backup_migrate_get_profile($item['profile_id']);
|
||||
$profile->filters['nodata_tables'] = unserialize($item['nodata_tables']);
|
||||
$profile->filters['exclude_tables'] = unserialize($item['exclude_tables']);
|
||||
$profile->save();
|
||||
}
|
||||
}
|
||||
if (db_field_exists('backup_migrate_profiles', 'nodata_tables')) {
|
||||
db_drop_field('backup_migrate_profiles', 'nodata_tables');
|
||||
}
|
||||
if (db_field_exists('backup_migrate_profiles', 'exclude_tables')) {
|
||||
db_drop_field('backup_migrate_profiles', 'exclude_tables');
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move the backup and migrate directory to the private directory.
|
||||
*/
|
||||
function backup_migrate_update_7200() {
|
||||
$from = 'public://backup_migrate';
|
||||
$to = 'private://backup_migrate';
|
||||
if (drupal_realpath($from) && !drupal_realpath($to)) {
|
||||
if (!rename($from, $to)) {
|
||||
drupal_set_message(t('Unable to move the backups directory to your private folder, please check file permissions and move the directory %from to %to', array('%from' => drupal_realpath($from), '%to' => drupal_realpath($to))), 'warning');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Change the filename field to support 255 characters.
|
||||
*/
|
||||
function backup_migrate_update_7202() {
|
||||
$ret = array();
|
||||
db_change_field('backup_migrate_profiles', 'filename', 'filename', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE));
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update the schedule last run times to use variables instead of saving with the schedule.
|
||||
*/
|
||||
function backup_migrate_update_7203() {
|
||||
$result = db_query('SELECT * FROM {backup_migrate_schedules}', array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
foreach ($result as $item) {
|
||||
if (isset($item['last_run'])) {
|
||||
variable_set('backup_migrate_schedule_last_run_' . $item['schedule_id'], $item['last_run']);
|
||||
}
|
||||
}
|
||||
if (db_field_exists('backup_migrate_schedules', 'last_run')) {
|
||||
db_drop_field('backup_migrate_schedules', 'last_run');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Uninstall backup migrate files if it's installed
|
||||
*/
|
||||
function backup_migrate_update_7300() {
|
||||
if (module_exists('backup_migrate_files')) {
|
||||
module_disable(array('backup_migrate_files'));
|
||||
$ret[] = array(
|
||||
'success' => true,
|
||||
'query' => 'Disabled the Backup and Migrate Files module',
|
||||
);
|
||||
}
|
||||
if (module_exists('nodesquirrel')) {
|
||||
module_disable(array('nodesquirrel'));
|
||||
$ret[] = array(
|
||||
'success' => true,
|
||||
'query' => 'Disabled the NodeSquirrel module',
|
||||
);
|
||||
}
|
||||
|
||||
// Add sources to the schema.
|
||||
$schema['backup_migrate_sources'] = array(
|
||||
'fields' => array(
|
||||
'source_id' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'not null' => TRUE,
|
||||
'default' => '0',
|
||||
'description' => t('The primary identifier for a source.'),
|
||||
),
|
||||
'name' => array(
|
||||
'description' => t('The name of the source.'),
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE
|
||||
),
|
||||
'type' => array(
|
||||
'description' => t('The type of the source.'),
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'not null' => TRUE
|
||||
),
|
||||
'location' => array(
|
||||
'description' => t('The the location string of the source.'),
|
||||
'type' => 'text',
|
||||
'not null' => TRUE
|
||||
),
|
||||
'settings' => array(
|
||||
'description' => t('Other settings for the source.'),
|
||||
'type' => 'text',
|
||||
'not null' => TRUE,
|
||||
'serialize' => TRUE,
|
||||
'serialized default' => 'a:0:{}',
|
||||
),
|
||||
),
|
||||
'primary key' => array('source_id'),
|
||||
);
|
||||
|
||||
if (!db_table_exists('backup_migrate_sources')) {
|
||||
db_create_table('backup_migrate_sources', $schema['backup_migrate_sources']);
|
||||
}
|
||||
|
||||
// Move custom destinations to sources.
|
||||
$result = db_query("SELECT * FROM {backup_migrate_destinations} WHERE type in ('filesource', 'db')", array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
foreach ($result as $item) {
|
||||
$item['source_id'] = $item['destination_id'];
|
||||
drupal_write_record('backup_migrate_source', $item);
|
||||
}
|
||||
|
||||
// Change 'destination' settings to 'source' settings
|
||||
$result = db_query('SELECT * FROM {backup_migrate_profiles}', array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
foreach ($result as $item) {
|
||||
$item['filters'] = unserialize($item['filters']);
|
||||
$item['filters']['sources'] = $item['filters']['destinations'];
|
||||
unset($item['filters']['destinations']);
|
||||
drupal_write_record('backup_migrate_profiles', $item, array('profile_id'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch the cron switch to text.
|
||||
*/
|
||||
function backup_migrate_update_7301() {
|
||||
db_change_field('backup_migrate_schedules', 'cron', 'cron', array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => 'builtin'));
|
||||
db_add_field('backup_migrate_schedules', 'cron_schedule', array('description' => 'The cron schedule to run on.', 'type' => 'varchar', 'length' => 255, 'default' => '0 4 * * *', 'not null' => TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a second destination to schedules.
|
||||
*/
|
||||
function backup_migrate_update_7302() {
|
||||
db_add_field('backup_migrate_schedules', 'copy_destination_id',
|
||||
array(
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'not null' => TRUE,
|
||||
'default' => '0',
|
||||
'description' => 'A second {backup_migrate_destination}.destination_id of the destination to copy the backup to.',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a serial id field to all tables to allow them to be ctools exportable.
|
||||
*/
|
||||
function backup_migrate_update_7303() {
|
||||
foreach (array('backup_migrate_sources' => 'source_id', 'backup_migrate_destinations' => 'destination_id', 'backup_migrate_schedules' => 'schedule_id', 'backup_migrate_profiles' => 'profile_id') as $table => $id) {
|
||||
// Take the primary key status from the machine name so it can be renamed
|
||||
// A serial field must be defined as a key, so make a temporary index.
|
||||
// See: https://www.drupal.org/node/190027
|
||||
db_add_index($table, 'temp', array($id));
|
||||
db_drop_primary_key($table);
|
||||
// Drop our temporary index.
|
||||
db_drop_index($table, 'temp');
|
||||
|
||||
// Switch the name of the id to 'machine_name' to be more ctools standard
|
||||
db_change_field($table, $id, 'machine_name', array('type' => 'varchar', 'length' => 32, 'not null' => TRUE));
|
||||
|
||||
// Add a serial ID
|
||||
db_add_field($table, $id,
|
||||
array(
|
||||
'type' => 'serial',
|
||||
'unsigned' => TRUE,
|
||||
'not null' => TRUE,
|
||||
'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
|
||||
'no export' => TRUE, // Do not export database-only keys.
|
||||
),
|
||||
array('primary key' => array($id))
|
||||
);
|
||||
}
|
||||
foreach (array('backup_migrate_sources', 'backup_migrate_destinations') as $table) {
|
||||
db_change_field($table, 'type', 'subtype', array('type' => 'varchar', 'length' => 32, 'not null' => TRUE));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update all schedules to use the built in cron if none is specified.
|
||||
*/
|
||||
function backup_migrate_update_7304() {
|
||||
db_query("UPDATE {backup_migrate_schedules} SET cron = 'builtin' WHERE cron = '0'");
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix schema mismatch after upgrade.
|
||||
*/
|
||||
function backup_migrate_update_7305() {
|
||||
foreach (array('backup_migrate_profiles', 'backup_migrate_destinations', 'backup_migrate_sources', 'backup_migrate_schedules') as $table) {
|
||||
db_change_field($table, 'machine_name', 'machine_name', array(
|
||||
'type' => 'varchar',
|
||||
'length' => 255,
|
||||
'not null' => TRUE,
|
||||
'default' => '0',
|
||||
));
|
||||
}
|
||||
db_change_field('backup_migrate_schedules', 'cron', 'cron', array(
|
||||
'type' => 'varchar',
|
||||
'length' => 32,
|
||||
'not null' => TRUE,
|
||||
'default' => 'builtin',
|
||||
));
|
||||
}
|
114
sites/all/modules/backup_migrate/backup_migrate.js
Normal file
114
sites/all/modules/backup_migrate/backup_migrate.js
Normal file
|
@ -0,0 +1,114 @@
|
|||
(function($) {
|
||||
Drupal.behaviors.backupMigrate = {
|
||||
attach: function(context) {
|
||||
if (Drupal.settings.backup_migrate !== undefined) {
|
||||
if (Drupal.settings.backup_migrate.dependents !== undefined) {
|
||||
for (key in Drupal.settings.backup_migrate.dependents) {
|
||||
info = Drupal.settings.backup_migrate.dependents[key];
|
||||
dependent = $('#edit-' + info['dependent']);
|
||||
for (key in info['dependencies']) {
|
||||
$('[name="' + key + '"]').each(function() {
|
||||
var dependentval = info['dependencies'][key];
|
||||
var dependency = $(this);
|
||||
(function(dependent, dependency) {
|
||||
var checkval = function(inval) {
|
||||
if (dependency.attr('type') == 'radio') {
|
||||
var val = $('[name="' + dependency.attr('name') + '"]:checked').val();
|
||||
return val == inval;
|
||||
}
|
||||
else if (dependency.attr('type') == 'checkbox') {
|
||||
return dependency.is(':checked') && inval == dependency.val();
|
||||
}
|
||||
else {
|
||||
return dependency.val() == inval;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
if (!checkval(dependentval)) {
|
||||
// Hide doesn't work inside collapsed fieldsets.
|
||||
dependent.css('display', 'none');
|
||||
}
|
||||
dependency.bind('load change click keypress focus', function() {
|
||||
if (checkval(dependentval)) {
|
||||
dependent.slideDown();
|
||||
}
|
||||
else {
|
||||
dependent.slideUp();
|
||||
}
|
||||
}).load();
|
||||
})(dependent, dependency);
|
||||
});
|
||||
}
|
||||
}
|
||||
for (key in Drupal.settings.backup_migrate.destination_selectors) {
|
||||
var info = Drupal.settings.backup_migrate.destination_selectors[key];
|
||||
(function(info) {
|
||||
var selector = $('#' + info['destination_selector']);
|
||||
var copy = $('#' + info['copy'])
|
||||
var copy_selector = $('#' + info['copy_destination_selector']);
|
||||
var copy_selector_options = {};
|
||||
|
||||
// Store a copy of the secondary selector options.
|
||||
copy_selector.find('optgroup').each(function() {
|
||||
var label = $(this).attr('label');
|
||||
copy_selector_options[label] = [];
|
||||
$(this).find('option').each(function() {
|
||||
copy_selector_options[label].push(this);
|
||||
});
|
||||
$(this).remove();
|
||||
})
|
||||
|
||||
// Assign an action to the main selector to modify the secondary selector
|
||||
selector.each(function() {
|
||||
$(this).bind('load change click keypress focus', function() {
|
||||
var group = $(this).find('option[value=' + $(this).val() + ']').parents('optgroup').attr('label');
|
||||
if (group) {
|
||||
copy.parent().find('.backup-migrate-destination-copy-label').text(info['labels'][group]);
|
||||
copy_selector.empty();
|
||||
for (var key in copy_selector_options) {
|
||||
if (key != group) {
|
||||
copy_selector.append(copy_selector_options[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}).load();
|
||||
});
|
||||
})(info);
|
||||
}
|
||||
// Add the convert to checkboxes functionality to all multiselects.
|
||||
$('#backup-migrate-ui-manual-backup-form select[multiple], #backup-migrate-crud-edit-form select[multiple]').each(function() {
|
||||
var self = this;
|
||||
$(self).after(
|
||||
$('<div class="description backup-migrate-checkbox-link"></div>').append(
|
||||
$('<a>'+ Drupal.settings.backup_migrate.checkboxLinkText +'</a>').click(function() {
|
||||
var $select = $(self);
|
||||
var $checkboxes = $('<div></div>').addClass('backup-migrate-tables-checkboxes');
|
||||
$('option', $select).each(function(i) {
|
||||
$checkboxes.append(
|
||||
$('<div class="form-item"></div>').append(
|
||||
$('<label class="option backup-migrate-table-select">' + this.value + '</label>').prepend(
|
||||
$('<input type="checkbox" class="backup-migrate-tables-checkbox" name="'+ $select.attr('name') +'"'+ (this.selected ? 'checked="checked"' : '') +' value="'+ this.value +'"/>')
|
||||
.bind('click change load', function() {
|
||||
if (this.checked) {
|
||||
$(this).parent().addClass('checked');
|
||||
}
|
||||
else {
|
||||
$(this).parent().removeClass('checked');
|
||||
}
|
||||
}).load()
|
||||
)
|
||||
)
|
||||
);
|
||||
});
|
||||
$select.parent().find('.backup-migrate-checkbox-link').remove();
|
||||
$select.before($checkboxes);
|
||||
$select.hide();
|
||||
})
|
||||
)
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})(jQuery);
|
1871
sites/all/modules/backup_migrate/backup_migrate.module
Normal file
1871
sites/all/modules/backup_migrate/backup_migrate.module
Normal file
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,277 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Drush commands for backup and migrate.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implementation of hook_drush_command().
|
||||
*/
|
||||
function backup_migrate_drush_command() {
|
||||
$items['bam-backup'] = array(
|
||||
'callback' => 'backup_migrate_drush_backup',
|
||||
'description' => dt('Backup the site\'s database with Backup and Migrate.'),
|
||||
'aliases' => array('bb'),
|
||||
'examples' => array(
|
||||
'drush bam-backup' => 'Backup the default databse to the manual backup directory using the default settings.',
|
||||
'drush bam-backup db scheduled mysettings' => 'Backup the database to the scheduled directory using a settings profile called "mysettings"',
|
||||
'drush bam-backup files' => 'Backup the files directory to the manual directory using the default settings. The Backup and Migrate Files module is required for files backups.',
|
||||
),
|
||||
'arguments' => array(
|
||||
'source' => "Optional. The id of the source (usually a database) to backup. Use 'drush bam-sources' to get a list of sources. Defaults to 'db'",
|
||||
'destination' => "Optional. The id of destination to send the backup file to. Use 'drush bam-destinations' to get a list of destinations. Defaults to 'manual'",
|
||||
'profile' => "Optional. The id of a settings profile to use. Use 'drush bam-profiles' to get a list of available profiles. Defaults to 'default'",
|
||||
),
|
||||
);
|
||||
$items['bam-restore'] = array(
|
||||
'callback' => 'backup_migrate_drush_restore',
|
||||
'description' => dt('Restore the site\'s database with Backup and Migrate.'),
|
||||
'arguments' => array(
|
||||
'source' => "Required. The id of the source (usually a database) to restore the backup to. Use 'drush bam-sources' to get a list of sources. Defaults to 'db'",
|
||||
'destination' => "Required. The id of destination to send the backup file to. Use 'drush bam-destinations' to get a list of destinations. Defaults to 'manual'",
|
||||
'backup id' => "Required. The id of a backup file restore. Use 'drush bam-backups' to get a list of available backup files.",
|
||||
),
|
||||
'options' => array(
|
||||
'yes' => 'Skip confirmation',
|
||||
),
|
||||
);
|
||||
$items['bam-destinations'] = array(
|
||||
'callback' => 'backup_migrate_drush_destinations',
|
||||
'description' => dt('Get a list of available destinations.'),
|
||||
);
|
||||
|
||||
$items['bam-sources'] = array(
|
||||
'callback' => 'backup_migrate_drush_sources',
|
||||
'description' => dt('Get a list of available sources.'),
|
||||
);
|
||||
$items['bam-profiles'] = array(
|
||||
'callback' => 'backup_migrate_drush_profiles',
|
||||
'description' => dt('Get a list of available settings profiles.'),
|
||||
);
|
||||
$items['bam-backups'] = array(
|
||||
'callback' => 'backup_migrate_drush_destination_files',
|
||||
'description' => dt('Get a list of previously created backup files.'),
|
||||
'arguments' => array(
|
||||
'destination' => "Optional. The id of destination to list backups from. Use 'drush bam-destinations' to get a list of destinations.",
|
||||
),
|
||||
);
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_drush_help().
|
||||
*/
|
||||
function backup_migrate_drush_help($section) {
|
||||
switch ($section) {
|
||||
case 'drush:bam-backup':
|
||||
return dt("Backup the site's database using default settings.");
|
||||
case 'drush:bam-restore':
|
||||
return dt('Restore the site\'s database with Backup and Migrate.');
|
||||
case 'drush:bam-destinations':
|
||||
return dt('Get a list of available destinations.');
|
||||
case 'drush:bam-profiles':
|
||||
return dt('Get a list of available settings profiles.');
|
||||
case 'drush:bam-backups':
|
||||
return dt('Get a list of previously created backup files.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup the default database.
|
||||
*/
|
||||
function backup_migrate_drush_backup($source_id = 'db', $destination_id = 'manual', $profile_id = 'default') {
|
||||
backup_migrate_include('profiles', 'destinations', 'sources');
|
||||
|
||||
// Set the message mode to logging.
|
||||
_backup_migrate_message_callback('_backup_migrate_message_drush');
|
||||
|
||||
if (!backup_migrate_get_source($source_id)) {
|
||||
_backup_migrate_message("Could not find the source '@source'. Try using 'drush bam-sources' to get a list of available sources or use 'db' to backup the Drupal database.", array('@source' => $source_id), 'error');
|
||||
return;
|
||||
}
|
||||
if (!backup_migrate_get_destination($destination_id)) {
|
||||
_backup_migrate_message("Could not find the destination '@destination'. Try using 'drush bam-destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error');
|
||||
return;
|
||||
}
|
||||
$settings = backup_migrate_get_profile($profile_id);
|
||||
if(!$settings) {
|
||||
_backup_migrate_message("Could not find the profile '@profile'. Try using 'drush bam-profiles' to get a list of available profiles.", array('@profile' => $profile_id), 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
_backup_migrate_message('Starting backup...');
|
||||
$settings->destination_id = $destination_id;
|
||||
$settings->source_id = $source_id;
|
||||
backup_migrate_perform_backup($settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore to the default database.
|
||||
*/
|
||||
function backup_migrate_drush_restore($source_id = '', $destination_id = '', $file_id = '') {
|
||||
|
||||
backup_migrate_include('profiles', 'destinations', 'sources');
|
||||
|
||||
// Set the message mode to drush output.
|
||||
_backup_migrate_message_callback('_backup_migrate_message_drush');
|
||||
|
||||
if (!backup_migrate_get_source($source_id)) {
|
||||
_backup_migrate_message("Could not find the source '@source'. Try using 'drush bam-sources' to get a list of available sources or use 'db' to backup the Drupal database.", array('@source' => $source_id), 'error');
|
||||
return;
|
||||
}
|
||||
if (!$destination = backup_migrate_get_destination($destination_id)) {
|
||||
_backup_migrate_message("Could not find the destination '@destination'. Try using 'drush bam-destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error');
|
||||
return;
|
||||
}
|
||||
else if (!$file_id || !$file = backup_migrate_destination_get_file($destination_id, $file_id)) {
|
||||
_backup_migrate_message("Could not find the file '@file'. Try using 'drush bam-backups @destination' to get a list of available backup files in this destination destinations.", array('@destination' => $destination_id, '@file' => $file_id), 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
drush_print(dt('Restoring will delete some or all of your data and cannot be undone. ALWAYS TEST YOUR BACKUPS ON A NON-PRODUCTION SERVER!'));
|
||||
if (!drush_confirm(dt('Are you sure you want to perform the restore?'))) {
|
||||
return drush_user_abort();
|
||||
}
|
||||
_backup_migrate_message('Starting restore...');
|
||||
$settings = array('source_id' => $source_id);
|
||||
backup_migrate_perform_restore($destination_id, $file_id, $settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of available destinations.
|
||||
*/
|
||||
function backup_migrate_drush_destinations() {
|
||||
return _backup_migrate_drush_destinations('all');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of available sources.
|
||||
*/
|
||||
function backup_migrate_drush_sources() {
|
||||
return _backup_migrate_drush_sources('source');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a list of available destinations with the given op.
|
||||
*/
|
||||
function _backup_migrate_drush_destinations($op = NULL) {
|
||||
backup_migrate_include('destinations');
|
||||
$rows = array(array(dt('ID'), dt('Name'), dt('Operations')));
|
||||
foreach (backup_migrate_get_destinations($op) as $destination) {
|
||||
$rows[] = array(
|
||||
$destination->get_id(),
|
||||
$destination->get_name(),
|
||||
implode (', ', $destination->ops()),
|
||||
);
|
||||
}
|
||||
drush_print_table($rows, TRUE, array(32, 32));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a list of available destinations with the given op.
|
||||
*/
|
||||
function _backup_migrate_drush_sources($op = NULL) {
|
||||
backup_migrate_include('sources');
|
||||
$rows = array(array(dt('ID'), dt('Name'), dt('Operations')));
|
||||
foreach (backup_migrate_get_sources($op) as $destination) {
|
||||
$rows[] = array(
|
||||
$destination->get_id(),
|
||||
$destination->get_name(),
|
||||
implode (', ', $destination->ops()),
|
||||
);
|
||||
}
|
||||
drush_print_table($rows, TRUE, array(32, 32));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of available profiles.
|
||||
*/
|
||||
function backup_migrate_drush_profiles() {
|
||||
backup_migrate_include('profiles');
|
||||
$rows = array(array(dt('ID'), dt('Name')));
|
||||
foreach (backup_migrate_get_profiles() as $profile) {
|
||||
$rows[] = array(
|
||||
$profile->get_id(),
|
||||
$profile->get_name(),
|
||||
);
|
||||
}
|
||||
drush_print_table($rows, TRUE, array(32, 32));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of files in a given destination
|
||||
*/
|
||||
function backup_migrate_drush_destination_files($destination_id = NULL) {
|
||||
backup_migrate_include('destinations');
|
||||
$destinations = array();
|
||||
|
||||
// Set the message mode to drush output.
|
||||
_backup_migrate_message_callback('_backup_migrate_message_drush');
|
||||
if ($destination_id && !$destination = backup_migrate_get_destination($destination_id)) {
|
||||
_backup_migrate_message("Could not find the destination '@destination'. Try using 'drush bam-destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
// Single destination required.
|
||||
if ($destination) {
|
||||
$destinations = array($destination);
|
||||
}
|
||||
// List all destinations
|
||||
else {
|
||||
$destinations = backup_migrate_get_destinations('list files');
|
||||
}
|
||||
|
||||
// Load all the files.
|
||||
$rows = $sort = array();
|
||||
foreach ($destinations as $destination) {
|
||||
$destination->file_cache_clear();
|
||||
$dest_files = $destination->list_files();
|
||||
foreach ($dest_files as $id => $file) {
|
||||
$info = $file->info();
|
||||
$rows[] = array(
|
||||
check_plain($info['filename']),
|
||||
$destination->get_id(),
|
||||
format_date($info['filetime'], 'small'),
|
||||
format_interval(time() - $info['filetime'], 1),
|
||||
format_size($info['filesize']),
|
||||
);
|
||||
$sort[] = $info['filetime'];
|
||||
}
|
||||
}
|
||||
|
||||
$headers = array(array(
|
||||
dt('Filename'),
|
||||
dt('Destination'),
|
||||
dt('Date'),
|
||||
dt('Age'),
|
||||
dt('Size'),
|
||||
));
|
||||
|
||||
if (count($rows)) {
|
||||
array_multisort($sort, SORT_DESC, $rows);
|
||||
drush_print_table(array_merge($headers, $rows), TRUE);
|
||||
}
|
||||
else {
|
||||
drush_print(dt('There are no backup files to display.'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to the drush log.
|
||||
*/
|
||||
function _backup_migrate_message_drush($message, $replace, $type) {
|
||||
// If this is an error use drush_set_error to notify the end user and set the exit status
|
||||
if ($type == 'error') {
|
||||
drush_set_error(strip_tags(dt($message, $replace)));
|
||||
}
|
||||
else {
|
||||
// Use drush_log to display to the user.
|
||||
drush_log(strip_tags(dt($message, $replace)), str_replace('status', 'notice', $type));
|
||||
}
|
||||
// Watchdog log the message as well for admins.
|
||||
_backup_migrate_message_log($message, $replace, $type);
|
||||
}
|
988
sites/all/modules/backup_migrate/includes/crud.inc
Normal file
988
sites/all/modules/backup_migrate/includes/crud.inc
Normal file
|
@ -0,0 +1,988 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* CRUD functions for backup and migrate types (schedules, profiles etc.).
|
||||
*/
|
||||
|
||||
define('BACKUP_MIGRATE_STORAGE_NONE', 0);
|
||||
define('BACKUP_MIGRATE_STORAGE_DB', 1);
|
||||
define('BACKUP_MIGRATE_STORAGE_OVERRIDEN', 2);
|
||||
|
||||
/**
|
||||
* Return a list of CRUD types in the module.
|
||||
*/
|
||||
function backup_migrate_crud_types() {
|
||||
$out = array(
|
||||
'schedule' => array(
|
||||
'class' => 'backup_migrate_schedule',
|
||||
'include' => 'schedules',
|
||||
),
|
||||
'source' => array(
|
||||
'class' => 'backup_migrate_source',
|
||||
'include' => 'sources',
|
||||
),
|
||||
'destination' => array(
|
||||
'class' => 'backup_migrate_destination',
|
||||
'include' => 'destinations',
|
||||
),
|
||||
'profile' => array(
|
||||
'class' => 'backup_migrate_profile',
|
||||
'include' => 'profiles',
|
||||
),
|
||||
);
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the info for a particular crud type.
|
||||
*/
|
||||
function backup_migrate_crud_type_info($type) {
|
||||
$types = backup_migrate_crud_types();
|
||||
if (isset($types[$type])) {
|
||||
return $types[$type];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of avaiable classes of each crud type.
|
||||
*/
|
||||
function backup_migrate_crud_subtypes($type) {
|
||||
$out = array();
|
||||
if ($info = backup_migrate_crud_type_info($type)) {
|
||||
// Include the function that contains the type base.
|
||||
if (!empty($info['include'])) {
|
||||
backup_migrate_include($info['include']);
|
||||
}
|
||||
|
||||
// Allow modules (including this one) to declare backup and migrate subtypes.
|
||||
// We don't use module_invoke_all so we can avoid the side-effects of array_merge_recursive.
|
||||
$out = array();
|
||||
foreach (module_implements('backup_migrate_' . $type . '_subtypes') as $module) {
|
||||
$function = $module . '_backup_migrate_' . $type . '_subtypes';
|
||||
$result = $function();
|
||||
if (isset($result) && is_array($result)) {
|
||||
foreach ($result as $key => $val) {
|
||||
$out[$key] = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the info for a particular crud subtype.
|
||||
*/
|
||||
function backup_migrate_crud_subtype_info($type, $subtype) {
|
||||
$types = backup_migrate_crud_subtypes($type);
|
||||
if (isset($types[$subtype])) {
|
||||
return $types[$subtype];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a generic object of the given type to be used for static-like functions.
|
||||
*
|
||||
* I'm not using actual static method calls since they don't work on variables prior to PHP 5.3.0
|
||||
*/
|
||||
function backup_migrate_crud_type_load($type, $subtype = NULL) {
|
||||
$out = $info = NULL;
|
||||
|
||||
if ($subtype) {
|
||||
$info = backup_migrate_crud_subtype_info($type, $subtype);
|
||||
}
|
||||
else {
|
||||
$info = backup_migrate_crud_type_info($type);
|
||||
}
|
||||
|
||||
if (!empty($info)) {
|
||||
if (!empty($info['include'])) {
|
||||
backup_migrate_include($info['include']);
|
||||
}
|
||||
if (!empty($info['file'])) {
|
||||
include_once './'. (isset($info['path']) ? $info['path'] : '') . $info['file'];
|
||||
}
|
||||
|
||||
if (class_exists($info['class'])) {
|
||||
$out = new $info['class'];
|
||||
$out = $out->create(array('subtype' => $subtype));
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Page callback to create a new item.
|
||||
*/
|
||||
function backup_migrate_crud_create($type, $subtype = NULL) {
|
||||
if ($item = backup_migrate_crud_type_load($type, $subtype)) {
|
||||
return $item;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the menu items handled by the CRUD code.
|
||||
*/
|
||||
function backup_migrate_crud_menu() {
|
||||
$items = array();
|
||||
foreach (backup_migrate_crud_types() as $type => $info) {
|
||||
$item = backup_migrate_crud_type_load($type);
|
||||
$items += (array)$item->get_menu_items();
|
||||
foreach (backup_migrate_crud_subtypes($type) as $subtype => $info) {
|
||||
$subitem = backup_migrate_crud_type_load($type, $subtype);
|
||||
$items += (array)$subitem->get_menu_items();
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Page callback to create a new item.
|
||||
*/
|
||||
function backup_migrate_crud_ui_create($type, $subtype = NULL) {
|
||||
if ($item = backup_migrate_crud_create($type, $subtype)) {
|
||||
return drupal_get_form('backup_migrate_crud_edit_form', $item);
|
||||
}
|
||||
return drupal_not_found();
|
||||
}
|
||||
|
||||
/**
|
||||
* Page callback to list all items.
|
||||
*/
|
||||
function backup_migrate_crud_ui_list($type) {
|
||||
$out = '';
|
||||
if ($type = backup_migrate_crud_type_load($type)) {
|
||||
$out = $type->get_list();
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Page callback to list all items.
|
||||
*/
|
||||
function backup_migrate_crud_ui_list_all() {
|
||||
$out = array();
|
||||
foreach (backup_migrate_crud_types() as $type => $info) {
|
||||
$type = backup_migrate_crud_type_load($type);
|
||||
$out[] = theme('backup_migrate_group', array('title' => t($type->title_plural), 'body' => $type->get_list()));
|
||||
}
|
||||
return implode('', $out);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Page callback to edit an item.
|
||||
*/
|
||||
function backup_migrate_crud_ui_edit($type, $item_id = NULL) {
|
||||
if ($type = backup_migrate_crud_type_load($type)) {
|
||||
if ($item_id && $item = $type->item($item_id)) {
|
||||
return drupal_get_form('backup_migrate_crud_edit_form', $item);
|
||||
}
|
||||
drupal_goto($type->get_settings_path());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Does a crud item with the given name exist.
|
||||
*
|
||||
* Callback for the 'machine_name' form type.
|
||||
*/
|
||||
function backup_migrate_crud_item_exists($machine_name, $element, $form_state) {
|
||||
return $form_state['values']['item']->item_exists($machine_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* A form callback to edit an item.
|
||||
*/
|
||||
function backup_migrate_crud_edit_form($form, $form_state, $item) {
|
||||
$form = $item->edit_form();
|
||||
|
||||
$form['item'] = array(
|
||||
'#type' => 'value',
|
||||
'#value' => $item,
|
||||
);
|
||||
$form['#validate'][] = 'backup_migrate_crud_edit_form_validate';
|
||||
$form['#submit'][] = 'backup_migrate_crud_edit_form_submit';
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the item edit form.
|
||||
*/
|
||||
function backup_migrate_crud_edit_form_validate($form, &$form_state) {
|
||||
$item = $form_state['values']['item'];
|
||||
$item->edit_form_validate($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the item edit form.
|
||||
*/
|
||||
function backup_migrate_crud_edit_form_submit($form, &$form_state) {
|
||||
$item = $form_state['values']['item'];
|
||||
|
||||
$item->edit_form_submit($form, $form_state);
|
||||
if (empty($form_state['redirect'])) {
|
||||
$form_state['redirect'] = $item->get_settings_path();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Page callback to delete an item.
|
||||
*/
|
||||
function backup_migrate_crud_ui_delete($type, $item_id = NULL) {
|
||||
if ($type = backup_migrate_crud_type_load($type)) {
|
||||
if ($item_id && $item = $type->item($item_id)) {
|
||||
return drupal_get_form('backup_migrate_crud_delete_confirm_form', $item);
|
||||
}
|
||||
drupal_goto($type->get_settings_path());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask confirmation for deletion of a item.
|
||||
*/
|
||||
function backup_migrate_crud_delete_confirm_form($form, &$form_state, $item) {
|
||||
$form['item'] = array(
|
||||
'#type' => 'value',
|
||||
'#value' => $item,
|
||||
);
|
||||
if ($item->storage == BACKUP_MIGRATE_STORAGE_OVERRIDEN) {
|
||||
$message = $item->revert_confirm_message();
|
||||
return confirm_form($form, t('Are you sure?'), $item->get_settings_path(), $message, t('Revert'), t('Cancel'));
|
||||
} else {
|
||||
$message = $item->delete_confirm_message();
|
||||
return confirm_form($form, t('Are you sure?'), $item->get_settings_path(), $message, t('Delete'), t('Cancel'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a item after confirmation.
|
||||
*/
|
||||
function backup_migrate_crud_delete_confirm_form_submit($form, &$form_state) {
|
||||
if ($form_state['values']['confirm']) {
|
||||
$item = $form_state['values']['item'];
|
||||
$item->delete();
|
||||
}
|
||||
$form_state['redirect'] = $item->get_settings_path();
|
||||
}
|
||||
|
||||
/**
|
||||
* Export an item.
|
||||
*/
|
||||
function backup_migrate_crud_ui_export($type, $item_id = NULL) {
|
||||
if ($type = backup_migrate_crud_type_load($type)) {
|
||||
if ($item_id && $item = $type->item($item_id)) {
|
||||
return drupal_get_form('backup_migrate_crud_export_form', $item->export());
|
||||
}
|
||||
drupal_goto($type->get_settings_path());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask confirmation for deletion of a destination.
|
||||
*/
|
||||
function backup_migrate_crud_export_form($form, &$form_state, $export) {
|
||||
$form['export'] = array(
|
||||
'#title' => t('Exported content'),
|
||||
'#type' => 'textarea',
|
||||
'#rows' => min(30, count(explode("\n", $export))),
|
||||
'#value' => $export,
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Page callback to import an item.
|
||||
*/
|
||||
function backup_migrate_crud_ui_import() {
|
||||
return drupal_get_form('backup_migrate_crud_import_form');
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask confirmation for deletion of a item.
|
||||
*/
|
||||
function backup_migrate_crud_import_form($form, &$form_state) {
|
||||
$form['code'] = array(
|
||||
'#type' => 'textarea',
|
||||
'#title' => t('Paste Exported Code Here'),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
$form['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Import'),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate handler to import a view
|
||||
*/
|
||||
function backup_migrate_crud_import_form_validate($form, &$form_state) {
|
||||
$item = backup_migrate_crud_create_from_import($form_state['values']['code']);
|
||||
if ($item) {
|
||||
$form_state['values']['item'] = $item;
|
||||
}
|
||||
else {
|
||||
form_set_error('code', t('Unable to import item.'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* import a item after confirmation.
|
||||
*/
|
||||
function backup_migrate_crud_import_form_submit($form, &$form_state) {
|
||||
$item = $form_state['values']['item'];
|
||||
$item->save();
|
||||
_backup_migrate_message('Your !type was imported', array('!type' => t($item->singular)));
|
||||
$form_state['redirect'] = $item->get_settings_path();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an object from the exported object.
|
||||
*/
|
||||
function backup_migrate_crud_create_from_import($code) {
|
||||
$item = NULL;
|
||||
$code = 'return ' . $code . ';';
|
||||
ob_start();
|
||||
$values = eval($code);
|
||||
ob_end_clean();
|
||||
|
||||
if ($values) {
|
||||
if (!empty($values['type_name']) && $type = backup_migrate_crud_type_load($values['type_name'])) {
|
||||
$item = $type->create($values);
|
||||
// Make sure the item's ID doesn't already exist.
|
||||
$item->unique_id();
|
||||
}
|
||||
}
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all items of the given type.
|
||||
*/
|
||||
function backup_migrate_crud_get_items($type) {
|
||||
if ($type = backup_migrate_crud_type_load($type)) {
|
||||
return $type->all_items();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an item of the specified type.
|
||||
*/
|
||||
function backup_migrate_crud_get_item($type, $id) {
|
||||
if ($type = backup_migrate_crud_type_load($type)) {
|
||||
return $type->item($id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new item of the given type.
|
||||
*/
|
||||
function backup_migrate_crud_create_item($type, $params) {
|
||||
if ($type = backup_migrate_crud_type_load($type)) {
|
||||
return $type->create($params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A base class for items which can be stored in the database, listed, edited, deleted etc.
|
||||
*/
|
||||
class backup_migrate_item {
|
||||
var $show_in_list = TRUE;
|
||||
var $settings_path = '/settings/';
|
||||
var $db_table = '';
|
||||
var $type_name = '';
|
||||
var $storage = FALSE;
|
||||
var $default_values = array();
|
||||
var $singular = 'item';
|
||||
var $plural = 'items';
|
||||
var $title_plural = 'Items';
|
||||
var $title_singular = 'Item';
|
||||
|
||||
/**
|
||||
* This function is not supposed to be called. It is just here to help the po extractor out.
|
||||
*/
|
||||
function strings() {
|
||||
// Help the pot extractor find these strings.
|
||||
t('item');
|
||||
t('items');
|
||||
t('Items');
|
||||
t('Item');
|
||||
|
||||
// Help the pot extractor find these strings.
|
||||
t('List !type');
|
||||
t('Create !type');
|
||||
t('Delete !type');
|
||||
t('Edit !type');
|
||||
t('Export !type');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Constructor, set the basic info pulled from the db or generated programatically.
|
||||
*/
|
||||
function __construct($params = array()) {
|
||||
$this->from_array($this->_merge_defaults((array)$params, (array)$this->get_default_values()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge parameters with the given defaults.
|
||||
*
|
||||
* Works like array_merge_recursive, but it doesn't turn scalar values into arrays.
|
||||
*/
|
||||
function _merge_defaults($params, $defaults) {
|
||||
foreach ($defaults as $key => $val) {
|
||||
if (!isset($params[$key])) {
|
||||
$params[$key] = $val;
|
||||
}
|
||||
else if (is_array($params[$key])) {
|
||||
$params[$key] = $this->_merge_defaults($params[$key], $val);
|
||||
}
|
||||
}
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default values for standard parameters.
|
||||
*/
|
||||
function get_default_values() {
|
||||
return $this->default_values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the item to the database.
|
||||
*/
|
||||
function save() {
|
||||
if (!$this->get_id()) {
|
||||
$this->unique_id();
|
||||
}
|
||||
$record = $this->to_array();
|
||||
drupal_write_record($this->db_table, $record, !empty($this->storage) ? $this->get_primary_key() : array());
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the item from the database.
|
||||
*/
|
||||
function delete() {
|
||||
$keys = (array)$this->get_machine_name_field();
|
||||
db_query('DELETE FROM {' . $this->db_table . '} WHERE ' . $keys[0] . ' = :id', array(':id' => $this->get_id()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Load an existing item from an array.
|
||||
*/
|
||||
function from_array($params) {
|
||||
foreach ($params as $key => $value) {
|
||||
if (method_exists($this, 'set_'. $key)) {
|
||||
$this->{'set_'. $key}($value);
|
||||
}
|
||||
else {
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return as an array of values.
|
||||
*/
|
||||
function to_array() {
|
||||
$out = array();
|
||||
// Return fields as specified in the schema.
|
||||
$schema = $this->get_schema();
|
||||
if (!empty($schema['fields']) && is_array($schema['fields'])) {
|
||||
foreach ($schema['fields'] as $field => $info) {
|
||||
$out[$field] = $this->get($field);
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return as an exported array of values.
|
||||
*/
|
||||
function export() {
|
||||
$out = $this->to_array();
|
||||
$out['type_name'] = $this->type_name;
|
||||
|
||||
ob_start();
|
||||
var_export($out);
|
||||
$out = ob_get_contents();
|
||||
ob_end_clean();
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load an existing item from an database (serialized) array.
|
||||
*/
|
||||
function load_row($data) {
|
||||
$params = array();
|
||||
$schema = $this->get_schema();
|
||||
// Load fields as specified in the schema.
|
||||
foreach ($schema['fields'] as $field => $info) {
|
||||
$params[$field] = empty($info['serialize']) ? $data[$field] : unserialize($data[$field]);
|
||||
}
|
||||
$this->from_array($params);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decode a loaded db row (unserialize necessary fields).
|
||||
*/
|
||||
function decode_db_row($data) {
|
||||
$params = array();
|
||||
$schema = $this->get_schema();
|
||||
// Load fields as specified in the schema.
|
||||
foreach ($schema['fields'] as $field => $info) {
|
||||
$params[$field] = empty($info['serialize']) ? $data[$field] : unserialize($data[$field]);
|
||||
}
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the fields which must be serialized before saving to the db.
|
||||
*/
|
||||
function get_serialized_fields() {
|
||||
$out = array();
|
||||
$schema = $this->get_schema();
|
||||
foreach ($schema['fields'] as $field => $info) {
|
||||
if (!empty($info['serialize'])) {
|
||||
$out[] = $field;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the primary key field title from the schema.
|
||||
*/
|
||||
function get_primary_key() {
|
||||
$schema = $this->get_schema();
|
||||
return @$schema['primary key'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the machine name field name from the schema.
|
||||
*/
|
||||
function get_machine_name_field() {
|
||||
$schema = $this->get_schema();
|
||||
if (isset($schema['export']['key'])) {
|
||||
return $schema['export']['key'];
|
||||
}
|
||||
return @$schema['primary key'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the schema for the item type.
|
||||
*/
|
||||
function get_schema() {
|
||||
return drupal_get_schema($this->db_table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the primary id for this item (if any is set).
|
||||
*
|
||||
* We only handle single field keys since that's all we need.
|
||||
*/
|
||||
function get_id() {
|
||||
$keys = (array)$this->get_machine_name_field();
|
||||
return !empty($keys[0]) && !empty($this->{$keys[0]}) ? (string)$this->{$keys[0]} : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the primary id for this item (if any is set).
|
||||
*/
|
||||
function set_id($id) {
|
||||
$keys = (array)$this->get_machine_name_field();
|
||||
if (!empty($keys[0])) {
|
||||
return $this->{$keys[0]} = $id;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a random (very very likely unique) string id for a new item.
|
||||
*/
|
||||
function generate_id() {
|
||||
$id = md5(uniqid(mt_rand(), true));
|
||||
|
||||
// Find the shortest possible unique id from (min 4 chars).
|
||||
for ($i = 4; $i < 32; $i++) {
|
||||
$new_id = substr($id, 0, $i);
|
||||
if (!$this->item($new_id)) {
|
||||
return $new_id;
|
||||
}
|
||||
}
|
||||
// If we get here, then all 28 increasingly complex ids were already taken so we'll try again.
|
||||
// this could theoretially lead to an infinite loop, but the odds are incredibly low.
|
||||
return $this->generate_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure this item has a unique id. Should only be called for new items or the item will collide with itself.
|
||||
*/
|
||||
function unique_id() {
|
||||
$id = $this->get_id();
|
||||
|
||||
// Unset the autoincrement field so it can be regenerated.
|
||||
foreach ((array)$this->get_primary_key() as $key) {
|
||||
$this->{$key} = NULL;
|
||||
}
|
||||
|
||||
// If the item doesn't have an ID or if it's id is already taken, generate random one.
|
||||
if (!$id || $this->item($id)) {
|
||||
$this->set_id($this->generate_id());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the item.
|
||||
*/
|
||||
function get_name() {
|
||||
return @$this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the member with the given key.
|
||||
*/
|
||||
function get($key) {
|
||||
if (method_exists($this, 'get_'. $key)) {
|
||||
return $this->{'get_'. $key}();
|
||||
}
|
||||
return @$this->{$key};
|
||||
}
|
||||
|
||||
/* UI Stuff */
|
||||
|
||||
/**
|
||||
* Get the action links for a destination.
|
||||
*/
|
||||
function get_action_links() {
|
||||
$out = array();
|
||||
|
||||
$item_id = $this->get_id();
|
||||
|
||||
$path = $this->get_settings_path();
|
||||
|
||||
if (@$this->storage == BACKUP_MIGRATE_STORAGE_DB || @$this->storage == BACKUP_MIGRATE_STORAGE_OVERRIDEN) {
|
||||
$out['edit'] = l(t("edit"), $path . "/edit/$item_id");
|
||||
}
|
||||
else if (@$this->storage == BACKUP_MIGRATE_STORAGE_NONE) {
|
||||
$out['edit'] = l(t("override"), $path . "/edit/$item_id");
|
||||
}
|
||||
if (@$this->storage == BACKUP_MIGRATE_STORAGE_DB) {
|
||||
$out['delete'] = l(t("delete"), $path . "/delete/$item_id");
|
||||
}
|
||||
else if (@$this->storage == BACKUP_MIGRATE_STORAGE_OVERRIDEN) {
|
||||
$out['delete'] = l(t("revert"), $path . "/delete/$item_id");
|
||||
}
|
||||
$out['export'] = l(t("export"), $path . "/export/$item_id");
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a table of all items of this type.
|
||||
*/
|
||||
function get_list() {
|
||||
$items = $this->all_items();
|
||||
$rows = array();
|
||||
foreach ((array)$items as $item) {
|
||||
if ($item->show_in_list()) {
|
||||
if ($row = $item->get_list_row()) {
|
||||
$rows[] = $row;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count($rows)) {
|
||||
$out = theme('table', array('header' => $this->get_list_header(), 'rows' => $rows));
|
||||
}
|
||||
else {
|
||||
$out = t('There are no !items to display.', array('!items' => $this->plural));
|
||||
}
|
||||
if (user_access('administer backup and migrate')) {
|
||||
$out .= ' '. l(t('Create a new !item', array('!item' => $this->singular)), $this->get_settings_path() .'/add');
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the columns needed to list the type.
|
||||
*/
|
||||
function show_in_list() {
|
||||
return $this->show_in_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the columns needed to list the type.
|
||||
*/
|
||||
function get_settings_path() {
|
||||
return BACKUP_MIGRATE_MENU_PATH . $this->settings_path . $this->type_name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the columns needed to list the type.
|
||||
*/
|
||||
function get_list_column_info() {
|
||||
return array(
|
||||
'actions' => array('title' => t('Operations'), 'html' => TRUE),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get header for a lost of this type.
|
||||
*/
|
||||
function get_list_header() {
|
||||
$out = array();
|
||||
foreach ($this->get_list_column_info() as $key => $col) {
|
||||
$out[] = $col['title'];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a row of data to be used in a list of items of this type.
|
||||
*/
|
||||
function get_list_row() {
|
||||
$out = array();
|
||||
foreach ($this->get_list_column_info() as $key => $col) {
|
||||
$out[$key] = empty($col['html']) ? check_plain($this->get($key)) : $this->get($key);
|
||||
if (isset($col['class'])) {
|
||||
$out[$key] = array('data' => $out[$key], 'class' => $col['class']);
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the rendered action links for a destination.
|
||||
*/
|
||||
function get_actions() {
|
||||
$links = $this->get_action_links();
|
||||
return implode(" ", $links);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the edit form for the item.
|
||||
*/
|
||||
function edit_form() {
|
||||
$form = array();
|
||||
$form['item'] = array(
|
||||
'#type' => 'value',
|
||||
'#value' => $this,
|
||||
);
|
||||
$name = $this->get('name');
|
||||
$form['name'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("!type name", array('!type' => $this->title_singular)),
|
||||
"#default_value" => empty($name) ? t('Untitled !type', array('!type' => $this->title_singular)) : $name,
|
||||
"#required" => TRUE,
|
||||
);
|
||||
$form['id'] = array(
|
||||
'#type' => 'value',
|
||||
'#value' => $this->get_id(),
|
||||
);
|
||||
|
||||
$form['machine_name'] = array(
|
||||
'#type' => 'machine_name',
|
||||
'#default_value' => $this->get_id(),
|
||||
'#maxlength' => 255,
|
||||
'#machine_name' => array(
|
||||
'source' => array('name'),
|
||||
'exists' => 'backup_migrate_crud_item_exists',
|
||||
),
|
||||
);
|
||||
|
||||
$form['actions'] = array('#prefix' => '<div class="container-inline">', '#suffix' => '</div>', '#weight' => 99);
|
||||
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save !type', array('!type' => t($this->singular))));
|
||||
$form['actions']['cancel'] = array('#value' => l(t('Cancel'), $this->get_settings_path()));
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the edit form for the item.
|
||||
*/
|
||||
function edit_form_validate($form, &$form_state) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the edit form for the item.
|
||||
*/
|
||||
function edit_form_submit($form, &$form_state) {
|
||||
$this->from_array($form_state['values']);
|
||||
$this->save();
|
||||
_backup_migrate_message('Your !type was saved', array('!type' => t($this->singular)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message to send to the user when confirming the deletion of the item.
|
||||
*/
|
||||
function delete_confirm_message() {
|
||||
return t('Are you sure you want to delete the !type %name?', array('!type' => t($this->singular), '%name' => $this->get('name')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message to send to the user when confirming the deletion of the item.
|
||||
*/
|
||||
function revert_confirm_message() {
|
||||
return t('Are you sure you want to revert the !type %name back to the default settings?', array('!type' => t($this->singular), '%name' => $this->get('name')));
|
||||
}
|
||||
|
||||
/* Static Functions */
|
||||
/**
|
||||
* Get the menu items for manipulating this type.
|
||||
*/
|
||||
function get_menu_items() {
|
||||
$path = $this->get_settings_path();
|
||||
|
||||
$type = $this->type_name;
|
||||
$items[$path] = array(
|
||||
'title' => $this->title_plural,
|
||||
'page callback' => 'backup_migrate_menu_callback',
|
||||
'page arguments' => array('crud', 'backup_migrate_crud_ui_list', TRUE, $this->type_name),
|
||||
'access arguments' => array('administer backup and migrate'),
|
||||
'weight' => 2,
|
||||
'type' => MENU_LOCAL_TASK,
|
||||
);
|
||||
$items[$path .'/list'] = array(
|
||||
'title' => 'List !type',
|
||||
'title arguments' => array('!type' => t($this->title_plural)),
|
||||
'weight' => 1,
|
||||
'type' => MENU_DEFAULT_LOCAL_TASK,
|
||||
);
|
||||
$items[$path .'/add'] = array(
|
||||
'title' => 'Add !type',
|
||||
'title arguments' => array('!type' => t($this->title_singular)),
|
||||
'page callback' => 'backup_migrate_menu_callback',
|
||||
'page arguments' => array('crud', 'backup_migrate_crud_ui_create', TRUE, $this->type_name),
|
||||
'access arguments' => array('administer backup and migrate'),
|
||||
'weight' => 2,
|
||||
'type' => MENU_LOCAL_ACTION,
|
||||
);
|
||||
$items[$path .'/delete'] = array(
|
||||
'title' => 'Delete !type',
|
||||
'title arguments' => array('!type' => t($this->title_singular)),
|
||||
'page callback' => 'backup_migrate_menu_callback',
|
||||
'page arguments' => array('crud', 'backup_migrate_crud_ui_delete', TRUE, $this->type_name),
|
||||
'access arguments' => array('administer backup and migrate'),
|
||||
'type' => MENU_CALLBACK,
|
||||
);
|
||||
$items[$path .'/edit'] = array(
|
||||
'title' => 'Edit !type',
|
||||
'title arguments' => array('!type' => t($this->title_singular)),
|
||||
'page callback' => 'backup_migrate_menu_callback',
|
||||
'page arguments' => array('crud', 'backup_migrate_crud_ui_edit', TRUE, $this->type_name),
|
||||
'access arguments' => array('administer backup and migrate'),
|
||||
'type' => MENU_CALLBACK,
|
||||
);
|
||||
$items[$path .'/export'] = array(
|
||||
'title' => 'Export !type',
|
||||
'title arguments' => array('!type' => t($this->title_singular)),
|
||||
'page callback' => 'backup_migrate_menu_callback',
|
||||
'page arguments' => array('crud', 'backup_migrate_crud_ui_export', TRUE, $this->type_name),
|
||||
'access arguments' => array('administer backup and migrate'),
|
||||
'type' => MENU_CALLBACK,
|
||||
);
|
||||
return $items;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new items with the given input. Doesn't load the parameters, but could use them to determine what type to create.
|
||||
*/
|
||||
function create($params = array()) {
|
||||
$type = get_class($this);
|
||||
return new $type($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the given items.
|
||||
*/
|
||||
function all_items() {
|
||||
$items = array();
|
||||
|
||||
// Get any items stored as a variable. This allows destinations to be defined in settings.php
|
||||
$defaults = (array)variable_get($this->db_table . '_defaults', array());
|
||||
foreach ($defaults as $info) {
|
||||
if (is_array($info) && $item = $this->create($info)) {
|
||||
$items[$item->get_id()] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
// Get the items from the db.
|
||||
$result = db_query("SELECT * FROM {{$this->db_table}}", array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
foreach ($result as $info) {
|
||||
$info = $this->decode_db_row($info);
|
||||
if ($item = $this->create($info)) {
|
||||
$item->storage = empty($items[$item->get_id()]) ? BACKUP_MIGRATE_STORAGE_DB : BACKUP_MIGRATE_STORAGE_OVERRIDEN;
|
||||
$items[$item->get_id()] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
// Allow other modules to declare destinations programatically.
|
||||
$default_items = module_invoke_all($this->db_table);
|
||||
|
||||
// Get CTools exported versions.
|
||||
if (function_exists('ctools_include')) {
|
||||
ctools_include('export');
|
||||
$defaults = ctools_export_load_object($this->db_table);
|
||||
foreach ($defaults as $info) {
|
||||
$info = (array)$info;
|
||||
if (!empty($info) && $item = $this->create($info)) {
|
||||
$default_items[$item->get_id()] = $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get any items stored as a variable again to correctly mark overrides.
|
||||
$defaults = (array)variable_get($this->db_table . '_defaults', array());
|
||||
foreach ($defaults as $info) {
|
||||
if (is_array($info) && $item = $this->create($info)) {
|
||||
$default_items[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
// Add the default items to the array or set the storage flag if they've already been overridden.
|
||||
foreach ($default_items as $item) {
|
||||
if (isset($items[$item->get_id()])) {
|
||||
$items[$item->get_id()]->storage = BACKUP_MIGRATE_STORAGE_OVERRIDEN;
|
||||
}
|
||||
else {
|
||||
$item->storage = BACKUP_MIGRATE_STORAGE_NONE;
|
||||
$items[$item->get_id()] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
// Allow other modules to alter the items. This should maybe be before the db override code above
|
||||
// but then the filters are not able to set defaults for missing values. Other modules should just
|
||||
// be careful not to overwrite the user's UI changes in an unexpected way.
|
||||
drupal_alter($this->db_table, $items);
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* A particular item.
|
||||
*/
|
||||
function item($item_id) {
|
||||
$items = $this->all_items();
|
||||
return !empty($items[$item_id]) ? $items[$item_id] : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* A particular item.
|
||||
*/
|
||||
function item_exists($item_id) {
|
||||
$items = $this->all_items();
|
||||
return !empty($items[$item_id]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Functions to handle the browser upload/download backup destination.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A destination type for browser upload/download.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
class backup_migrate_destination_browser extends backup_migrate_destination {
|
||||
/**
|
||||
* Get a row of data to be used in a list of items of this type.
|
||||
*/
|
||||
function get_list_row() {
|
||||
// Return none as this type should not be displayed.
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A destination type for browser upload.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
class backup_migrate_destination_browser_upload extends backup_migrate_destination_browser {
|
||||
var $supported_ops = array('restore');
|
||||
function __construct() {
|
||||
$params = array();
|
||||
$params['name'] = "Upload";
|
||||
$params['machine_name'] = 'upload';
|
||||
parent::__construct($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* File load destination callback.
|
||||
*/
|
||||
function load_file($file_id) {
|
||||
if ($file = file_save_upload('backup_migrate_restore_upload')) {
|
||||
$out = new backup_file(array('filepath' => $file->uri));
|
||||
backup_migrate_temp_files_add($file->uri);
|
||||
return $out;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A destination type for browser download.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
class backup_migrate_destination_browser_download extends backup_migrate_destination_browser {
|
||||
var $supported_ops = array('manual backup');
|
||||
// Browser downloads must always be the last destination as they must end the current process when they are done.
|
||||
var $weight = 1000;
|
||||
|
||||
function __construct() {
|
||||
$params = array();
|
||||
$params['name'] = "Download";
|
||||
$params['machine_name'] = 'download';
|
||||
parent::__construct($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* File save destination callback.
|
||||
*/
|
||||
function save_file($file, $settings) {
|
||||
backup_migrate_include('files');
|
||||
$file->transfer();
|
||||
}
|
||||
}
|
||||
|
318
sites/all/modules/backup_migrate/includes/destinations.db.inc
Normal file
318
sites/all/modules/backup_migrate/includes/destinations.db.inc
Normal file
|
@ -0,0 +1,318 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Functions to handle the direct to database destination.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A destination type for saving to a database server.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
class backup_migrate_destination_db extends backup_migrate_destination_remote {
|
||||
var $supported_ops = array('scheduled backup', 'manual backup', 'configure', 'source');
|
||||
var $db_target = 'default';
|
||||
var $connection = null;
|
||||
|
||||
|
||||
function type_name() {
|
||||
return t("Database");
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the info by importing it into the database.
|
||||
*/
|
||||
function save_file($file, $settings) {
|
||||
backup_migrate_include('files');
|
||||
|
||||
// Set the source_id to the destination_id in the settings since for a restore, the source_id is the
|
||||
// database that gets restored to.
|
||||
$settings->set_source($this->get_id());
|
||||
|
||||
// Restore the file to the source database.
|
||||
$file = backup_migrate_perform_restore($this->get_id(), $file, $settings);
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destination configuration callback.
|
||||
*/
|
||||
function edit_form() {
|
||||
$form = parent::edit_form();
|
||||
$form['scheme']['#title'] = t('Database type');
|
||||
// $form['scheme']['#options'] = array($GLOBALS['db_type'] => $GLOBALS['db_type']);
|
||||
$form['scheme']['#description'] = t('The type of the database. Drupal only supports one database type at a time, so this must be the same as the current database type.');
|
||||
$form['path']['#title'] = t('Database name');
|
||||
$form['path']['#description'] = t('The name of the database. The database must exist, it will not be created for you.');
|
||||
$form['user']['#description'] = t('Enter the name of a user who has write access to the database.');
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the configuration form. Make sure the db info is valid.
|
||||
*/
|
||||
function edit_form_validate($form, &$form_state) {
|
||||
if (!preg_match('/[a-zA-Z0-9_\$]+/', $form_state['values']['path'])) {
|
||||
form_set_error('path', t('The database name is not valid.'));
|
||||
}
|
||||
parent::edit_form_validate($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default settings for this object.
|
||||
*
|
||||
* @return array
|
||||
* The default tables whose data can be ignored. These tables mostly
|
||||
* contain info which can be easily reproducted (such as cache or search
|
||||
* index) but also tables which can become quite bloated but are not
|
||||
* necessarily extremely important to back up or migrate during development
|
||||
* (such as access log and watchdog).
|
||||
*/
|
||||
function backup_settings_default() {
|
||||
$all_tables = $this->_get_table_names();
|
||||
|
||||
// Basic modules that should be excluded.
|
||||
$basic = array(
|
||||
// Default core tables.
|
||||
'accesslog',
|
||||
'sessions',
|
||||
'watchdog',
|
||||
// Search module.
|
||||
'search_dataset',
|
||||
'search_index',
|
||||
'search_keywords_log',
|
||||
'search_total',
|
||||
// Devel module.
|
||||
'devel_queries',
|
||||
'devel_times',
|
||||
);
|
||||
|
||||
// Identify all cache tables.
|
||||
$cache = array('cache');
|
||||
foreach ($all_tables as $table_name) {
|
||||
if (strpos($table_name, 'cache_') === 0) {
|
||||
$cache[] = $table_name;
|
||||
}
|
||||
}
|
||||
|
||||
// Simpletest can create a lot of tables that do not need to be backed up,
|
||||
// but all of them start with the string 'simpletest' so they can be easily
|
||||
// excluded.
|
||||
$simpletest = array();
|
||||
foreach ($all_tables as $table_name) {
|
||||
if (strpos($table_name, 'simpletest') === 0) {
|
||||
$simpletest[] = $table_name;
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'nodata_tables' => array_merge($basic, $cache, module_invoke_all('devel_caches')),
|
||||
'exclude_tables' => $simpletest,
|
||||
'utils_lock_tables' => FALSE,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the backup settings for this destination.
|
||||
*/
|
||||
function backup_settings_form($settings) {
|
||||
$objects = $this->get_object_names();
|
||||
$form['#description'] = t("You may omit specific tables, or specific table data from the backup file. Only omit data that you know you will not need such as cache data, or tables from other applications. Excluding tables can break your Drupal install, so <strong>do not change these settings unless you know what you're doing</strong>.");
|
||||
$form['exclude_tables'] = array(
|
||||
"#type" => "select",
|
||||
"#multiple" => TRUE,
|
||||
"#title" => t("Exclude the following tables altogether"),
|
||||
"#options" => $objects,
|
||||
"#default_value" => $settings['exclude_tables'],
|
||||
"#description" => t("The selected tables will not be added to the backup file."),
|
||||
);
|
||||
$tables = $this->get_table_names();
|
||||
$form['nodata_tables'] = array(
|
||||
"#type" => "select",
|
||||
"#multiple" => TRUE,
|
||||
"#title" => t("Exclude the data from the following tables"),
|
||||
"#options" => $tables,
|
||||
"#default_value" => $settings['nodata_tables'],
|
||||
"#description" => t("The selected tables will have their structure backed up but not their contents. This is useful for excluding cache data to reduce file size."),
|
||||
);
|
||||
$form['utils_lock_tables'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Lock tables during backup'),
|
||||
'#default_value' => !empty($settings['utils_lock_tables']) ? $settings['utils_lock_tables'] : NULL,
|
||||
'#description' => t('This can help reduce data corruption, but will make your site unresponsive.'),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup from this source.
|
||||
*/
|
||||
function backup_to_file($file, $settings) {
|
||||
$file->push_type($this->get_file_type_id());
|
||||
|
||||
backup_migrate_filters_invoke_all('pre_backup', $this, $file, $settings);
|
||||
//$this->lock_tables($settings);
|
||||
|
||||
// Switch to a different db if specified.
|
||||
$success = $this->_backup_db_to_file($file, $settings);
|
||||
|
||||
//$this->unlock_tables($settings);
|
||||
backup_migrate_filters_invoke_all('post_backup', $this, $file, $settings, $success);
|
||||
|
||||
return $success ? $file : FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore to this source.
|
||||
*/
|
||||
function restore_from_file($file, &$settings) {
|
||||
$num = 0;
|
||||
$type = $this->get_file_type_id();
|
||||
// Open the file using the file wrapper. Check that the dump is of the right type (allow .sql for legacy reasons).
|
||||
if ($file->type_id() !== $this->get_file_type_id() && $file->type_id() !== 'sql') {
|
||||
_backup_migrate_message("Unable to restore from file %file because a %type file can't be restored to this database.", array("%file" => $file->filepath(), '%type' => $file->type_id()), 'error');
|
||||
}
|
||||
else {
|
||||
backup_migrate_filters_invoke_all('pre_restore', $file, $settings);
|
||||
|
||||
// Restore the database.
|
||||
$num = $this->_restore_db_from_file($file, $settings);
|
||||
$settings->performed_action = $num ? t('%num SQL commands executed.', array('%num' => $num)) : '';
|
||||
|
||||
backup_migrate_filters_invoke_all('post_restore', $file, $settings, $num);
|
||||
}
|
||||
return $num;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the db connection for the specified db.
|
||||
*/
|
||||
function _get_db_connection() {
|
||||
if (!$this->connection) {
|
||||
$target = $key = '';
|
||||
$parts = explode(':', $this->get_id());
|
||||
// One of the predefined databases (set in settings.php)
|
||||
if ($parts[0] == 'db') {
|
||||
$key = empty($parts[1]) ? 'default' : $parts[1];
|
||||
$target = empty($parts[2]) ? 'default' : $parts[2];
|
||||
}
|
||||
// Another db url.
|
||||
else {
|
||||
// If the url is specified build it into a connection info array.
|
||||
if (!empty($this->dest_url)) {
|
||||
$info = array(
|
||||
'driver' => empty($this->dest_url['scheme']) ? NULL : $this->dest_url['scheme'],
|
||||
'host' => empty($this->dest_url['host']) ? NULL : $this->dest_url['host'],
|
||||
'port' => empty($this->dest_url['port']) ? NULL : $this->dest_url['port'],
|
||||
'username' => empty($this->dest_url['user']) ? NULL : $this->dest_url['user'],
|
||||
'password' => empty($this->dest_url['pass']) ? NULL : $this->dest_url['pass'],
|
||||
'database' => empty($this->dest_url['path']) ? NULL : $this->dest_url['path'],
|
||||
);
|
||||
$key = uniqid('backup_migrate_tmp_');
|
||||
$target = 'default';
|
||||
Database::addConnectionInfo($key, $target, $info);
|
||||
}
|
||||
// No database selected. Assume the default.
|
||||
else {
|
||||
$key = $target = 'default';
|
||||
}
|
||||
}
|
||||
if ($target && $key) {
|
||||
$this->connection = Database::getConnection($target, $key);
|
||||
}
|
||||
}
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup the databases to a file.
|
||||
*/
|
||||
function _backup_db_to_file($file, $settings) {
|
||||
// Must be overridden.
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup the databases to a file.
|
||||
*/
|
||||
function _restore_db_from_file($file, $settings) {
|
||||
// Must be overridden.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of objects in the database.
|
||||
*/
|
||||
function get_object_names() {
|
||||
// Must be overridden.
|
||||
$out = $this->_get_table_names();
|
||||
if (method_exists($this, '_get_view_names')) {
|
||||
$out += $this->_get_view_names();
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of tables in the database.
|
||||
*/
|
||||
function get_table_names() {
|
||||
// Must be overridden.
|
||||
$out = $this->_get_table_names();
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of tables in the database.
|
||||
*/
|
||||
function _get_table_names() {
|
||||
// Must be overridden.
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock the database in anticipation of a backup.
|
||||
*/
|
||||
function lock_tables($settings) {
|
||||
if ($settings->filters['utils_lock_tables']) {
|
||||
$tables = array();
|
||||
foreach ($this->get_table_names() as $table) {
|
||||
// There's no need to lock excluded or structure only tables because it doesn't matter if they change.
|
||||
if (empty($settings->filters['exclude_tables']) || !in_array($table, (array)$settings->filters['exclude_tables'])) {
|
||||
$tables[] = $table;
|
||||
}
|
||||
}
|
||||
$this->_lock_tables($tables);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock the list of given tables in the database.
|
||||
*/
|
||||
function _lock_tables($tables) {
|
||||
// Must be overridden.
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock any tables that have been locked.
|
||||
*/
|
||||
function unlock_tables($settings) {
|
||||
if ($settings->filters['utils_lock_tables']) {
|
||||
$this->_unlock_tables();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock the list of given tables in the database.
|
||||
*/
|
||||
function _unlock_tables($tables) {
|
||||
// Must be overridden.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file type for to backup this destination to.
|
||||
*/
|
||||
function get_file_type_id() {
|
||||
return 'sql';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,477 @@
|
|||
<?php
|
||||
|
||||
backup_migrate_include('destinations.db');
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Functions to handle the direct to database destination.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A destination type for saving to a database server.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
|
||||
class backup_migrate_destination_db_mysql extends backup_migrate_destination_db {
|
||||
function type_name() {
|
||||
return t("MySQL Database");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of backup filetypes.
|
||||
*/
|
||||
function file_types() {
|
||||
return array(
|
||||
"sql" => array(
|
||||
"extension" => "sql",
|
||||
"filemime" => "text/x-sql",
|
||||
"backup" => TRUE,
|
||||
"restore" => TRUE,
|
||||
),
|
||||
"mysql" => array(
|
||||
"extension" => "mysql",
|
||||
"filemime" => "text/x-sql",
|
||||
"backup" => TRUE,
|
||||
"restore" => TRUE,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare any mysql databases defined in the settings.php file as a possible destination.
|
||||
*/
|
||||
function destinations() {
|
||||
$out = array();
|
||||
global $databases;
|
||||
foreach ((array)$databases as $db_key => $target) {
|
||||
foreach ((array)$target as $tgt_key => $info) {
|
||||
// Only mysql/mysqli supported by this destination.
|
||||
$key = $db_key . ':' . $tgt_key;
|
||||
if ($info['driver'] === 'mysql') {
|
||||
$url = $info['driver'] . '://' . $info['username'] . ':' . $info['password'] . '@' . $info['host'] . (isset($info['port']) ? ':' . $info['port'] : '') . '/' . $info['database'];
|
||||
if ($destination = backup_migrate_create_destination('mysql', array('url' => $url))) {
|
||||
// Treat the default database differently because it is probably the only one available.
|
||||
if ($key == 'default:default') {
|
||||
$destination->set_id('db');
|
||||
$destination->set_name(t('Default Database'));
|
||||
// Dissalow backing up to the default database because that's confusing and potentially dangerous.
|
||||
$destination->remove_op('scheduled backup');
|
||||
$destination->remove_op('manual backup');
|
||||
}
|
||||
else {
|
||||
$destination->set_id('db:'. $key);
|
||||
$destination->set_name($key .": ". $destination->get_display_location());
|
||||
}
|
||||
$out[$destination->get_id()] = $destination;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file type for to backup this destination to.
|
||||
*/
|
||||
function get_file_type_id() {
|
||||
return 'mysql';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the backup settings for this destination.
|
||||
*/
|
||||
function backup_settings_form($settings) {
|
||||
$form = parent::backup_settings_form($settings);
|
||||
|
||||
$form['use_mysqldump'] = array(
|
||||
"#type" => "checkbox",
|
||||
"#title" => t("Use mysqldump command"),
|
||||
"#default_value" => !empty($settings['use_mysqldump']),
|
||||
"#description" => t("Use the mysqldump command line tool if available. This can be faster for large databases but will not work on all servers. Also exporting SQL views is not really solid with this option. EXPERIMENTAL"),
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Backup the databases to a file.
|
||||
*
|
||||
* Returns a list of sql commands, one command per line.
|
||||
* That makes it easier to import without loading the whole file into memory.
|
||||
* The files are a little harder to read, but human-readability is not a priority
|
||||
*/
|
||||
function _backup_db_to_file($file, $settings) {
|
||||
if (!empty($settings->filters['use_mysqldump']) && $this->_backup_db_to_file_mysqldump($file, $settings)) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
$lines = 0;
|
||||
$exclude = !empty($settings->filters['exclude_tables']) ? $settings->filters['exclude_tables'] : array();
|
||||
$nodata = !empty($settings->filters['nodata_tables']) ? $settings->filters['nodata_tables'] : array();
|
||||
if ($file->open(TRUE)) {
|
||||
$file->write($this->_get_sql_file_header());
|
||||
$alltables = $this->_get_tables();
|
||||
$allviews = $this->_get_views();
|
||||
|
||||
foreach ($alltables as $table) {
|
||||
if (_backup_migrate_check_timeout()) {
|
||||
return FALSE;
|
||||
}
|
||||
if ($table['name'] && !isset($exclude[$table['name']])) {
|
||||
$file->write($this->_get_table_structure_sql($table));
|
||||
$lines++;
|
||||
if (!in_array($table['name'], $nodata)) {
|
||||
$lines += $this->_dump_table_data_sql_to_file($file, $table);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($allviews as $view) {
|
||||
if (_backup_migrate_check_timeout()) {
|
||||
return FALSE;
|
||||
}
|
||||
if ($view['name'] && !isset($exclude[$view['name']])) {
|
||||
$file->write($this->_get_view_create_sql($view));
|
||||
}
|
||||
|
||||
}
|
||||
$file->write($this->_get_sql_file_footer());
|
||||
$file->close();
|
||||
return $lines;
|
||||
}
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Backup the databases to a file using the mysqldump command.
|
||||
*/
|
||||
function _backup_db_to_file_mysqldump($file, $settings) {
|
||||
$success = FALSE;
|
||||
$nodata_tables = array();
|
||||
$alltables = $this->_get_tables();
|
||||
|
||||
|
||||
$command = 'mysqldump --result-file=%file --opt -Q --host=%host --port=%port --user=%user --password=%pass %db';
|
||||
$args = array(
|
||||
'%file' => $file->filepath(),
|
||||
'%host' => $this->dest_url['host'],
|
||||
'%port' => !empty($this->dest_url['port']) ? $this->dest_url['port'] : '3306',
|
||||
'%user' => $this->dest_url['user'],
|
||||
'%pass' => $this->dest_url['pass'],
|
||||
'%db' => $this->dest_url['path'],
|
||||
);
|
||||
|
||||
// Ignore the excluded and no-data tables.
|
||||
$db = $this->dest_url['path'];
|
||||
if (!empty($settings->filters['exclude_tables'])) {
|
||||
foreach ((array)$settings->filters['exclude_tables'] as $table) {
|
||||
if (isset($alltables[$table])) {
|
||||
$command .= ' --ignore-table='. $db .'.'. $table;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!empty($settings->filters['nodata_tables'])) {
|
||||
foreach ((array)$settings->filters['nodata_tables'] as $table) {
|
||||
if (isset($alltables[$table])) {
|
||||
$nodata_tables[] = $table;
|
||||
$command .= ' --ignore-table='. $db .'.'. $table;
|
||||
}
|
||||
}
|
||||
}
|
||||
$success = backup_migrate_exec($command, $args);
|
||||
|
||||
// Get the nodata tables.
|
||||
if ($success && !empty($nodata_tables)) {
|
||||
$tables = implode(' ', array_unique($nodata_tables));
|
||||
$command = "mysqldump --no-data --opt -Q --host=%host --port=%port --user=%user --password=%pass %db $tables >> %file";
|
||||
$success = backup_migrate_exec($command, $args);
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup the databases to a file.
|
||||
*/
|
||||
function _restore_db_from_file($file, $settings) {
|
||||
$num = 0;
|
||||
|
||||
if ($file->open() && $conn = $this->_get_db_connection()) {
|
||||
// Read one line at a time and run the query.
|
||||
while ($line = $this->_read_sql_command_from_file($file)) {
|
||||
if (_backup_migrate_check_timeout()) {
|
||||
return FALSE;
|
||||
}
|
||||
if ($line) {
|
||||
// Prepeare and exexute the statement instead of the api function to avoid substitution of '{' etc.
|
||||
$stmt = $conn->prepare($line);
|
||||
$stmt->execute();
|
||||
$num++;
|
||||
}
|
||||
}
|
||||
// Close the file with fclose/gzclose.
|
||||
$file->close();
|
||||
}
|
||||
else {
|
||||
drupal_set_message(t("Unable to open file %file to restore database", array("%file" => $file->filepath())), 'error');
|
||||
$num = FALSE;
|
||||
}
|
||||
return $num;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read a multiline sql command from a file.
|
||||
*
|
||||
* Supports the formatting created by mysqldump, but won't handle multiline comments.
|
||||
*/
|
||||
function _read_sql_command_from_file($file) {
|
||||
$out = '';
|
||||
while ($line = $file->read()) {
|
||||
$first2 = substr($line, 0, 2);
|
||||
$first3 = substr($line, 0, 3);
|
||||
|
||||
// Ignore single line comments. This function doesn't support multiline comments or inline comments.
|
||||
if ($first2 != '--' && ($first2 != '/*' || $first3 == '/*!')) {
|
||||
$out .= ' ' . trim($line);
|
||||
// If a line ends in ; or */ it is a sql command.
|
||||
if (substr($out, strlen($out) - 1, 1) == ';') {
|
||||
return trim($out);
|
||||
}
|
||||
}
|
||||
}
|
||||
return trim($out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of tables in the database.
|
||||
*/
|
||||
function _get_table_names() {
|
||||
$out = array();
|
||||
foreach ($this->_get_tables() as $table) {
|
||||
$out[$table['name']] = $table['name'];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of views in the database.
|
||||
*/
|
||||
function _get_view_names() {
|
||||
$out = array();
|
||||
foreach ($this->_get_views() as $view) {
|
||||
$out[$view['name']] = $view['name'];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock the list of given tables in the database.
|
||||
*/
|
||||
function _lock_tables($tables) {
|
||||
if ($tables) {
|
||||
$tables_escaped = array();
|
||||
foreach ($tables as $table) {
|
||||
$tables_escaped[] = '`'. db_escape_table($table) .'` WRITE';
|
||||
}
|
||||
$this->query('LOCK TABLES '. implode(', ', $tables_escaped));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock all tables in the database.
|
||||
*/
|
||||
function _unlock_tables($settings) {
|
||||
$this->query('UNLOCK TABLES');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of tables in the db.
|
||||
*/
|
||||
function _get_tables() {
|
||||
$out = array();
|
||||
// get auto_increment values and names of all tables
|
||||
$tables = $this->query("show table status", array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
foreach ($tables as $table) {
|
||||
// Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed.
|
||||
// See: http://drupal.org/node/1171866
|
||||
$table = array_change_key_case($table);
|
||||
if (!empty($table['engine'])) {
|
||||
$out[$table['name']] = $table;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of views in the db.
|
||||
*/
|
||||
function _get_views() {
|
||||
$out = array();
|
||||
// get auto_increment values and names of all tables
|
||||
$tables = $this->query("show table status", array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
foreach ($tables as $table) {
|
||||
// Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed.
|
||||
// See: http://drupal.org/node/1171866
|
||||
$table = array_change_key_case($table);
|
||||
if (empty($table['engine'])) {
|
||||
$out[$table['name']] = $table;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sql for the structure of the given table.
|
||||
*/
|
||||
function _get_table_structure_sql($table) {
|
||||
$out = "";
|
||||
$result = $this->query("SHOW CREATE TABLE `". $table['name'] ."`", array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
foreach ($result as $create) {
|
||||
// Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed.
|
||||
// See: http://drupal.org/node/1171866
|
||||
$create = array_change_key_case($create);
|
||||
$out .= "DROP TABLE IF EXISTS `". $table['name'] ."`;\n";
|
||||
// Remove newlines and convert " to ` because PDO seems to convert those for some reason.
|
||||
$out .= strtr($create['create table'], array("\n" => ' ', '"' => '`'));
|
||||
if ($table['auto_increment']) {
|
||||
$out .= " AUTO_INCREMENT=". $table['auto_increment'];
|
||||
}
|
||||
$out .= ";\n";
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sql for the structure of the given table.
|
||||
*/
|
||||
function _get_view_create_sql($view) {
|
||||
$out = "";
|
||||
// Switch SQL mode to get rid of "CREATE ALGORITHM..." what requires more permissions + troubles with the DEFINER user
|
||||
$sql_mode = $this->query("SELECT @@SESSION.sql_mode")->fetchField();
|
||||
$this->query("SET sql_mode = 'ANSI'");
|
||||
$result = $this->query("SHOW CREATE VIEW `" . $view['name'] . "`", array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
$this->query("SET SQL_mode = :mode", array(':mode' => $sql_mode));
|
||||
foreach ($result as $create) {
|
||||
$out .= "DROP VIEW IF EXISTS `". $view['name'] ."`;\n";
|
||||
$out .= "SET sql_mode = 'ANSI';\n";
|
||||
$out .= strtr($create['Create View'], "\n", " ") . ";\n";
|
||||
$out .= "SET sql_mode = '$sql_mode';\n";
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sql to insert the data for a given table
|
||||
*/
|
||||
function _dump_table_data_sql_to_file($file, $table) {
|
||||
$rows_per_line = variable_get('backup_migrate_data_rows_per_line', 30);
|
||||
$bytes_per_line = variable_get('backup_migrate_data_bytes_per_line', 2000);
|
||||
|
||||
$lines = 0;
|
||||
$data = $this->query("SELECT * FROM `". $table['name'] ."`", array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
$rows = $bytes = 0;
|
||||
|
||||
// Escape backslashes, PHP code, special chars
|
||||
$search = array('\\', "'", "\x00", "\x0a", "\x0d", "\x1a");
|
||||
$replace = array('\\\\', "''", '\0', '\n', '\r', '\Z');
|
||||
|
||||
$line = array();
|
||||
foreach ($data as $row) {
|
||||
// DB Escape the values.
|
||||
$items = array();
|
||||
foreach ($row as $key => $value) {
|
||||
$items[] = is_null($value) ? "null" : "'". str_replace($search, $replace, $value) ."'";
|
||||
}
|
||||
|
||||
// If there is a row to be added.
|
||||
if ($items) {
|
||||
// Start a new line if we need to.
|
||||
if ($rows == 0) {
|
||||
$file->write("INSERT INTO `". $table['name'] ."` VALUES ");
|
||||
$bytes = $rows = 0;
|
||||
}
|
||||
// Otherwise add a comma to end the previous entry.
|
||||
else {
|
||||
$file->write(",");
|
||||
}
|
||||
|
||||
// Write the data itself.
|
||||
$sql = implode(',', $items);
|
||||
$file->write('('. $sql .')');
|
||||
$bytes += strlen($sql);
|
||||
$rows++;
|
||||
|
||||
// Finish the last line if we've added enough items
|
||||
if ($rows >= $rows_per_line || $bytes >= $bytes_per_line) {
|
||||
$file->write(";\n");
|
||||
$lines++;
|
||||
$bytes = $rows = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Finish any unfinished insert statements.
|
||||
if ($rows > 0) {
|
||||
$file->write(";\n");
|
||||
$lines++;
|
||||
}
|
||||
|
||||
return $lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the db connection for the specified db.
|
||||
*/
|
||||
function _get_db_connection() {
|
||||
if (!$this->connection) {
|
||||
$this->connection = parent::_get_db_connection();
|
||||
// Set the sql mode because the default is ANSI,TRADITIONAL which is not aware of collation or storage engine.
|
||||
$this->connection->exec("SET sql_mode=''");
|
||||
}
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a db query on this destination's db.
|
||||
*/
|
||||
function query($query, $args = array(), $options = array()) {
|
||||
if ($conn = $this->_get_db_connection()) {
|
||||
return $conn->query($query, $args, $options);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The header for the top of the sql dump file. These commands set the connection
|
||||
* character encoding to help prevent encoding conversion issues.
|
||||
*/
|
||||
function _get_sql_file_header() {
|
||||
return "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
|
||||
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
|
||||
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
|
||||
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=NO_AUTO_VALUE_ON_ZERO */;
|
||||
|
||||
SET NAMES utf8;
|
||||
|
||||
";
|
||||
}
|
||||
|
||||
/**
|
||||
* The footer of the sql dump file.
|
||||
*/
|
||||
function _get_sql_file_footer() {
|
||||
return "
|
||||
|
||||
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
|
||||
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
|
||||
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
|
||||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||
";
|
||||
}
|
||||
}
|
140
sites/all/modules/backup_migrate/includes/destinations.email.inc
Normal file
140
sites/all/modules/backup_migrate/includes/destinations.email.inc
Normal file
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Functions to handle the email backup destination.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A destination for emailing database backups.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
class backup_migrate_destination_email extends backup_migrate_destination {
|
||||
var $supported_ops = array('scheduled backup', 'manual backup', 'remote backup', 'configure');
|
||||
|
||||
/**
|
||||
* Save to (ie. email the file) to the email destination.
|
||||
*/
|
||||
function save_file($file, $settings) {
|
||||
$size = filesize($file->filepath());
|
||||
$max = variable_get('backup_migrate_max_email_size', 20971520);
|
||||
if ($size > $max) {
|
||||
_backup_migrate_message('Could not email the file @file because it is @size and Backup and Migrate only supports emailing files smaller than @max.', array('@file' => $file->filename(), '@size' => format_size($size), '@max' => format_size($max)), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
$attachment = new stdClass();
|
||||
$attachment->filename = $file->filename();
|
||||
$attachment->path = $file->filepath();
|
||||
_backup_migrate_destination_email_mail_backup($attachment, $this->get_location());
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function edit_form() {
|
||||
$form = parent::edit_form();
|
||||
$form['location'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Email Address"),
|
||||
"#default_value" => $this->get_location(),
|
||||
"#required" => TRUE,
|
||||
"#description" => t('Enter the email address to send the backup files to. Make sure the email sever can handle large file attachments'),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the configuration form. Make sure the email address is valid.
|
||||
*/
|
||||
function settings_form_validate($values) {
|
||||
if (!valid_email_address($values['location'])) {
|
||||
form_set_error('[location]', t('The e-mail address %mail is not valid.', array('%mail' => $form_state['values']['location'])));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @function
|
||||
* Temporary mail handler class.
|
||||
*
|
||||
* Defines a mail class to send a message with an attachment. Eventually Drupal
|
||||
* core should provide this functionality, at which time this code will be
|
||||
* removed.
|
||||
*
|
||||
* More info on sending email at <http://php.net/function.mail>.
|
||||
* This function taken from dba.module.
|
||||
*
|
||||
* @param $attachment
|
||||
* An object which contains two variables "path" the path to the file and
|
||||
* filename and "filename" which is just the filename.
|
||||
*/
|
||||
function _backup_migrate_destination_email_mail_backup($attachment, $to) {
|
||||
// Send mail
|
||||
$attach = fread(fopen($attachment->path, "r"), filesize($attachment->path));
|
||||
$mail = new mime_mail();
|
||||
$mail->from = variable_get('site_mail', ini_get('sendmail_from'));
|
||||
$mail->headers = 'Errors-To: [EMAIL='. $mail->from .']'. $mail->from .'[/EMAIL]';
|
||||
$mail->to = $to;
|
||||
$mail->subject = t('Database backup from !site: !file', array('!site' => variable_get('site_name', 'drupal'), '!file' => $attachment->filename));
|
||||
$mail->body = t('Database backup attached.') ."\n\n";
|
||||
|
||||
$mail->add_attachment("$attach", $attachment->filename, "Content-Transfer-Encoding: base64 /9j/4AAQSkZJRgABAgEASABIAAD/7QT+UGhvdG9zaG", NULL, TRUE);
|
||||
$mail->send();
|
||||
}
|
||||
|
||||
class mime_mail {
|
||||
var $parts;
|
||||
var $to;
|
||||
var $from;
|
||||
var $headers;
|
||||
var $subject;
|
||||
var $body;
|
||||
|
||||
function __construct() {
|
||||
$this->parts = array();
|
||||
$this->to = "";
|
||||
$this->from = "";
|
||||
$this->headers = "";
|
||||
$this->subject = "";
|
||||
$this->body = "";
|
||||
}
|
||||
|
||||
function add_attachment($message, $name = "", $ctype = "application/octet-stream", $encode = NULL, $attach = FALSE) {
|
||||
$this->parts[] = array(
|
||||
"ctype" => $ctype,
|
||||
"message" => $message,
|
||||
"encode" => $encode,
|
||||
"name" => $name,
|
||||
"attach" => $attach,
|
||||
);
|
||||
}
|
||||
|
||||
function build_message($part) {
|
||||
$message = $part["message"];
|
||||
$message = chunk_split(base64_encode($message));
|
||||
$encoding = "base64";
|
||||
$disposition = $part['attach'] ? "Content-Disposition: attachment; filename=$part[name]\n" : '';
|
||||
return "Content-Type: ". $part["ctype"] . ($part["name"] ? "; name = \"". $part["name"] ."\"" : "") ."\nContent-Transfer-Encoding: $encoding\n$disposition\n$message\n";
|
||||
}
|
||||
|
||||
function build_multipart() {
|
||||
$boundary = "b". md5(uniqid(time()));
|
||||
$multipart = "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME encoded message.\n\n--$boundary";
|
||||
for ($i = sizeof($this->parts) - 1; $i >= 0; $i--) {
|
||||
$multipart .= "\n". $this->build_message($this->parts[$i]) ."--$boundary";
|
||||
}
|
||||
return $multipart .= "--\n";
|
||||
}
|
||||
|
||||
function send() {
|
||||
$mime = "";
|
||||
if (!empty($this->from)) $mime .= "From: ". $this->from ."\n";
|
||||
if (!empty($this->headers)) $mime .= $this->headers ."\n";
|
||||
if (!empty($this->body)) $this->add_attachment($this->body, "", "text/plain");
|
||||
$mime .= "MIME-Version: 1.0\n". $this->build_multipart();
|
||||
mail(trim($this->to), $this->subject, "", $mime);
|
||||
}
|
||||
}
|
318
sites/all/modules/backup_migrate/includes/destinations.file.inc
Normal file
318
sites/all/modules/backup_migrate/includes/destinations.file.inc
Normal file
|
@ -0,0 +1,318 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* A destination type for saving locally to the server.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A destination type for saving locally to the server.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
|
||||
class backup_migrate_destination_files extends backup_migrate_destination {
|
||||
var $supported_ops = array('scheduled backup', 'manual backup', 'local backup', 'restore', 'list files', 'configure', 'delete');
|
||||
|
||||
function type_name() {
|
||||
return t("Server Directory");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file location.
|
||||
*/
|
||||
function get_realpath() {
|
||||
return drupal_realpath($this->get_location());
|
||||
}
|
||||
|
||||
/**
|
||||
* File save destination callback.
|
||||
*/
|
||||
function _save_file($file, $settings) {
|
||||
if ($this->confirm_destination() && $dir = $this->get_location()) {
|
||||
$filepath = rtrim($dir, "/") ."/". $file->filename();
|
||||
// Copy the file if there are multiple destinations.
|
||||
if (count($settings->get_destinations()) > 1) {
|
||||
file_unmanaged_copy($file->filepath(), $filepath);
|
||||
}
|
||||
// Otherwise we can move it and save a delete.
|
||||
else {
|
||||
file_unmanaged_move($file->filepath(), $filepath);
|
||||
}
|
||||
|
||||
// chmod, chown and chgrp the file if needed.
|
||||
if ($chmod = $this->settings('chmod')) {
|
||||
if (!@drupal_chmod($filepath, octdec($chmod))) {
|
||||
_backup_migrate_message('Unable to set the file mode for: @file', array('@file' => $filepath), 'error');
|
||||
}
|
||||
}
|
||||
if ($chgrp = $this->settings('chgrp')) {
|
||||
if (!@chgrp($filepath, $chgrp)) {
|
||||
_backup_migrate_message('Unable to set the file group for: @file', array('@file' => $filepath), 'error');
|
||||
}
|
||||
}
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if we can read the given file.
|
||||
*/
|
||||
function can_read_file($file_id) {
|
||||
return $this->op('restore') && is_readable($this->get_filepath($file_id));
|
||||
}
|
||||
|
||||
/**
|
||||
* File load destination callback.
|
||||
*/
|
||||
function load_file($file_id) {
|
||||
$filepath = $this->get_filepath($file_id);
|
||||
if (file_exists($filepath)) {
|
||||
backup_migrate_include('files');
|
||||
return new backup_file(array('filepath' => $filepath));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file object for the given file.
|
||||
*/
|
||||
function get_file($file_id) {
|
||||
$files = $this->list_files();
|
||||
if (isset($files[$file_id])) {
|
||||
isset($files[$file_id]);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* File list destination callback.
|
||||
*/
|
||||
function _list_files() {
|
||||
$files = array();
|
||||
if ($dir = $this->get_realpath()) {
|
||||
if ($handle = @opendir($dir)) {
|
||||
backup_migrate_include('files');
|
||||
while (FALSE !== ($file = readdir($handle))) {
|
||||
if (substr($file, 0, 1) !== '.') {
|
||||
$filepath = $dir ."/". $file;
|
||||
$files[$file] = new backup_file(array('filepath' => $filepath));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* File delete destination callback.
|
||||
*/
|
||||
function _delete_file($file_id) {
|
||||
$filepath = $this->get_filepath($file_id);
|
||||
file_unmanaged_delete($filepath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the filepath from the given file id.
|
||||
*/
|
||||
function get_filepath($file_id) {
|
||||
if ($dir = $this->get_realpath()) {
|
||||
$filepath = rtrim($dir, '/') .'/'. $file_id;
|
||||
return $filepath;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for the files destination.
|
||||
*/
|
||||
function edit_form() {
|
||||
$form = parent::edit_form();
|
||||
$form['location'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Directory path"),
|
||||
"#default_value" => $this->get_location(),
|
||||
"#required" => TRUE,
|
||||
"#description" => t('Enter the path to the directory to save the backups to. Use a relative path to pick a path relative to your Drupal root directory. The web server must be able to write to this path.'),
|
||||
);
|
||||
$form['settings'] = array(
|
||||
'#type' => 'fieldset',
|
||||
'#title' => t('Advanced Settings'),
|
||||
'#tree' => TRUE,
|
||||
'#collapsible' => TRUE,
|
||||
'#collapsed' => TRUE,
|
||||
);
|
||||
if (function_exists('chmod')) {
|
||||
$form['settings']['chmod'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Change file mode (chmod)'),
|
||||
'#size' => 5,
|
||||
'#default_value' => $this->settings('chmod'),
|
||||
'#description' => t('If you enter a value here, backup files will be chmoded with the mode you specify. Specify the mode in octal form (e.g. 644 or 0644) or leave blank to disable this feature.'),
|
||||
);
|
||||
}
|
||||
if (function_exists('chgrp')) {
|
||||
$form['settings']['chgrp'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Change file group (chgrp)'),
|
||||
'#size' => 5,
|
||||
'#default_value' => $this->settings('chgrp'),
|
||||
'#description' => t('If you enter a value here, backup files will be chgrped to the group you specify. Leave blank to disable this feature.'),
|
||||
);
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the form for the settings for the files destination.
|
||||
*/
|
||||
function edit_form_validate($form, &$form_state) {
|
||||
$values = $form_state['values'];
|
||||
if (isset($values['settings']['chmod']) && !empty($values['settings']['chmod']) && !preg_match('/0?[0-7]{3}/', $values['settings']['chmod'])) {
|
||||
form_set_error('chmod', t('You must enter a valid chmod octal value (e.g. 644 or 0644) in the change mode field, or leave it blank.'));
|
||||
}
|
||||
parent::edit_form_validate($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the form for the settings for the files destination.
|
||||
*/
|
||||
function edit_form_submit($form, &$form_state) {
|
||||
// Add a 0 to the start of a 3 digit file mode to make it proper PHP encoded octal.
|
||||
if (strlen($form_state['values']['settings']['chmod']) == 3) {
|
||||
$form_state['values']['settings']['chmod'] = '0' . $form_state['values']['settings']['chmod'];
|
||||
}
|
||||
parent::edit_form_submit($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a destination is valid.
|
||||
*/
|
||||
function confirm_destination() {
|
||||
if ($dir = $this->get_location()) {
|
||||
return $this->check_dir($dir);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the destination directory for the backups.
|
||||
*/
|
||||
function check_dir($directory) {
|
||||
if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
|
||||
// Unable to create destination directory.
|
||||
_backup_migrate_message("Unable to create or write to the save directory '%directory'. Please check the file permissions of that directory and try again.", array('%directory' => $directory), "error");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// If the destination directory is within the webroot, then secure it as best we can.
|
||||
if ($this->dir_in_webroot($directory)) {
|
||||
$directory = $this->check_web_dir($directory);
|
||||
}
|
||||
|
||||
return $directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a web accessible directory has been properly secured, othewise attempt to secure it.
|
||||
*/
|
||||
function check_web_dir($directory) {
|
||||
// Check if the file has already been tested.
|
||||
if (is_file($directory .'/tested.txt')) {
|
||||
return $directory;
|
||||
}
|
||||
else {
|
||||
file_create_htaccess($directory, TRUE);
|
||||
|
||||
// Check the user agent to make sure we're not responding to a request from drupal itself.
|
||||
// That should prevent infinite loops which could be caused by poormanscron in some circumstances.
|
||||
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Drupal') !== FALSE) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Check to see if the destination is publicly accessible
|
||||
$test_contents = "this file should not be publicly accessible";
|
||||
// Create the the text.txt file if it's not already there.
|
||||
if (!is_file($directory .'/test.txt') || file_get_contents($directory .'/test.txt') != $test_contents) {
|
||||
if ($fp = fopen($directory .'/test.txt', 'w')) {
|
||||
@fputs($fp, $test_contents);
|
||||
fclose($fp);
|
||||
}
|
||||
else {
|
||||
$message = t("Security notice: Backup and Migrate was unable to write a test text file to the destination directory %directory, and is therefore unable to check the security of the backup destination. Backups to the server will be disabled until the destination becomes writable and secure.", array('%directory' => $directory));
|
||||
drupal_set_message($message, "error");
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to read the test file via http. This may fail for other reasons,
|
||||
// so it's not a bullet-proof check.
|
||||
if ($this->test_file_readable_remotely($directory .'/test.txt', $test_contents)) {
|
||||
$message = t("Security notice: Backup and Migrate will not save backup files to the server because the destination directory is publicly accessible. If you want to save files to the server, please secure the '%directory' directory", array('%directory' => $directory));
|
||||
drupal_set_message($message, "error");
|
||||
return FALSE;
|
||||
}
|
||||
// Directory tested OK, so we mark it as tested.
|
||||
if ($fp = fopen($directory .'/tested.txt', 'w')) {
|
||||
$contents = t('The presence of this file indicates that this directory has been tested as safe to use as a destination for Backup and Migrate. If you change the permissions of this directory or change your web server settings, please delete this file so that the directory can be checked again.');
|
||||
@fputs($fp, $contents);
|
||||
fclose($fp);
|
||||
}
|
||||
return $directory;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given directory is within the webroot and is therefore web accessible.
|
||||
*/
|
||||
function dir_in_webroot($directory) {
|
||||
$real_dir = drupal_realpath($directory);
|
||||
$real_root = drupal_realpath(DRUPAL_ROOT);
|
||||
if ($real_dir == $real_root || strpos($real_dir, $real_root . '/') === 0) {
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a file can be read remotely via http.
|
||||
*/
|
||||
function test_file_readable_remotely($directory, $contents) {
|
||||
$real_dir = drupal_realpath($directory);
|
||||
$real_root = drupal_realpath(DRUPAL_ROOT);
|
||||
if ($real_dir && $real_root) {
|
||||
// Get the root relative path.
|
||||
$path = substr($real_dir, strlen($real_root));
|
||||
|
||||
$url = $GLOBALS['base_url'] . str_replace('\\', '/', $path);
|
||||
$result = drupal_http_request($url);
|
||||
if (!empty($result->data) && strpos($result->data, $contents) !== FALSE) {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The manual files directory.
|
||||
*/
|
||||
class backup_migrate_destination_files_manual extends backup_migrate_destination_files {
|
||||
var $supported_ops = array('manual backup', 'restore', 'list files', 'configure', 'delete');
|
||||
function __construct($params = array()) {
|
||||
$dir = 'private://backup_migrate/manual';
|
||||
parent::__construct($params + array('location' => $dir, 'name' => t('Manual Backups Directory')));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The scheduled files directory.
|
||||
*/
|
||||
class backup_migrate_destination_files_scheduled extends backup_migrate_destination_files {
|
||||
var $supported_ops = array('scheduled backup', 'restore', 'list files', 'configure', 'delete');
|
||||
function __construct($params = array()) {
|
||||
$dir = 'private://backup_migrate/scheduled';
|
||||
parent::__construct($params + array('location' => $dir, 'name' => t('Scheduled Backups Directory')));
|
||||
}
|
||||
}
|
||||
|
435
sites/all/modules/backup_migrate/includes/destinations.ftp.inc
Normal file
435
sites/all/modules/backup_migrate/includes/destinations.ftp.inc
Normal file
|
@ -0,0 +1,435 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Functions to handle the FTP backup destination.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A destination for sending database backups to an FTP server.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
class backup_migrate_destination_ftp extends backup_migrate_destination_remote {
|
||||
var $supported_ops = array('scheduled backup', 'manual backup', 'remote backup', 'restore', 'list files', 'configure', 'delete');
|
||||
var $ftp = NULL;
|
||||
|
||||
/**
|
||||
* Save to the ftp destination.
|
||||
*/
|
||||
function _save_file($file, $settings) {
|
||||
$ftp = $this->ftp_object();
|
||||
if (drupal_ftp_file_to_ftp($file->filepath(), $file->filename(), '.', $ftp)) {
|
||||
return $file;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load from the ftp destination.
|
||||
*/
|
||||
function load_file($file_id) {
|
||||
backup_migrate_include('files');
|
||||
$file = new backup_file(array('filename' => $file_id));
|
||||
$this->ftp_object();
|
||||
if (drupal_ftp_ftp_to_file($file->filepath(), $file_id, '.', $this->ftp)) {
|
||||
return $file;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete from the ftp destination.
|
||||
*/
|
||||
function _delete_file($file_id) {
|
||||
$this->ftp_object();
|
||||
drupal_ftp_delete_file($file_id, $this->ftp);
|
||||
}
|
||||
|
||||
function _list_files() {
|
||||
backup_migrate_include('files');
|
||||
$files = array();
|
||||
$this->ftp_object();
|
||||
$ftp_files = (array)drupal_ftp_file_list('.', $this->ftp);
|
||||
foreach ($ftp_files as $file) {
|
||||
$files[$file['filename']] = new backup_file($file);
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function edit_form() {
|
||||
$form = parent::edit_form();
|
||||
$form['scheme']['#type'] = 'value';
|
||||
$form['scheme']['#value'] = 'ftp';
|
||||
$form['port'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Port"),
|
||||
"#default_value" => @$this->dest_url['port'] ? $this->dest_url['port'] : '21',
|
||||
"#weight" => 15,
|
||||
);
|
||||
$form['pasv'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Use PASV transfers'),
|
||||
'#default_value' => $this->get_pasv(),
|
||||
'#weight' => 50,
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
function set_pasv($value) {
|
||||
$this->settings['pasv'] = (bool)$value;
|
||||
}
|
||||
|
||||
function get_pasv() {
|
||||
return isset($this->settings['pasv']) ? $this->settings['pasv'] : FALSE;
|
||||
}
|
||||
|
||||
function ftp_object() {
|
||||
if (!$this->ftp) {
|
||||
$this->dest_url['port'] = empty($this->dest_url['port']) ? '21' : $this->dest_url['port'];
|
||||
$this->dest_url['pasv'] = $this->get_pasv();
|
||||
$this->ftp = drupal_ftp_ftp_object($this->dest_url['host'], $this->dest_url['port'], $this->dest_url['user'], $this->dest_url['pass'], $this->dest_url['path'], $this->dest_url['pasv']);
|
||||
}
|
||||
return $this->ftp;
|
||||
}
|
||||
}
|
||||
|
||||
// The FTP code below was taken from the ftp module by Aaron Winborn.
|
||||
|
||||
// Inspired by http://www.devarticles.com/c/a/PHP/My-FTP-Wrapper-Class-for-PHP/
|
||||
// It's been drupalized, however, and most of the bugs from that example have been fixed.
|
||||
// - winborn 2007-06-22 - 2007-06-28
|
||||
|
||||
define('DRUPAL_FTP_FT_DIRECTORY', 0);
|
||||
define('DRUPAL_FTP_FT_FILE', 1);
|
||||
|
||||
/**
|
||||
* creates a new ftp object. if any elements of ftp_map are missing, they'll be filled with the server defaults.
|
||||
*/
|
||||
function drupal_ftp_ftp_object($server, $port, $user, $pass, $dir, $pasv) {
|
||||
$ftp = new stdClass();
|
||||
|
||||
$ftp->__server = $server;
|
||||
$ftp->__port = $port;
|
||||
$ftp->__user = $user;
|
||||
$ftp->__password = $pass;
|
||||
$ftp->__directory = $dir;
|
||||
$ftp->__pasv = $pasv;
|
||||
|
||||
return $ftp;
|
||||
}
|
||||
|
||||
/**
|
||||
* The drupal_ftp_connect function
|
||||
* This function connects to an FTP server and attempts to change into the directory specified by
|
||||
* the fourth parameter, $directory.
|
||||
*/
|
||||
function drupal_ftp_connect(&$ftp) {
|
||||
if (is_NULL($ftp)) {
|
||||
$ftp = drupal_ftp_ftp_object();
|
||||
}
|
||||
|
||||
if (!$ftp->__conn && !drupal_ftp_connected($ftp)) {
|
||||
// Attempt to connect to the remote server
|
||||
$ftp->__conn = @ftp_connect($ftp->__server, $ftp->__port);
|
||||
|
||||
if (!$ftp->__conn) {
|
||||
_backup_migrate_message('FTP Error: Couldn\'t connect to server @server', array('@server' => $ftp->__server), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Attempt to login to the remote server
|
||||
$ftp->__login = @ftp_login($ftp->__conn, $ftp->__user, $ftp->__password);
|
||||
|
||||
if (!$ftp->__login) {
|
||||
_backup_migrate_message('FTP Error: Couldn\'t login as user @ftp_user to @server', array('@ftp_user' => $ftp->__user, '@server' => $ftp->__server), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Attempt to change into the working directory
|
||||
$chdir = @ftp_chdir($ftp->__conn, $ftp->__directory);
|
||||
|
||||
if (!$chdir) {
|
||||
_backup_migrate_message('FTP Error: Couldn\'t change into the @directory directory', array('@directory' => $ftp->__directory), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Set PASV - if needed
|
||||
if ($ftp->__pasv) {
|
||||
$pasv = @ftp_pasv($ftp->__conn, TRUE);
|
||||
if (!$pasv) {
|
||||
_backup_migrate_message('FTP Error: Couldn\'t set PASV mode', array(), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Everything worked OK, return TRUE
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* The drupal_ftp_connected function
|
||||
* This function queries the FTP server with the ftp_systype command to check if the connection is still alive.
|
||||
* It returns TRUE on success or FALSE on disconnection.
|
||||
*/
|
||||
function drupal_ftp_connected(&$ftp) {
|
||||
// Attempt to call the ftp_systype to see if the connect
|
||||
// to the FTP server is still alive and kicking
|
||||
|
||||
if (is_NULL($ftp)) {
|
||||
$ftp = drupal_ftp_ftp_object();
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!@ftp_systype($ftp->__conn)) {
|
||||
// The connection is dead
|
||||
return FALSE;
|
||||
}
|
||||
else {
|
||||
// The connection is still alive
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function tries to retrieve the contents of a file from the FTP server.
|
||||
* Firstly it changes into the $directory directory, and then attempts to download the file $filename.
|
||||
* The file is saved locally and its contents are returned to the caller of the function.
|
||||
*/
|
||||
function drupal_ftp_ftp_to_file($file, $filename, $directory, &$ftp) {
|
||||
// Change into the remote directory and retrieve the content
|
||||
// of a file. Once retrieve, return this value to the caller
|
||||
|
||||
if (!@drupal_ftp_connect($ftp)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// We are now connected, so let's retrieve the file contents.
|
||||
// Firstly, we change into the directory
|
||||
$chdir = @ftp_chdir($ftp->__conn, $directory);
|
||||
|
||||
if (!$chdir) {
|
||||
_backup_migrate_message('FTP Error: Couldn\'t change into directory: @directory', array('@directory' => $directory), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// We have changed into the directory, let's attempt to get the file
|
||||
$fp = @fopen($file, 'wb');
|
||||
$get_file = @ftp_fget($ftp->__conn, $fp, $filename, FTP_BINARY);
|
||||
fclose($fp);
|
||||
|
||||
$fp = NULL;
|
||||
|
||||
if (!$get_file) {
|
||||
_backup_migrate_message('FTP Error: Unable to download file: @filename from @directory', array('@filename' => $filename, '@directory' => $directory), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a file to an FTP server.
|
||||
*/
|
||||
function drupal_ftp_file_to_ftp($file, $ftp_filename, $ftp_directory, &$ftp) {
|
||||
if (!@drupal_ftp_connect($ftp)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if ($source = drupal_realpath($file)) {
|
||||
// Now we can try to write to the remote file
|
||||
$complete_filename = $ftp_directory .'/'. $ftp_filename;
|
||||
$put_file = @ftp_put($ftp->__conn, $complete_filename, $source, FTP_BINARY);
|
||||
if (!$put_file) {
|
||||
_backup_migrate_message('FTP Error: Couldn\'t write to @complete_filename when trying to save file on the ftp server.', array('@complete_filename' => $complete_filename), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Everything worked OK
|
||||
return TRUE;
|
||||
}
|
||||
else {
|
||||
_backup_migrate_message('FTP Error: Couldn\'t find @file.', array('@file'), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The drupal_ftp_change_directory Function
|
||||
* This function simply changes into the $directory folder on the FTP server.
|
||||
* If a connection or permission error occurs then _backup_migrate_message() will contain the error message.
|
||||
*/
|
||||
function drupal_ftp_change_directory($directory, &$ftp) {
|
||||
// Switch to another directory on the web server. If we don't
|
||||
// have permissions then an error will occur
|
||||
|
||||
if (!@drupal_ftp_connect($ftp)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Try and change into another directory
|
||||
$chdir = ftp_chdir($ftp->__conn, $directory);
|
||||
|
||||
if (!$chdir) {
|
||||
_backup_migrate_message('FTP Error: Couldn\'t change into directory: @directory', array('@directory', $directory), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
else {
|
||||
// Changing directories worked OK
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The drupal_ftp_file_list Function
|
||||
* This function will change into the $directory folder and get a list of files and directories contained in that folder.
|
||||
* This function still needs a lot of work, but should work in most cases.
|
||||
*/
|
||||
function drupal_ftp_file_list($directory, &$ftp) {
|
||||
// This function will attempt to change into the specified
|
||||
// directory and retrieve a list of files as an associative
|
||||
// array. This list will include file name, size and date last modified
|
||||
|
||||
$file_array = array();
|
||||
|
||||
// Can we switch to the desired directory?
|
||||
if (!drupal_ftp_change_directory($directory, $ftp)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// We are in the directory, let's retrieve a list of files
|
||||
// This is slower than parsing the raw return values, but it is faster.
|
||||
$file_list = ftp_nlist($ftp->__conn, $directory);
|
||||
|
||||
// Save the list of files
|
||||
if (@is_array($file_list)) {
|
||||
// Interate through the array
|
||||
foreach ($file_list as $file) {
|
||||
$file_array[] = array(
|
||||
'filename' => $file,
|
||||
'filesize' => ftp_size($ftp->__conn, $directory ."/". $file),
|
||||
'filetime' => ftp_mdtm($ftp->__conn, $directory ."/". $file),
|
||||
);
|
||||
}
|
||||
}
|
||||
sort($file_array);
|
||||
return $file_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* The drupal_ftp_create_directory Function
|
||||
* This function tries to make a new directory called $folder_name on the FTP server.
|
||||
* If it can create the folder, then the folder is given appropriate rights with the CHMOD command.
|
||||
*/
|
||||
function drupal_ftp_create_directory($folder_name, &$ftp) {
|
||||
// Makes a new folder on the web server via FTP
|
||||
|
||||
if (!@drupal_ftp_connect($ftp)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$create_result = @ftp_mkdir($ftp->__conn, $folder_name);
|
||||
|
||||
if ($create_result == TRUE) {
|
||||
// Can we change the files permissions?
|
||||
$exec_result = @ftp_site($ftp->__conn, 'chmod 0777 '. $folder_name .'/');
|
||||
|
||||
if ($exec_result == TRUE) {
|
||||
return TRUE;
|
||||
}
|
||||
else {
|
||||
_backup_migrate_message('FTP Error: Couldn\'t set owner permissions on @folder.', array('@folder', $folder_name), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
else {
|
||||
_backup_migrate_message('FTP Error: Couldn\'t create new folder @folder.', array('@folder', $folder_name), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The drupal_ftp_delete_file Function
|
||||
* This function attempts to delete a file called $filename from the FTP server.
|
||||
*/
|
||||
function drupal_ftp_delete_file($filename, &$ftp) {
|
||||
// Remove the specified file from the FTP server
|
||||
if (!@drupal_ftp_connect($ftp)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$delete_result = @ftp_delete($ftp->__conn, $filename);
|
||||
|
||||
if ($delete_result == TRUE) {
|
||||
// The file/folder was renamed successfully
|
||||
return TRUE;
|
||||
}
|
||||
else {
|
||||
// Couldn't delete the selected file
|
||||
_backup_migrate_message('FTP Error: Couldn\'t delete the selected file: @filename', array('@filename' => $filename), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The drupal_ftp_delete_folder Function
|
||||
* This function was one of the hardest to write. It recursively deletes all files and folders from a directory called $folder_name.
|
||||
*/
|
||||
function drupal_ftp_delete_folder($folder_name, &$ftp) {
|
||||
if (!@drupal_ftp_connect($ftp)) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
@ftp_chdir($ftp->__conn, $folder_name);
|
||||
$location = @ftp_pwd($ftp->__conn);
|
||||
|
||||
$directories = array();
|
||||
$files = array();
|
||||
$dir_counter = 0;
|
||||
$file_counter = 0;
|
||||
$content = @ftp_nlist($ftp->__conn, ".");
|
||||
|
||||
for ($i = 0; $i < sizeof($content); $i++) {
|
||||
// If we can change into this then it's a directory.
|
||||
// If not, it's a file
|
||||
if ($content[$i] != "." && $content[$i] != "..") {
|
||||
if (@ftp_chdir($ftp->__conn, $content[$i])) {
|
||||
// We have a directory
|
||||
$directories[] = $content[$i];
|
||||
$dir_counter++;
|
||||
@ftp_cdup($ftp->__conn);
|
||||
}
|
||||
else {
|
||||
// We have a file
|
||||
$files[] = $content[$i];
|
||||
$file_counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for ($j = 0; $j < $file_counter; $j++) {
|
||||
@ftp_delete($ftp->__conn, $files[$j]);
|
||||
}
|
||||
|
||||
for ($j = 0; $j < $dir_counter; $j++) {
|
||||
if ($directories[$j] != "." OR $directories[$j] != "..") {
|
||||
$location = ftp_pwd($ftp->__conn);
|
||||
drupal_ftp_delete_folder($directories[$j], $ftp);
|
||||
@ftp_cdup ($ftp->__conn);
|
||||
@ftp_rmdir($ftp->__conn, $directories[$j]);
|
||||
}
|
||||
}
|
||||
|
||||
// Lastly, we change into the directory that we want to delete and see
|
||||
// if we can cdup. If we can, we delete it.
|
||||
@ftp_chdir($ftp->__conn, $folder_name);
|
||||
@ftp_cdup($ftp->__conn);
|
||||
@ftp_rmdir($ftp->__conn, $folder_name);
|
||||
|
||||
// Did the recursive folder/file deletion work?
|
||||
return TRUE;
|
||||
}
|
1280
sites/all/modules/backup_migrate/includes/destinations.inc
Normal file
1280
sites/all/modules/backup_migrate/includes/destinations.inc
Normal file
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,976 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Functions to handle the NodeSquirrel backup destination.
|
||||
*/
|
||||
|
||||
define('NODESQUIRREL_SECRET_KEY_PATTERN', '/^[0-9a-f]{32}\:?[0-9a-f]{32}$/');
|
||||
|
||||
/**
|
||||
* Check that a nodesquirel key is valid.
|
||||
*/
|
||||
function backup_migrate_nodesquirrel_check_secret_key($secret_key) {
|
||||
if ($destination = backup_migrate_nodesquirrel_get_destination($secret_key)) {
|
||||
if ($destination->confirm_destination()) {
|
||||
return $destination;
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the NS destination for the given key.
|
||||
*/
|
||||
function backup_migrate_nodesquirrel_get_destination($secret_key) {
|
||||
if ($secret_key) {
|
||||
backup_migrate_include('destinations');
|
||||
$destination = backup_migrate_create_destination('nodesquirrel', array('machine_name' => 'nodesquirrel'));
|
||||
$destination->settings['secret_key'] = $secret_key;
|
||||
return $destination;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a helper link to activate a site and create a tree.
|
||||
*/
|
||||
function backup_migrate_nodesquirrel_get_activate_help_text() {
|
||||
$activate_link = backup_migrate_nodesquirrel_get_activate_link();
|
||||
return array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('Need a Secret Key?'),
|
||||
'#markup' => t('Visit !nodesquirrel.', array('!nodesquirrel' => $activate_link)),
|
||||
'#description' => t('Don\'t worry if you don\'t have an account yet. You can create one when you get there.'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a helper link to activate a site and create a tree.
|
||||
*/
|
||||
function backup_migrate_nodesquirrel_get_activate_link() {
|
||||
$activate_link = l('nodesquirrel.com/activate', variable_get('nodesquirrel_activate_url', 'https://manage.nodesquirrel.com/activate'), array('query' => array('url' => url('', array('absolute' => TRUE)), 'email' => variable_get('site_mail', ''), 'configure' => url($_GET['q'], array('absolute' => TRUE)))));
|
||||
return $activate_link;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a helper link to activate a site and create a tree.
|
||||
*/
|
||||
function backup_migrate_nodesquirrel_get_manage_link($destination) {
|
||||
$url = variable_get('nodesquirrel_manage_url', 'https://manage.nodesquirrel.com') . '/backups/' . $destination->_get_destination();
|
||||
return l($url, $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a helper link to activate a site and create a tree.
|
||||
*/
|
||||
function backup_migrate_nodesquirrel_get_plan_link() {
|
||||
$url = variable_get('nodesquirrel_manage_url', 'https://manage.nodesquirrel.com') . '/plan';
|
||||
return l($url, $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to the nodesquirrel settings tab.
|
||||
*/
|
||||
function backup_migrate_nodesquirrel_settings_path() {
|
||||
$path = BACKUP_MIGRATE_MENU_PATH . '/settings/destinations/nodesquirrel/edit';
|
||||
return $path;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* NodeSquirrel settings page callback.
|
||||
*/
|
||||
function backup_migrate_nodesquirrel_settings() {
|
||||
return drupal_get_form('backup_migrate_nodesquirrel_settings_form');
|
||||
}
|
||||
|
||||
/**
|
||||
* NodeSquirrel settings form.
|
||||
*/
|
||||
function backup_migrate_nodesquirrel_credentials_settings_form($key = '', $status) {
|
||||
$collapse = !empty($status);
|
||||
$form['nodesquirrel_credentials'] = array(
|
||||
'#type' => 'fieldset',
|
||||
'#title' => t('NodeSquirrel Credentials'),
|
||||
'#collapsible' => $collapse,
|
||||
'#collapsed' => $collapse,
|
||||
);
|
||||
|
||||
$form['nodesquirrel_credentials']['nodesquirrel_secret_key'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Secret Key'),
|
||||
'#size' => 80,
|
||||
'#default_value' => $key,
|
||||
);
|
||||
if (empty($key)) {
|
||||
$form['nodesquirrel_credentials']['secret_key_help'] = backup_migrate_nodesquirrel_get_activate_help_text();
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a form element with some help text describing NodeSquirrel.
|
||||
*/
|
||||
function backup_migrate_nodesquirrel_info_form() {
|
||||
$form = array();
|
||||
|
||||
$form['nodesquirrel_info'] = array(
|
||||
'#type' => 'fieldset',
|
||||
'#title' => t('What is NodeSquirrel?'),
|
||||
);
|
||||
|
||||
$form['nodesquirrel_info']['intro'] = array(
|
||||
'#type' => 'markup',
|
||||
'#markup' => t('<p>NodeSquirrel is the cloud backup service built by the maintainers of Backup and Migrate.</p></p>NodeSquirrel is now free for all Drupal sites, courtesy of Pantheon.</p><p>Create your free account at !signup</p>', array('!nodesquirrel' => l('nodesquirrel.com', 'http://www.nodesquirrel.com'), '!signup' => backup_migrate_nodesquirrel_get_activate_link())),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the NodeSquirrel status on the configuration form.
|
||||
*/
|
||||
function backup_migrate_nodesquirrel_status_form($key, $destination, $status) {
|
||||
$form = array();
|
||||
|
||||
$form['nodesquirrel_status'] = array(
|
||||
'#type' => 'fieldset',
|
||||
'#title' => t('NodeSquirrel Status'),
|
||||
);
|
||||
$form['nodesquirrel_status']['status'] = array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('NodeSquirrel Status'),
|
||||
'#markup' => t('Not Configured. Enter your Secret Key below to get started.'),
|
||||
);
|
||||
|
||||
// Warn the user if the key they entered is invalid.
|
||||
if ($key && empty($status)) {
|
||||
$form['nodesquirrel_status']['status']['#markup'] = t('Your secret key does not seem to be valid. Please check that you entered it correctly or visit !ns to generate a new key.', array('!ns' => backup_migrate_nodesquirrel_get_activate_link()));
|
||||
}
|
||||
else if (!empty($destination) && is_array($status)) {
|
||||
if (!empty($status['lifetime_backups_used']) && !empty($status['lifetime_backups']) && $status['lifetime_backups_used'] >= $status['lifetime_backups']) {
|
||||
$form['nodesquirrel_status']['status']['#markup'] = t('Your !num backup trial has expired. Visit !link to continue backing up.', array('!num' => $status['lifetime_backups'], '!link' => backup_migrate_nodesquirrel_get_plan_link()));
|
||||
}
|
||||
else {
|
||||
$form['nodesquirrel_status']['status']['#markup'] = t('Ready to Backup');
|
||||
if (user_access('perform backup')) {
|
||||
$form['nodesquirrel_status']['status']['#markup'] .= ' ' . l('(' . t('backup now') . ')', BACKUP_MIGRATE_MENU_PATH);
|
||||
}
|
||||
}
|
||||
if (!empty($status['plan_name'])) {
|
||||
$form['nodesquirrel_status']['plan_name'] = array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('Current Plan'),
|
||||
'#markup' => check_plain($status['plan_name'])
|
||||
);
|
||||
|
||||
if (!empty($status['lifetime_backups']) && isset($status['lifetime_backups_used'])) {
|
||||
$remains = $status['lifetime_backups'] - $status['lifetime_backups_used'];
|
||||
$remains = $remains > 0 ? $remains : t('none');
|
||||
$form['nodesquirrel_status']['plan_name']['#markup'] .= ' ' . t('(@remains remaining of @backups backup trial)', array('@backups' => $status['lifetime_backups'], '@remains' => $remains));
|
||||
}
|
||||
|
||||
if (!empty($status['lifespan']) && isset($status['age']) && $status['lifespan'] > 0) {
|
||||
$remains = ceil(($status['lifespan'] - $status['age']) / 86400);
|
||||
if ($remains <= 0) {
|
||||
$form['nodesquirrel_status']['plan_name']['#markup'] .= ' ' . t('(Your !span day trial has expired.)', array('!span' => ceil($status['lifespan'] / 86400)));
|
||||
}
|
||||
else {
|
||||
$form['nodesquirrel_status']['plan_name']['#markup'] .= ' ' . format_plural($remains, '(1 day remaining)', '(!span days remaining)', array('!span' => ceil($remains)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (isset($status['backups_used'])) {
|
||||
$form['nodesquirrel_status']['backups_used'] = array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('Number of Stored Backups'),
|
||||
'#markup' => $status['backups_used'] == 0 ? t('None') : number_format($status['backups_used'])
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($status['last_backup'])) {
|
||||
$form['nodesquirrel_status']['last_backup'] = array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('Last Backup'),
|
||||
'#markup' => empty($status['last_backup']) ? t('Never') : t('!date (!ago ago)', array('!date' => format_date($status['last_backup'], 'small'), '!ago' => format_interval(time() - $status['last_backup'], 1)))
|
||||
);
|
||||
}
|
||||
if ($status['bytes_per_locker']) {
|
||||
if (isset($status['bytes_used'])) {
|
||||
$form['nodesquirrel_status']['space'] = array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('Storage Space'),
|
||||
'#markup' => t('!used used of !total (!remaining remaining)', array('!used' => backup_migrate_format_size($status['bytes_used']), '!total' => backup_migrate_format_size($status['bytes_per_locker']), '!remaining' => backup_migrate_format_size(max(0, $status['bytes_per_locker'] - $status['bytes_used']))))
|
||||
);
|
||||
}
|
||||
else {
|
||||
$form['nodesquirrel_status']['space'] = array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('Total Storage Space'),
|
||||
'#markup' => t('!total', array('!total' => backup_migrate_format_size($status['bytes_per_locker'])))
|
||||
);
|
||||
}
|
||||
}
|
||||
$form['nodesquirrel_status']['manage'] = array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('Management Console'),
|
||||
'#markup' => backup_migrate_nodesquirrel_get_manage_link($destination),
|
||||
'#description' => t('You can use the NodeSquirrel management console to add and edit your sites, reset your secret key, download and delete backups, and modify your NodeSquirrel account.'),
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
function backup_migrate_nodesquirrel_schedule_settings_form($destination, $status) {
|
||||
backup_migrate_include('sources', 'schedules', 'profiles');
|
||||
|
||||
// If the schedule has been overriden it must be edited in the schedule tab.
|
||||
$schedule = backup_migrate_crud_get_item('schedule', 'nodesquirrel');
|
||||
|
||||
$default = 60*60*24;
|
||||
|
||||
$form = array();
|
||||
$form['nodesquirrel_schedule'] = array(
|
||||
'#type' => 'fieldset',
|
||||
'#title' => t('Backup Schedule'),
|
||||
'#description' => t('Set up a schedule to back up your site to NodeSquirrel. You can customize this schedule or add additional schedules in the !schedule.', array('!schedule' => l(t('Schedules tab'), BACKUP_MIGRATE_MENU_PATH . '/schedule'), '!cron' => l(t('cron'), 'http://drupal.org/cron'))),
|
||||
);
|
||||
|
||||
$key = 'nodesquirrel_schedule';
|
||||
$form['nodesquirrel_schedule'][$key] = array();
|
||||
$defaults = array(
|
||||
'period' => empty($schedule) ? variable_get('nodesquirrel_schedule', 60*60*24) : $schedule->get('period'),
|
||||
'enabled' => empty($schedule) ? variable_get('nodesquirrel_schedule_enabled', TRUE) : $schedule->get('enabled'),
|
||||
'source_id' => empty($schedule) ? variable_get('nodesquirrel_schedule_source_id', 'db') : $schedule->get('source_id'),
|
||||
'keep' => empty($schedule) ? variable_get('nodesquirrel_schedule_keep', BACKUP_MIGRATE_SMART_DELETE) : $schedule->get('keep'),
|
||||
);
|
||||
|
||||
$form['nodesquirrel_schedule'][$key]['nodesquirrel_schedule_enabled'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Automatically backup to NodeSquirrel'),
|
||||
'#default_value' => $defaults['enabled'],
|
||||
);
|
||||
$form['nodesquirrel_schedule'][$key]['settings'] = array(
|
||||
'#type' => 'backup_migrate_dependent',
|
||||
'#dependencies' => array(
|
||||
'nodesquirrel_schedule_enabled' => TRUE,
|
||||
),
|
||||
);
|
||||
$form['nodesquirrel_schedule'][$key]['settings']['nodesquirrel_schedule_source_id'] = _backup_migrate_get_source_pulldown($defaults['source_id']);
|
||||
$options = array(
|
||||
(60*60) => t('Once an hour'),
|
||||
(60*60*24) => t('Once a day'),
|
||||
(60*60*24*7) => t('Once a week'),
|
||||
);
|
||||
$period = $defaults['period'];
|
||||
if (!isset($options[$period])) {
|
||||
$options[$period] = empty($schedule) ? t('Custom') : $schedule->get('frequency_description');
|
||||
}
|
||||
$form['nodesquirrel_schedule'][$key]['settings']['nodesquirrel_schedule'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Schedule Frequency'),
|
||||
'#options' => $options,
|
||||
'#default_value' => $period,
|
||||
);
|
||||
$form['nodesquirrel_schedule'][$key]['settings']['nodesquirrel_schedule_keep'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Automatically delete old backups'),
|
||||
'#return_value' => $defaults['keep'] == BACKUP_MIGRATE_KEEP_ALL ? BACKUP_MIGRATE_SMART_DELETE : $defaults['keep'],
|
||||
'#default_value' => $defaults['keep'],
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* NodeSquirrel settings form.
|
||||
*/
|
||||
function backup_migrate_nodesquirrel_settings_form($form_state) {
|
||||
_backup_migrate_message_callback('_backup_migrate_message_browser');
|
||||
|
||||
$form = array();
|
||||
|
||||
$key = variable_get('nodesquirrel_secret_key', '');
|
||||
$status = array();
|
||||
if ($destination = backup_migrate_nodesquirrel_get_destination($key)) {
|
||||
$status = $destination->check_limits();
|
||||
}
|
||||
|
||||
$form += backup_migrate_nodesquirrel_info_form();
|
||||
$form += backup_migrate_nodesquirrel_status_form($key, $destination, $status);
|
||||
$form += backup_migrate_nodesquirrel_credentials_settings_form($key, $status);
|
||||
$form += backup_migrate_nodesquirrel_schedule_settings_form($destination, $status);
|
||||
|
||||
return system_settings_form($form);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A destination for sending database backups to the NodeSquirel backup service.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
class backup_migrate_destination_nodesquirrel extends backup_migrate_destination {
|
||||
var $supported_ops = array('scheduled backup', 'manual backup', 'remote backup', 'restore', 'list files', 'configure', 'delete');
|
||||
var $cache_files = TRUE;
|
||||
// Don't generate a metadata file as NodeSquirrel can save metadata natively.
|
||||
var $save_metadata = FALSE;
|
||||
|
||||
/**
|
||||
* Get the destination name. Provide a default.
|
||||
*/
|
||||
function get_name() {
|
||||
if (empty($this->name)) {
|
||||
return t('NodeSquirrel');
|
||||
}
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the menu items for manipulating this type.
|
||||
*/
|
||||
function get_menu_items() {
|
||||
$items = array();
|
||||
$path = BACKUP_MIGRATE_MENU_PATH . '/nodesquirrel';
|
||||
$items[$path] = array(
|
||||
'title' => t('NodeSquirrel'),
|
||||
'page callback' => 'backup_migrate_menu_callback',
|
||||
'page arguments' => array('destinations.nodesquirrel', 'backup_migrate_nodesquirrel_settings'),
|
||||
'access arguments' => array('administer backup and migrate'),
|
||||
'weight' => 10,
|
||||
'type' => MENU_LOCAL_TASK,
|
||||
);
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare any mysql databases defined in the settings.php file as a possible destination.
|
||||
*/
|
||||
function destinations() {
|
||||
$out = array();
|
||||
$out['nodesquirrel'] = backup_migrate_create_destination('nodesquirrel', array('machine_name' => 'nodesquirrel'));
|
||||
if ($secret_key = variable_get('nodesquirrel_secret_key', '')) {
|
||||
$out['nodesquirrel']->settings['secret_key'] = $secret_key;
|
||||
}
|
||||
else {
|
||||
$out['nodesquirrel']->supported_ops = array('manual backup', 'remote backup');
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a form to be completed before saving to the destination can be complete.
|
||||
*/
|
||||
function before_backup_form($settings) {
|
||||
$form = array();
|
||||
$key = $this->settings('secret_key');
|
||||
if (empty($key)) {
|
||||
$form += backup_migrate_nodesquirrel_info_form();
|
||||
$form += backup_migrate_nodesquirrel_credentials_settings_form();
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the before_backup_form form.
|
||||
*/
|
||||
function before_backup_form_validate($settings, $form, $form_state) {
|
||||
if (isset($form_state['values']['nodesquirrel_secret_key'])) {
|
||||
$key = trim($form_state['values']['nodesquirrel_secret_key']);
|
||||
if ($error = $this->vaidate_key($key)) {
|
||||
form_set_error('secret_key', $error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the before_backup_form form.
|
||||
*/
|
||||
function before_backup_form_submit($settings, $form, $form_state) {
|
||||
if (isset($form_state['values']['nodesquirrel_secret_key'])) {
|
||||
$this->settings['secret_key'] = $form_state['values']['nodesquirrel_secret_key'];
|
||||
variable_set('nodesquirrel_secret_key', $this->settings['secret_key']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save to the NodeSquirrel destination.
|
||||
*/
|
||||
function save_file($file, $settings) {
|
||||
if ($destination = $this->_get_destination()) {
|
||||
srand((double)microtime()*1000000);
|
||||
|
||||
$filename = $file->filename();
|
||||
$filesize = filesize($file->filepath());
|
||||
$ticket = $this->_xmlrpc('backups.getUploadTicket', array($destination, $filename, $filesize, $file->file_info));
|
||||
|
||||
if ($ticket) {
|
||||
|
||||
$url = $ticket['url'];
|
||||
|
||||
// If the ticket requires authentication add our username/password to the url.
|
||||
if (!empty($ticket['auth']) && $ticket['auth'] = 'basic') {
|
||||
$parts = parse_url($ticket['url']);
|
||||
list($parts['user'], $parts['pass']) = $this->get_user_pass();
|
||||
$url = $this->glue_url($parts, FALSE);
|
||||
}
|
||||
|
||||
$out = $this->_post_file($url, 'POST', $ticket['params'], $file);
|
||||
|
||||
if ($out->code == 200) {
|
||||
// Confirm the upload.
|
||||
$confirm = $this->_xmlrpc('backups.confirmUpload', array($destination, $filename, $filesize));
|
||||
|
||||
if ($confirm['success']) {
|
||||
// Set a message with a link to the manage console.
|
||||
_backup_migrate_message('Your backup has been saved to your NodeSquirrel account. View it at !account', array('!account' => backup_migrate_nodesquirrel_get_manage_link($this)));
|
||||
return $file;
|
||||
}
|
||||
else {
|
||||
_backup_migrate_message('The backup file never made it to the NodeSquirrel backup server. There may have been a network problem. Please try again later');
|
||||
}
|
||||
}
|
||||
else {
|
||||
$error = !empty($out->headers['x-bams-error']) ? $out->headers['x-bams-error'] : $out->error;
|
||||
_backup_migrate_message('The NodeSquirrel server returned the following error: %err', array('%err' => $error), 'error');
|
||||
}
|
||||
}
|
||||
else if ($err = xmlrpc_error()) {
|
||||
// XMLRPC errors are already handled by the server function below.
|
||||
}
|
||||
else {
|
||||
_backup_migrate_message('The NodeSquirrel server refused the backup but did not specify why. Maybe the server is down.');
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load from the NodeSquirrel destination.
|
||||
*/
|
||||
function load_file($file_id) {
|
||||
if ($destination = $this->_get_destination()) {
|
||||
backup_migrate_include('files');
|
||||
$file = new backup_file(array('filename' => $file_id));
|
||||
|
||||
$ticket = $this->_xmlrpc('backups.getDownloadTicket', array($destination, $file_id));
|
||||
|
||||
if ($ticket && $url = $ticket['url']) {
|
||||
// If the ticket requires authentication add our username/password to the url.
|
||||
if (!empty($ticket['auth']) && $ticket['auth'] = 'basic') {
|
||||
$parts = parse_url($ticket['url']);
|
||||
$parts['user'] = @$this->dest_url['user'];
|
||||
$parts['pass'] = @$this->dest_url['pass'];
|
||||
$url = $this->glue_url($parts, FALSE);
|
||||
}
|
||||
|
||||
$out = drupal_http_request($url);
|
||||
|
||||
if ($out->code == 200) {
|
||||
file_put_contents($file->filepath(), $out->data);
|
||||
return $file;
|
||||
}
|
||||
else {
|
||||
$error = !empty($out->headers['x-bams-error']) ? $out->headers['x-bams-error'] : $out->error;
|
||||
_backup_migrate_message('The server returned the following error: %err', array('%err' => $error), 'error');
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete from the NodeSquirrel destination.
|
||||
*/
|
||||
function delete_file($file_id) {
|
||||
if ($destination = $this->_get_destination()) {
|
||||
$result = $this->_xmlrpc('backups.deleteFile', array($destination, $file_id));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List the files in the remote destination.
|
||||
*/
|
||||
function _list_files() {
|
||||
$files = array();
|
||||
backup_migrate_include('files');
|
||||
|
||||
if ($destination = $this->_get_destination()) {
|
||||
$file_list = $this->_xmlrpc('backups.listFiles', array($destination));
|
||||
foreach ((array)$file_list as $file) {
|
||||
$files[$file['filename']] = new backup_file($file);
|
||||
}
|
||||
}
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* List the files in the remote destination.
|
||||
*/
|
||||
function check_limits() {
|
||||
if (empty($this->limits)) {
|
||||
$this->limits = $this->_xmlrpc('backups.getLimits', array($this->_get_destination()));
|
||||
}
|
||||
return $this->limits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a destination is valid.
|
||||
*/
|
||||
function confirm_destination() {
|
||||
return $this->check_limits();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this destination.
|
||||
*/
|
||||
function edit_form() {
|
||||
$form = parent::edit_form();
|
||||
|
||||
// If this is a new destination but the default NS destination has not been created yet,
|
||||
// redirect to the NS config screen.
|
||||
if (!$this->get_id() && !variable_get('nodesquirrel_secret_key', '')) {
|
||||
drupal_goto(BACKUP_MIGRATE_MENU_PATH . '/nodesquirrel');
|
||||
}
|
||||
|
||||
$form['settings'] = array('#tree' => TRUE);
|
||||
$activate_link = backup_migrate_nodesquirrel_get_activate_link();
|
||||
|
||||
// Retrieve the key from the settings or get it from the get string if this is an auto-config action.
|
||||
$key = $this->settings('secret_key');
|
||||
if (!empty($_GET['key']) && preg_match(NODESQUIRREL_SECRET_KEY_PATTERN, $_GET['key'])) {
|
||||
$key = $_GET['key'];
|
||||
}
|
||||
|
||||
$form['settings']['secret_key'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Secret Key'),
|
||||
'#default_value' => $key,
|
||||
);
|
||||
$form['settings']['location'] = array('#type' => 'value', '#value' => '');
|
||||
|
||||
$form['settings']['secret_key_help'] = array(
|
||||
'#type' => 'item',
|
||||
'#title' => t('Need a Secret Key?'),
|
||||
'#markup' => t('Visit !nodesquirrel.', array('!nodesquirrel' => $activate_link)),
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the configuration form. Glue the url together and add the old password back if a new one was not specified.
|
||||
*/
|
||||
function edit_form_validate($form, &$form_state) {
|
||||
$key = trim($form_state['values']['settings']['secret_key']);
|
||||
if ($error = $this->vaidate_key($key)) {
|
||||
form_set_error('secret_key', $error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a secret key. Returns error text if the key is invalid.
|
||||
*/
|
||||
function vaidate_key($key) {
|
||||
$error = FALSE;
|
||||
if ($key) {
|
||||
if (!preg_match(NODESQUIRREL_SECRET_KEY_PATTERN, $key)) {
|
||||
return 'The secret key you entered is not the right format. Please make sure you copy it exactly.';
|
||||
}
|
||||
$this->settings['secret_key'] = check_plain($key);
|
||||
|
||||
$limits = $this->check_limits();
|
||||
|
||||
if (!$limits) {
|
||||
$err = xmlrpc_error();
|
||||
if (!empty($err->code) && $err->code == '401') {
|
||||
return 'Could not log in to the NodeSquirrel server. Please check that your secret key is correct.';
|
||||
}
|
||||
else {
|
||||
return 'Your site could not be found on NodeSquirrel. Please check that your secret key is correct.';
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return 'You must enter a NodeSquirrel secret key.';
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the configuration form. Glue the url together and add the old password back if a new one was not specified.
|
||||
*/
|
||||
function edit_form_submit($form, &$form_state) {
|
||||
$form_state['values']['secret_key'] = check_plain($form_state['values']['settings']['secret_key']);
|
||||
parent::edit_form_submit($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the destination id or warn the user that it has not been set.
|
||||
*/
|
||||
function _get_destination($warn = TRUE) {
|
||||
list($id, $key) = $this->get_user_pass();
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the destination id or warn the user that it has not been set.
|
||||
*/
|
||||
function _get_private_key($warn = TRUE) {
|
||||
list($id, $key) = $this->get_user_pass();
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Break the secret key into the public/private key (user/pass).
|
||||
*/
|
||||
function get_user_pass() {
|
||||
$key = $this->settings('secret_key');
|
||||
// The username is the first 32 chars.
|
||||
$user = substr($key, 0, 32);
|
||||
// The pass is the last 32 chars. There may be a separating character.
|
||||
$pass = substr($key, strlen($key) - 32);
|
||||
return array($user, $pass);
|
||||
}
|
||||
|
||||
function get_display_location() {
|
||||
return t('NodeSquirrel.com');
|
||||
}
|
||||
|
||||
function add_scheme($url) {
|
||||
return 'https://' . $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this destination.
|
||||
*/
|
||||
function _xmlrpc($method, $args = array()) {
|
||||
// Retrieve the severs or read them from a stored variable.
|
||||
$servers = $this->_get_endpoints();
|
||||
|
||||
// Do the actual call.
|
||||
return $this->__xmlrpc($method, $args, $servers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this destination.
|
||||
*/
|
||||
function __xmlrpc($method, $args, $servers, $retry = 3) {
|
||||
if ($servers && --$retry > 0) {
|
||||
// Add the key authentication arguments if we can.
|
||||
if ($this->_sign_request($args)) {
|
||||
$url = reset($servers);
|
||||
// Try each available server in order.
|
||||
while ($url) {
|
||||
|
||||
$url = $this->add_scheme($url);
|
||||
|
||||
$out = xmlrpc($url, array($method => $args));
|
||||
|
||||
// Check for errors.
|
||||
$err = xmlrpc_error();
|
||||
if ($err && $err->is_error) {
|
||||
switch ($err->code) {
|
||||
case '500':
|
||||
case '503':
|
||||
case '404':
|
||||
// Some sort of server error. Try the next one.
|
||||
$url = next($servers);
|
||||
|
||||
// If we're at the end of the line then try refetching the urls
|
||||
if (!$url) {
|
||||
$servers = $this->_get_endpoints(TRUE, $retry);
|
||||
return $this->__xmlrpc($method, $args, $servers, $retry);
|
||||
}
|
||||
break;
|
||||
case '300':
|
||||
// 'Multiple Choices' means that the existing server list needs to be refreshed.
|
||||
$servers = $this->_get_endpoints(TRUE, $retry);
|
||||
return $this->__xmlrpc($method, $args, $servers, $retry);
|
||||
break;
|
||||
case '401':
|
||||
case '403':
|
||||
// Authentication failed.
|
||||
_backup_migrate_message('Couldn\'t log in to NodeSquirrel. The server error was: %err', array('%err' => $err->message), 'error');
|
||||
return FALSE;
|
||||
break;
|
||||
default:
|
||||
// Some sort of client error. Don't try the next server because it'll probably say the same thing.
|
||||
_backup_migrate_message('The NodeSquirrel server returned the following error: %err', array('%err' => $err->message), 'error');
|
||||
return FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// No error, return the result.
|
||||
else {
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Genrate a hash with a given secret key, timestamp and random value.
|
||||
*/
|
||||
function _get_hash($time, $nonce) {
|
||||
if ($private_key = $this->_get_private_key()) {
|
||||
$message = $time . ':' . $nonce . ':' . $private_key;
|
||||
// Use HMAC-SHA1 to authenticate the call.
|
||||
$hash = base64_encode(
|
||||
pack('H*', sha1((str_pad($private_key, 64, chr(0x00)) ^ (str_repeat(chr(0x5c), 64))) .
|
||||
pack('H*', sha1((str_pad($private_key, 64, chr(0x00)) ^ (str_repeat(chr(0x36), 64))) .
|
||||
$message))))
|
||||
);
|
||||
return $hash;
|
||||
}
|
||||
_backup_migrate_message('You must enter a valid secret key to use NodeSquirrel.', array(), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Genrate a hash with a given secret key, timestamp and random value.
|
||||
*/
|
||||
function _sign_request(&$args) {
|
||||
$nonce = md5(mt_rand());
|
||||
$time = time();
|
||||
$hash = $this->_get_hash($time, $nonce);
|
||||
if ($hash) {
|
||||
array_unshift($args, $nonce);
|
||||
array_unshift($args, $time);
|
||||
array_unshift($args, $hash);
|
||||
return TRUE;
|
||||
}
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the list of servers.
|
||||
*/
|
||||
function _get_endpoints($refresh = FALSE, $retry = 3) {
|
||||
$servers = (array)variable_get('nodesquirrel_endpoint_urls', array());
|
||||
|
||||
// No servers saved or a force refreshr required.
|
||||
if ($refresh || empty($servers)) {
|
||||
$servers = array_unique(array_merge($servers, variable_get('nodesquirrel_default_endpoint_urls', array('api.nodesquirrel.com/services/xmlrpc'))));
|
||||
// Call the get endpoints method but use the default or previous servers to avoid infinite loops.
|
||||
$new_servers = $this->__xmlrpc('backups.getEndpoints', array($this->_get_destination(), 'xmlrpc'), $servers, $retry);
|
||||
if ($new_servers) {
|
||||
variable_set('nodesquirrel_endpoint_urls', $new_servers);
|
||||
$servers = $new_servers;
|
||||
}
|
||||
}
|
||||
return $servers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Post a file via http.
|
||||
*
|
||||
* This looks a lot like a clone of drupal_http_request but it can post a large
|
||||
* file without reading the whole file into memory.
|
||||
*/
|
||||
function _post_file($url, $method = 'GET', $params = array(), $file = NULL, $retry = 3) {
|
||||
global $db_prefix;
|
||||
|
||||
$result = new stdClass();
|
||||
|
||||
// Parse the URL and make sure we can handle the schema.
|
||||
$uri = parse_url($url);
|
||||
|
||||
if ($uri == FALSE) {
|
||||
$result->error = 'unable to parse URL';
|
||||
$result->code = -1001;
|
||||
return $result;
|
||||
}
|
||||
|
||||
if (!isset($uri['scheme'])) {
|
||||
$result->error = 'missing schema';
|
||||
$result->code = -1002;
|
||||
return $result;
|
||||
}
|
||||
|
||||
switch ($uri['scheme']) {
|
||||
case 'http':
|
||||
case 'feed':
|
||||
$port = isset($uri['port']) ? $uri['port'] : 80;
|
||||
$host = $uri['host'] . ($port != 80 ? ':'. $port : '');
|
||||
$fp = @fsockopen($uri['host'], $port, $errno, $errstr, 15);
|
||||
break;
|
||||
case 'https':
|
||||
// Note: Only works for PHP 4.3 compiled with OpenSSL.
|
||||
$port = isset($uri['port']) ? $uri['port'] : 443;
|
||||
$host = $uri['host'] . ($port != 443 ? ':'. $port : '');
|
||||
$fp = @fsockopen('ssl://'. $uri['host'], $port, $errno, $errstr, 20);
|
||||
break;
|
||||
default:
|
||||
$result->error = 'invalid schema '. $uri['scheme'];
|
||||
$result->code = -1003;
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Make sure the socket opened properly.
|
||||
if (!$fp) {
|
||||
// When a network error occurs, we use a negative number so it does not
|
||||
// clash with the HTTP status codes.
|
||||
$result->code = -$errno;
|
||||
$result->error = trim($errstr);
|
||||
|
||||
// Mark that this request failed. This will trigger a check of the web
|
||||
// server's ability to make outgoing HTTP requests the next time that
|
||||
// requirements checking is performed.
|
||||
// @see system_requirements()
|
||||
variable_set('drupal_http_request_fails', TRUE);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Construct the path to act on.
|
||||
$path = isset($uri['path']) ? $uri['path'] : '/';
|
||||
if (isset($uri['query'])) {
|
||||
$path .= '?'. $uri['query'];
|
||||
}
|
||||
|
||||
// Prepare the data payload.
|
||||
$boundary = '---------------------------'. substr(md5(rand(0,32000)),0,10);
|
||||
$data_footer = "\r\n--$boundary--\r\n";
|
||||
|
||||
$data_header = '';
|
||||
foreach ($params as $key => $value) {
|
||||
$data_header .="--$boundary\r\n";
|
||||
$data_header .= "Content-Disposition: form-data; name=\"".$key."\"\r\n";
|
||||
$data_header .= "\r\n".$value."\r\n";
|
||||
$data_header .="--$boundary\r\n";
|
||||
}
|
||||
|
||||
// Add the file header to the post payload.
|
||||
$data_header .="--$boundary\r\n";
|
||||
$data_header .= "Content-Disposition: form-data; name=\"file\"; filename=\"". $file->filename() ."\"\r\n";
|
||||
$data_header .= "Content-Type: application/octet-stream;\r\n";
|
||||
$data_header .= "\r\n";
|
||||
|
||||
// Calculate the content length.
|
||||
$content_length = strlen($data_header . $data_footer) + filesize($file->filepath());
|
||||
|
||||
//file_get_contents($file->filepath()));
|
||||
|
||||
// Create HTTP request.
|
||||
$defaults = array(
|
||||
// RFC 2616: "non-standard ports MUST, default ports MAY be included".
|
||||
// We don't add the port to prevent from breaking rewrite rules checking the
|
||||
// host that do not take into account the port number.
|
||||
'Host' => "Host: $host",
|
||||
'Content-type' => "Content-type: multipart/form-data, boundary=$boundary",
|
||||
'User-Agent' => 'User-Agent: NodeSquirrel Client/1.x (+http://www.nodesquirrel.com) (Drupal '. VERSION .'; Backup and Migrate 2.x)',
|
||||
'Content-Length' => 'Content-Length: '. $content_length
|
||||
);
|
||||
|
||||
// If the server url has a user then attempt to use basic authentication
|
||||
if (isset($uri['user'])) {
|
||||
$defaults['Authorization'] = 'Authorization: Basic '. base64_encode($uri['user'] . (!empty($uri['pass']) ? ":". $uri['pass'] : ''));
|
||||
}
|
||||
|
||||
$request = $method .' '. $path ." HTTP/1.0\r\n";
|
||||
$request .= implode("\r\n", $defaults);
|
||||
$request .= "\r\n\r\n";
|
||||
$result->request = $request;
|
||||
|
||||
// Write the headers and start of the headers
|
||||
fwrite($fp, $request);
|
||||
fwrite($fp, $data_header);
|
||||
|
||||
// Copy the file 512k at a time to prevent memory issues.
|
||||
if ($fp_in = fopen($file->filepath(), 'rb')) {
|
||||
while (!feof($fp_in)) {
|
||||
fwrite($fp, fread($fp_in, 1024 * 512));
|
||||
}
|
||||
$success = TRUE;
|
||||
}
|
||||
@fclose($fp_in);
|
||||
|
||||
// Finish the write.
|
||||
fwrite($fp, $data_footer);
|
||||
|
||||
// Fetch response.
|
||||
$response = '';
|
||||
while (!feof($fp) && $chunk = fread($fp, 1024)) {
|
||||
$response .= $chunk;
|
||||
}
|
||||
fclose($fp);
|
||||
|
||||
if (variable_get('debug_http_request', FALSE)) {
|
||||
drupal_debug(date('r'));
|
||||
drupal_debug($request);
|
||||
drupal_debug($response);
|
||||
}
|
||||
|
||||
// Parse response.
|
||||
list($split, $result->data) = explode("\r\n\r\n", $response, 2);
|
||||
$split = preg_split("/\r\n|\n|\r/", $split);
|
||||
|
||||
list($protocol, $code, $status_message) = explode(' ', trim(array_shift($split)), 3);
|
||||
$result->protocol = $protocol;
|
||||
$result->status_message = $status_message;
|
||||
|
||||
$result->headers = array();
|
||||
|
||||
// Parse headers.
|
||||
while ($line = trim(array_shift($split))) {
|
||||
list($header, $value) = explode(':', $line, 2);
|
||||
if (isset($result->headers[$header]) && $header == 'Set-Cookie') {
|
||||
// RFC 2109: the Set-Cookie response header comprises the token Set-
|
||||
// Cookie:, followed by a comma-separated list of one or more cookies.
|
||||
$result->headers[$header] .= ','. trim($value);
|
||||
}
|
||||
else {
|
||||
$result->headers[$header] = trim($value);
|
||||
}
|
||||
}
|
||||
|
||||
$responses = array(
|
||||
100 => 'Continue', 101 => 'Switching Protocols',
|
||||
200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content',
|
||||
300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect',
|
||||
400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed',
|
||||
500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported'
|
||||
);
|
||||
// RFC 2616 states that all unknown HTTP codes must be treated the same as the
|
||||
// base code in their class.
|
||||
if (!isset($responses[$code])) {
|
||||
$code = floor($code / 100) * 100;
|
||||
}
|
||||
|
||||
switch ($code) {
|
||||
case 200: // OK
|
||||
case 304: // Not modified
|
||||
break;
|
||||
case 301: // Moved permanently
|
||||
case 302: // Moved temporarily
|
||||
case 307: // Moved temporarily
|
||||
$location = $result->headers['Location'];
|
||||
|
||||
if ($retry) {
|
||||
$result = drupal_http_request($result->headers['Location'], $headers, $method, $data, --$retry);
|
||||
$result->redirect_code = $result->code;
|
||||
}
|
||||
$result->redirect_url = $location;
|
||||
|
||||
break;
|
||||
default:
|
||||
$result->error = $status_message;
|
||||
}
|
||||
|
||||
$result->code = $code;
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
184
sites/all/modules/backup_migrate/includes/destinations.s3.inc
Normal file
184
sites/all/modules/backup_migrate/includes/destinations.s3.inc
Normal file
|
@ -0,0 +1,184 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Functions to handle the s3 backup destination.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A destination for sending database backups to an s3 server.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
class backup_migrate_destination_s3 extends backup_migrate_destination_remote {
|
||||
var $supported_ops = array('scheduled backup', 'manual backup', 'remote backup', 'restore', 'list files', 'configure', 'delete');
|
||||
var $s3 = NULL;
|
||||
var $cache_files = TRUE;
|
||||
|
||||
|
||||
/**
|
||||
* Save to to the s3 destination.
|
||||
*/
|
||||
function _save_file($file, $settings) {
|
||||
if ($s3 = $this->s3_object()) {
|
||||
$path = $file->filename();
|
||||
if ($s3->putObject($s3->inputFile($file->filepath(), FALSE), $this->get_bucket(), $this->remote_path($file->filename()), S3::ACL_PRIVATE)) {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load from the s3 destination.
|
||||
*/
|
||||
function load_file($file_id) {
|
||||
backup_migrate_include('files');
|
||||
$file = new backup_file(array('filename' => $file_id));
|
||||
if ($s3 = $this->s3_object()) {
|
||||
$data = $s3->getObject($this->get_bucket(), $this->remote_path($file_id), $file->filepath());
|
||||
if (!$data->error) {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete from the s3 destination.
|
||||
*/
|
||||
function _delete_file($file_id) {
|
||||
if ($s3 = $this->s3_object()) {
|
||||
$s3->deleteObject($this->get_bucket(), $this->remote_path($file_id));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List all files from the s3 destination.
|
||||
*/
|
||||
function _list_files() {
|
||||
backup_migrate_include('files');
|
||||
$files = array();
|
||||
if ($s3 = $this->s3_object()) {
|
||||
$s3_files = $s3->getBucket($this->get_bucket(), $this->get_subdir());
|
||||
foreach ((array)$s3_files as $id => $file) {
|
||||
$info = array(
|
||||
'filename' => $this->local_path($file['name']),
|
||||
'filesize' => $file['size'],
|
||||
'filetime' => $file['time'],
|
||||
);
|
||||
$files[$info['filename']] = new backup_file($info);
|
||||
}
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function edit_form() {
|
||||
// Check for the library.
|
||||
$this->s3_object();
|
||||
|
||||
$form = parent::edit_form();
|
||||
$form['scheme']['#type'] = 'value';
|
||||
$form['scheme']['#value'] = 'https';
|
||||
$form['host']['#type'] = 'value';
|
||||
$form['host']['#value'] = 's3.amazonaws.com';
|
||||
|
||||
$form['path']['#title'] = 'S3 Bucket';
|
||||
$form['path']['#default_value'] = $this->get_bucket();
|
||||
$form['path']['#description'] = 'This bucket must already exist. It will not be created for you.';
|
||||
|
||||
$form['user']['#title'] = 'Access Key ID';
|
||||
$form['pass']['#title'] = 'Secret Access Key';
|
||||
|
||||
$form['subdir'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Subdirectory'),
|
||||
'#default_value' => $this->get_subdir(),
|
||||
'#weight' => 25
|
||||
);
|
||||
$form['settings']['#weight'] = 50;
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the form for the settings for the s3 destination.
|
||||
*/
|
||||
function edit_form_submit($form, &$form_state) {
|
||||
// Append the subdir onto the path.
|
||||
|
||||
if (!empty($form_state['values']['subdir'])) {
|
||||
$form_state['values']['path'] .= '/'. trim($form_state['values']['subdir'], '/');
|
||||
}
|
||||
parent::edit_form_submit($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a filepath with the correct prefix.
|
||||
*/
|
||||
function remote_path($path) {
|
||||
if ($subdir = $this->get_subdir()) {
|
||||
$path = $subdir .'/'. $path;
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a filepath with the correct prefix.
|
||||
*/
|
||||
function local_path($path) {
|
||||
if ($subdir = $this->get_subdir()) {
|
||||
$path = str_replace($subdir .'/', '', $path);
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bucket which is the first part of the path.
|
||||
*/
|
||||
function get_bucket() {
|
||||
$parts = explode('/', @$this->dest_url['path']);
|
||||
return $parts[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bucket which is the first part of the path.
|
||||
*/
|
||||
function get_subdir() {
|
||||
// Support the older style of subdir saving.
|
||||
if ($subdir = $this->settings('subdir')) {
|
||||
return $subdir;
|
||||
}
|
||||
$parts = explode('/', @$this->dest_url['path']);
|
||||
array_shift($parts);
|
||||
return implode('/', array_filter($parts));
|
||||
}
|
||||
|
||||
function s3_object() {
|
||||
// Try to use libraries module if available to find the path.
|
||||
if (function_exists('libraries_get_path')) {
|
||||
$library_paths[] = libraries_get_path('s3-php5-curl');
|
||||
}
|
||||
else {
|
||||
$library_paths[] = 'sites/all/libraries/s3-php5-curl';
|
||||
}
|
||||
$library_paths[] = drupal_get_path('module', 'backup_migrate') . '/includes/s3-php5-curl';
|
||||
$library_paths[] = drupal_get_path('module', 'backup_migrate') . '/includes';
|
||||
|
||||
foreach($library_paths as $path) {
|
||||
if (file_exists($path . '/S3.php')) {
|
||||
require_once $path . '/S3.php';
|
||||
if (!$this->s3 && !empty($this->dest_url['user'])) {
|
||||
$this->s3 = new S3($this->dest_url['user'], $this->dest_url['pass']);
|
||||
}
|
||||
return $this->s3;
|
||||
}
|
||||
}
|
||||
drupal_set_message(t('Due to drupal.org code hosting policies, the S3 library needed to use an S3 destination is no longer distributed with this module. You must download the library from !link and place it in one of these locations: %locations.', array('%locations' => implode(', ', $library_paths), '!link' => l('http://undesigned.org.za/2007/10/22/amazon-s3-php-class', 'http://undesigned.org.za/2007/10/22/amazon-s3-php-class'))), 'error', FALSE);
|
||||
return NULL;
|
||||
}
|
||||
}
|
564
sites/all/modules/backup_migrate/includes/files.inc
Normal file
564
sites/all/modules/backup_migrate/includes/files.inc
Normal file
|
@ -0,0 +1,564 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* General file handling code for Backup and Migrate.
|
||||
*/
|
||||
|
||||
define('BACKUP_MIGRATE_FILENAME_MAXLENGTH', 255);
|
||||
|
||||
/**
|
||||
* Add a file to the temporary files list for deletion when we're done.
|
||||
*/
|
||||
function backup_migrate_temp_files_add($filepath = NULL) {
|
||||
$files = &drupal_static('backup_migrate_temp_files_add', array());
|
||||
if (!$filepath) {
|
||||
return $files;
|
||||
}
|
||||
else {
|
||||
$files[] = $filepath;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all temporary files.
|
||||
*/
|
||||
function _backup_migrate_temp_files_delete() {
|
||||
if (variable_get('backup_migrate_cleanup_temp_files', TRUE)) {
|
||||
// Delete the temp files created during this run.
|
||||
foreach (backup_migrate_temp_files_add() as $file) {
|
||||
if (file_exists($file) && is_writable($file)) {
|
||||
_backup_migrate_temp_files_delete_file($file);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete temp files abandoned for 6 or more hours.
|
||||
$dir = file_directory_temp();
|
||||
$expire = time() - variable_get('backup_migrate_cleanup_time', 21600);
|
||||
if (file_exists($dir) && is_dir($dir) && is_readable($dir) && $handle = opendir($dir)) {
|
||||
while (FALSE !== ($file = @readdir($handle))) {
|
||||
// Delete 'backup_migrate_' files in the temp directory that are older than the expire time.
|
||||
// We should only attempt to delete writable files to prevent errors in shared environments.
|
||||
// This could still cause issues in shared environments with poorly configured file permissions.
|
||||
if (strpos($file, 'backup_migrate_') === 0 && is_writable("$dir/$file") && @filectime("$dir/$file") < $expire) {
|
||||
_backup_migrate_temp_files_delete_file("$dir/$file");
|
||||
}
|
||||
}
|
||||
closedir($handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a temporary file or folder
|
||||
*/
|
||||
function _backup_migrate_temp_files_delete_file($file) {
|
||||
if (file_exists($file) && (is_writable($file) || is_link($file))) {
|
||||
if (!is_link($file) && is_dir($file) && is_readable($file) && $handle = opendir($file)) {
|
||||
$dir = $file;
|
||||
while (FALSE !== ($file = @readdir($handle))) {
|
||||
if ($file != '..' && $file != '.') {
|
||||
_backup_migrate_temp_files_delete_file("$dir/$file");
|
||||
}
|
||||
}
|
||||
rmdir($dir);
|
||||
}
|
||||
else {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Move files recursively.
|
||||
*/
|
||||
function _backup_migrate_move_files($from, $to) {
|
||||
if (is_readable($from)) {
|
||||
if (is_dir($from) && is_readable($from) && $handle = opendir($from)) {
|
||||
if (!file_exists($to)) {
|
||||
mkdir($to);
|
||||
}
|
||||
while (FALSE !== ($file = @readdir($handle))) {
|
||||
if ($file != '..' && $file != '.') {
|
||||
_backup_migrate_move_files("$from/$file", "$to/$file");
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
rename($from, $to);
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create a temporary directory.
|
||||
*/
|
||||
function backup_migrate_temp_directory() {
|
||||
$tmp = realpath(file_directory_temp());
|
||||
// Check the writability of the temp directory.
|
||||
if (!is_writable(realpath(file_directory_temp()))) {
|
||||
_backup_migrate_message('Your temporary directory %tmp is not writable. Backup and migrate needs to be able to create temporary files.', array('%tmp' => $tmp), 'error');
|
||||
}
|
||||
|
||||
// Use a full path so that the files can be deleted during the shutdown function if needed.
|
||||
$file = $tmp .'/'. uniqid('backup_migrate_');
|
||||
mkdir($file);
|
||||
backup_migrate_temp_files_add($file);
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of backup filetypes.
|
||||
*/
|
||||
function _backup_migrate_filetypes() {
|
||||
backup_migrate_include('filters');
|
||||
|
||||
$out = backup_migrate_filters_file_types();
|
||||
|
||||
foreach ($out as $key => $info) {
|
||||
$out[$key]['id'] = empty($info['id']) ? $key : $info['id'];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjust the length of a filename to allow for a string to be appended,
|
||||
* staying within the maximum filename limit.
|
||||
*/
|
||||
function _backup_migrate_filename_append_prepare($filename, $append_str) {
|
||||
$max_name_len = BACKUP_MIGRATE_FILENAME_MAXLENGTH - drupal_strlen($append_str);
|
||||
if (drupal_strlen($filename) > $max_name_len) {
|
||||
$filename = drupal_substr($filename, 0, $max_name_len);
|
||||
}
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a filename using token and some cleaning.
|
||||
*/
|
||||
function _backup_migrate_construct_filename($settings) {
|
||||
// Get the raw filename from the settings.
|
||||
$filename = $settings->filename;
|
||||
|
||||
// Replace any tokens
|
||||
if (module_exists('token') && function_exists('token_replace')) {
|
||||
$filename = token_replace($filename);
|
||||
}
|
||||
|
||||
// Remove illegal characters
|
||||
$filename = _backup_migrate_clean_filename($filename);
|
||||
|
||||
// Generate a timestamp if needed.
|
||||
$timestamp = '';
|
||||
if ($settings->append_timestamp && $settings->timestamp_format) {
|
||||
$timestamp = format_date(time(), 'custom', $settings->timestamp_format);
|
||||
}
|
||||
|
||||
// Trim to length if needed to allow the timestamp to fit into the max filename.
|
||||
$filename = _backup_migrate_filename_append_prepare($filename, $timestamp);
|
||||
$filename .= '-' . $timestamp;
|
||||
$filename = trim($filename, '-');
|
||||
|
||||
// If there is no filename, then just call it untitled.
|
||||
if (drupal_strlen($filename) == 0) {
|
||||
$filename = 'untitled';
|
||||
}
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a default filename using the site's name.
|
||||
*/
|
||||
function _backup_migrate_default_filename() {
|
||||
if (module_exists('token')) {
|
||||
return '[site:name]';
|
||||
}
|
||||
else {
|
||||
// Cleaning the string isn't strictly necessary but it looks better in the settings field.
|
||||
return _backup_migrate_clean_filename(variable_get('site_name', "backup_migrate"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up a filename to remove unsafe characters.
|
||||
*/
|
||||
function _backup_migrate_clean_filename($filename) {
|
||||
$filename = preg_replace("/[^a-zA-Z0-9\.\-_]/", "", $filename);
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* An output buffer callback which simply throws away the buffer instead of sending it to the browser.
|
||||
*/
|
||||
function _backup_migrate_file_dispose_buffer($buffer) {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A backup file which allows for saving to and reading from the server.
|
||||
*/
|
||||
class backup_file {
|
||||
var $file_info = array();
|
||||
var $type = array();
|
||||
var $ext = array();
|
||||
var $path = "";
|
||||
var $name = "";
|
||||
var $handle = NULL;
|
||||
|
||||
/**
|
||||
* Construct a file object given a file path, or create a temp file for writing.
|
||||
*/
|
||||
function __construct($params = array()) {
|
||||
if (isset($params['filepath']) && file_exists($params['filepath'])) {
|
||||
$this->set_filepath($params['filepath']);
|
||||
}
|
||||
else {
|
||||
$this->set_file_info($params);
|
||||
$this->temporary_file();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file_id if the file has been saved to a destination.
|
||||
*/
|
||||
function file_id() {
|
||||
// The default file_id is the filename. Destinations can override the file_id if needed.
|
||||
return isset($this->file_info['file_id']) ? $this->file_info['file_id'] : $this->filename();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current filepath.
|
||||
*/
|
||||
function filepath() {
|
||||
return drupal_realpath($this->path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the final filename.
|
||||
*/
|
||||
function filename($name = NULL) {
|
||||
if ($name) {
|
||||
$this->name = $name;
|
||||
}
|
||||
$extension_str = '.' . $this->extension();
|
||||
$this->name = _backup_migrate_filename_append_prepare($this->name, $extension_str);
|
||||
return $this->name . $extension_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current filepath.
|
||||
*/
|
||||
function set_filepath($path) {
|
||||
$this->path = $path;
|
||||
$params = array(
|
||||
'filename' => basename($path),
|
||||
'file_id' => basename($path)
|
||||
);
|
||||
if (file_exists($path)) {
|
||||
$params['filesize'] = filesize($path);
|
||||
$params['filetime'] = filectime($path);
|
||||
}
|
||||
$this->set_file_info($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get one or all pieces of info for the file.
|
||||
*/
|
||||
function info($key = NULL) {
|
||||
if ($key) {
|
||||
return @$this->file_info[$key];
|
||||
}
|
||||
return $this->file_info;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get one or all pieces of info for the file.
|
||||
*/
|
||||
function info_set($key, $value) {
|
||||
$this->file_info[$key] = $value;
|
||||
}
|
||||
/**
|
||||
* Get the file extension.
|
||||
*/
|
||||
function extension() {
|
||||
return implode(".", $this->ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file type.
|
||||
*/
|
||||
function type() {
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file mimetype.
|
||||
*/
|
||||
function mimetype() {
|
||||
return @$this->type['filemime'] ? $this->type['filemime'] : 'application/octet-stream';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file mimetype.
|
||||
*/
|
||||
function type_id() {
|
||||
return @$this->type['id'];
|
||||
}
|
||||
|
||||
function filesize() {
|
||||
if (empty($this->file_info['filesize'])) {
|
||||
$this->calculate_filesize();
|
||||
}
|
||||
return $this->file_info['filesize'];
|
||||
}
|
||||
|
||||
function calculate_filesize() {
|
||||
$this->file_info['filesize'] = '';
|
||||
if (!empty($this->path) && file_exists($this->path)) {
|
||||
$this->file_info['filesize'] = filesize($this->path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Can this file be used to backup to.
|
||||
*/
|
||||
function can_backup() {
|
||||
return @$this->type['backup'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Can this file be used to restore to.
|
||||
*/
|
||||
function can_restore() {
|
||||
return @$this->type['restore'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Can this file be used to restore to.
|
||||
*/
|
||||
function is_recognized_type() {
|
||||
return @$this->type['restore'] || @$this->type['backup'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a file for reading or writing.
|
||||
*/
|
||||
function open($write = FALSE, $binary = FALSE) {
|
||||
if (!$this->handle) {
|
||||
$path = $this->filepath();
|
||||
|
||||
// Check if the file can be read/written.
|
||||
if ($write && ((file_exists($path) && !is_writable($path)) || !is_writable(dirname($path)))) {
|
||||
_backup_migrate_message('The file %path cannot be written to.', array('%path' => $path), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
if (!$write && !is_readable($path)) {
|
||||
_backup_migrate_message('The file %path cannot be read.', array('%path' => $path), 'error');
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Open the file.
|
||||
$mode = ($write ? "w" : "r") . ($binary ? "b" : "");
|
||||
$this->handle = fopen($path, $mode);
|
||||
return $this->handle;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close a file when we're done reading/writing.
|
||||
*/
|
||||
function close() {
|
||||
fclose($this->handle);
|
||||
$this->handle = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a line to the file.
|
||||
*/
|
||||
function write($data) {
|
||||
if (!$this->handle) {
|
||||
$this->handle = $this->open(TRUE);
|
||||
}
|
||||
if ($this->handle) {
|
||||
fwrite($this->handle, $data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a line from the file.
|
||||
*/
|
||||
function read($size = NULL) {
|
||||
if (!$this->handle) {
|
||||
$this->handle = $this->open();
|
||||
}
|
||||
if ($this->handle && !feof($this->handle)) {
|
||||
return $size ? fread($this->handle, $size) : fgets($this->handle);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data to the file.
|
||||
*/
|
||||
function put_contents($data) {
|
||||
file_put_contents($this->filepath(), $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read data from the file.
|
||||
*/
|
||||
function get_contents() {
|
||||
return file_get_contents($this->filepath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfer file using http to client. Similar to the built in file_transfer,
|
||||
* but it calls module_invoke_all('exit') so that temp files can be deleted.
|
||||
*/
|
||||
function transfer() {
|
||||
$headers = array(
|
||||
array('key' => 'Content-Type', 'value' => $this->mimetype()),
|
||||
array('key' => 'Content-Disposition', 'value' => 'attachment; filename="'. $this->filename() .'"'),
|
||||
);
|
||||
// In some circumstances, web-servers will double compress gzipped files.
|
||||
// This may help aleviate that issue by disabling mod-deflate.
|
||||
if ($this->mimetype() == 'application/x-gzip') {
|
||||
$headers[] = array('key' => 'Content-Encoding', 'value' => 'gzip');
|
||||
}
|
||||
if ($size = $this->info('filesize')) {
|
||||
$headers[] = array('key' => 'Content-Length', 'value' => $size);
|
||||
}
|
||||
|
||||
// Suppress the warning you get when the buffer is empty.
|
||||
@ob_end_clean();
|
||||
|
||||
if ($this->open(FALSE, TRUE)) {
|
||||
foreach ($headers as $header) {
|
||||
// To prevent HTTP header injection, we delete new lines that are
|
||||
// not followed by a space or a tab.
|
||||
// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
|
||||
$header['value'] = preg_replace('/\r?\n(?!\t| )/', '', $header['value']);
|
||||
drupal_add_http_header($header['key'], $header['value']);
|
||||
}
|
||||
// Transfer file in 1024 byte chunks to save memory usage.
|
||||
while ($data = $this->read(1024)) {
|
||||
print $data;
|
||||
}
|
||||
$this->close();
|
||||
|
||||
// Ask devel.module not to print it's footer.
|
||||
$GLOBALS['devel_shutdown'] = FALSE;
|
||||
}
|
||||
else {
|
||||
drupal_not_found();
|
||||
}
|
||||
|
||||
// Start buffering and throw away the results so that errors don't get appended to the file.
|
||||
ob_start('_backup_migrate_file_dispose_buffer');
|
||||
backup_migrate_cleanup();
|
||||
module_invoke_all('exit');
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Push a file extension onto the file and return the previous file path.
|
||||
*/
|
||||
function push_type($extension) {
|
||||
$types = _backup_migrate_filetypes();
|
||||
if ($type = @$types[$extension]) {
|
||||
$this->push_filetype($type);
|
||||
}
|
||||
|
||||
$out = $this->filepath();
|
||||
$this->temporary_file();
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Push a file extension onto the file and return the previous file path.
|
||||
*/
|
||||
function pop_type() {
|
||||
$out = new backup_file(array('filepath' => $this->filepath()));
|
||||
$this->pop_filetype();
|
||||
$this->temporary_file();
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current file type.
|
||||
*/
|
||||
function set_filetype($type) {
|
||||
$this->type = $type;
|
||||
$this->ext = array($type['extension']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current file type.
|
||||
*/
|
||||
function push_filetype($type) {
|
||||
$this->ext[] = $type['extension'];
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pop the current file type.
|
||||
*/
|
||||
function pop_filetype() {
|
||||
array_pop($this->ext);
|
||||
$this->detect_filetype_from_extension();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the file info.
|
||||
*/
|
||||
function set_file_info($file_info) {
|
||||
$this->file_info = $file_info;
|
||||
|
||||
$this->ext = explode('.', @$this->file_info['filename']);
|
||||
// Remove the underscores added to file extensions by Drupal's upload security.
|
||||
foreach ($this->ext as $key => $val) {
|
||||
$this->ext[$key] = trim($val, '_');
|
||||
}
|
||||
$this->filename(array_shift($this->ext));
|
||||
$this->detect_filetype_from_extension();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the filetype info of the given file, or false if the file is not a valid type.
|
||||
*/
|
||||
function detect_filetype_from_extension() {
|
||||
$ext = end($this->ext);
|
||||
$this->type = array();
|
||||
$types = _backup_migrate_filetypes();
|
||||
foreach ($types as $key => $type) {
|
||||
if (trim($ext, "_0123456789") === $type['extension']) {
|
||||
$this->type = $type;
|
||||
$this->type['id'] = $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a temporary file name with path.
|
||||
*/
|
||||
function temporary_file() {
|
||||
$file = drupal_tempnam('temporary://', 'backup_migrate_');
|
||||
// Add the version without the extension. The tempnam function creates this for us.
|
||||
backup_migrate_temp_files_add($file);
|
||||
|
||||
if ($this->extension()) {
|
||||
$file .= '.'. $this->extension();
|
||||
// Add the version with the extension. This is the one we will actually use.
|
||||
backup_migrate_temp_files_add($file);
|
||||
}
|
||||
$this->path = $file;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,249 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* This filter performs tha actual backup or restore operation. Not technically a filter per-se, but it does need to fit in the call chain.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A filter backup or migrate the specified source.
|
||||
*
|
||||
* @ingroup backup_migrate_filters
|
||||
*/
|
||||
class backup_migrate_filter_backup_restore extends backup_migrate_filter {
|
||||
var $op_weights = array('backup' => 0, 'restore' => 0);
|
||||
|
||||
/**
|
||||
* Get the default destinations for this filter.
|
||||
*/
|
||||
function destinations() {
|
||||
$out = array();
|
||||
foreach ($this->_get_destination_types() as $destination) {
|
||||
if (method_exists($destination, 'destinations')) {
|
||||
$out += $destination->destinations();
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default sources for this filter.
|
||||
*/
|
||||
function sources() {
|
||||
$out = array();
|
||||
foreach ($this->_get_source_types() as $type) {
|
||||
if (method_exists($type, 'sources')) {
|
||||
$out += $type->sources();
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default backup settings for this filter.
|
||||
*/
|
||||
function backup_settings_default() {
|
||||
backup_migrate_include('sources');
|
||||
$out = array();
|
||||
foreach (backup_migrate_get_sources() as $source) {
|
||||
$out['sources'][$source->get_id()] = $source->backup_settings_default();
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function backup_settings_form_validate($form, &$form_state) {
|
||||
foreach ($this->_get_destination_types() as $destination) {
|
||||
$destination->backup_settings_form_validate($form, $form_state);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the settings form. Any values returned will be saved.
|
||||
*/
|
||||
function backup_settings_form_submit($form, &$form_state) {
|
||||
foreach ($this->_get_destination_types() as $destination) {
|
||||
$destination->backup_settings_form_submit($form, $form_state);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default restore settings for this filter.
|
||||
*/
|
||||
function restore_settings_default() {
|
||||
$out = array();
|
||||
foreach ($this->_get_destination_types() as $destination) {
|
||||
$out += $destination->restore_settings_default();
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the backup settings for this filter.
|
||||
*/
|
||||
function backup_settings_form($settings) {
|
||||
backup_migrate_include('sources');
|
||||
$out = array('sources' => array(
|
||||
'#tree' => TRUE,
|
||||
));
|
||||
foreach (backup_migrate_get_sources() as $source) {
|
||||
$source_settings = (array)(@$settings['sources'][$source->get_id()]) + $settings;
|
||||
if ($form = $source->backup_settings_form($source_settings)) {
|
||||
$out['sources'][$source->get_id()] = array(
|
||||
'#type' => 'fieldset',
|
||||
'#title' => t('!name Backup Options', array('!name' => $source->get('name'))),
|
||||
"#collapsible" => TRUE,
|
||||
"#collapsed" => TRUE,
|
||||
'#tree' => TRUE,
|
||||
'#parents' => array('filters', 'sources', $source->get_id()),
|
||||
) + $form;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the restore settings for this filter.
|
||||
*/
|
||||
function restore_settings_form($settings) {
|
||||
$form = array();
|
||||
foreach ($this->_get_destination_types() as $destination) {
|
||||
$destination->restore_settings_form($form, $settings);
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the before-backup form for the active sources and destinations.
|
||||
*/
|
||||
function before_action_form($op, $settings) {
|
||||
$form = array();
|
||||
$method = 'before_' . $op . '_form';
|
||||
if ($source = $settings->get_source()) {
|
||||
if (method_exists($source, $method)) {
|
||||
$form += $source->{$method}($settings);
|
||||
}
|
||||
}
|
||||
foreach ($settings->get_destinations() as $destination) {
|
||||
if (method_exists($destination, $method)) {
|
||||
$form += $destination->{$method}($settings);
|
||||
}
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the before-backup form for the active sources and destinations.
|
||||
*/
|
||||
function before_action_form_validate($op, $settings, $form, $form_state) {
|
||||
$method = 'before_' . $op . '_form_validate';
|
||||
foreach ($settings->get_all_locations() as $location) {
|
||||
if (method_exists($location, $method)) {
|
||||
$location->{$method}($settings, $form, $form_state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the before-backup form for the active sources and destinations.
|
||||
*/
|
||||
function before_action_form_submit($op, $settings, $form, $form_state) {
|
||||
$method = 'before_' . $op . '_form_submit';
|
||||
foreach ($settings->get_all_locations() as $location) {
|
||||
if (method_exists($location, $method)) {
|
||||
$location->{$method}($settings, $form, $form_state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file types supported by this destination.
|
||||
*/
|
||||
function file_types() {
|
||||
$types = array();
|
||||
foreach ($this->_get_destination_types() as $destination) {
|
||||
$types += $destination->file_types();
|
||||
}
|
||||
foreach ($this->_get_source_types() as $source) {
|
||||
$types += $source->file_types();
|
||||
}
|
||||
return $types;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup the data from the source specified in the settings.
|
||||
*/
|
||||
function backup($file, $settings) {
|
||||
if ($source = $settings->get_source()) {
|
||||
if (!empty($settings->filters['sources'][$source->get_id()])) {
|
||||
$settings->filters = (array)($settings->filters['sources'][$source->get_id()]) + $settings->filters;
|
||||
}
|
||||
|
||||
$file = $source->backup_to_file($file, $settings);
|
||||
return $file;
|
||||
}
|
||||
backup_migrate_backup_fail("Could not run backup because the source '%source' is missing.", array('%source' => $settings->source_id), $settings);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore the data from to source specified in the settings.
|
||||
*/
|
||||
function restore($file, $settings) {
|
||||
if ($source = $settings->get_source()) {
|
||||
if (!empty($settings->filters['sources'][$source->get_id()])) {
|
||||
$settings->filters = (array)($settings->filters['sources'][$source->get_id()]) + $settings->filters;
|
||||
}
|
||||
$num = $source->restore_from_file($file, $settings);
|
||||
return $num ? $file : FALSE;
|
||||
}
|
||||
backup_migrate_restore_fail("Could not run restore because the source '%source' is missing.", array('%source' => $settings->source_id), $settings);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of dummy destinations representing each of the available destination types.
|
||||
*/
|
||||
function _get_destination_types() {
|
||||
backup_migrate_include('destinations');
|
||||
$destinations = &drupal_static('backup_migrate_filter_backup_restore::_get_destination_types', NULL);
|
||||
if (!is_array($destinations)) {
|
||||
$destinations = array();
|
||||
$types = backup_migrate_get_destination_subtypes();
|
||||
// If no (valid) node type has been provided, display a node type overview.
|
||||
foreach ($types as $key => $type) {
|
||||
// Include the necessary file if specified by the type.
|
||||
if (!empty($type['file'])) {
|
||||
require_once './'. $type['file'];
|
||||
}
|
||||
$destinations[] = new $type['class'](array());
|
||||
}
|
||||
}
|
||||
return $destinations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of dummy destinations representing each of the available source types.
|
||||
*/
|
||||
function _get_source_types() {
|
||||
backup_migrate_include('sources');
|
||||
$sources = &drupal_static('backup_migrate_filter_backup_restore::_get_source_types', NULL);
|
||||
if (!is_array($sources)) {
|
||||
$sources = array();
|
||||
$types = backup_migrate_get_source_subtypes();
|
||||
// If no (valid) node type has been provided, display a node type overview.
|
||||
foreach ($types as $key => $type) {
|
||||
// Include the necessary file if specified by the type.
|
||||
if (!empty($type['file'])) {
|
||||
require_once './'. $type['file'];
|
||||
}
|
||||
$sources[] = new $type['class'](array());
|
||||
}
|
||||
}
|
||||
return $sources;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,298 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* A filter for compressing bckups with zip, gz bzip etc.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A filter for compressing backup files.
|
||||
*
|
||||
* @ingroup backup_migrate_filters
|
||||
*/
|
||||
class backup_migrate_filter_compression extends backup_migrate_filter {
|
||||
var $op_weights = array('backup' => 100, 'restore' => -100);
|
||||
|
||||
/**
|
||||
* This function is called on a backup file after the backup has been completed.
|
||||
*/
|
||||
function backup($file, $settings) {
|
||||
return $this->_backup_migrate_file_compress($file, $settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called on a backup file before importing it.
|
||||
*/
|
||||
function restore($file, $settings) {
|
||||
return $this->_backup_migrate_file_decompress($file, $settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function backup_settings_default() {
|
||||
$options = $this->_backup_migrate_get_compression_form_item_options();
|
||||
return array('compression' => isset($options['gzip']) ? 'gzip' : 'none');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function backup_settings_form($settings) {
|
||||
$form = array();
|
||||
$compression_options = $this->_backup_migrate_get_compression_form_item_options();
|
||||
$form['file']['compression'] = array(
|
||||
"#type" => count($compression_options) > 1 ? "select" : 'value',
|
||||
"#title" => t("Compression"),
|
||||
"#options" => $compression_options,
|
||||
"#default_value" => $settings['compression'],
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of backup filetypes.
|
||||
*/
|
||||
function file_types() {
|
||||
return array(
|
||||
"gzip" => array(
|
||||
"extension" => "gz",
|
||||
"filemime" => "application/x-gzip",
|
||||
"backup" => TRUE,
|
||||
"restore" => TRUE,
|
||||
),
|
||||
"bzip" => array(
|
||||
"extension" => "bz",
|
||||
"filemime" => "application/x-bzip",
|
||||
"backup" => TRUE,
|
||||
"restore" => TRUE,
|
||||
),
|
||||
"bzip2" => array(
|
||||
"extension" => "bz2",
|
||||
"filemime" => "application/x-bzip",
|
||||
"backup" => TRUE,
|
||||
"restore" => TRUE,
|
||||
),
|
||||
"zip" => array(
|
||||
"extension" => "zip",
|
||||
"filemime" => "application/zip",
|
||||
"backup" => TRUE,
|
||||
"restore" => TRUE,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the compression options as an options array for a form item.
|
||||
*/
|
||||
function _backup_migrate_get_compression_form_item_options() {
|
||||
$compression_options = array("none" => t("No Compression"));
|
||||
if (@function_exists("gzencode")) {
|
||||
$compression_options['gzip'] = t("GZip");
|
||||
}
|
||||
if (@function_exists("bzcompress")) {
|
||||
$compression_options['bzip'] = t("BZip");
|
||||
}
|
||||
if (class_exists('ZipArchive')) {
|
||||
$compression_options['zip'] = t("Zip", array(), array('context' => 'compression format'));
|
||||
}
|
||||
return $compression_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gzip encode a file.
|
||||
*/
|
||||
function _backup_migrate_gzip_encode($source, $dest, $level = 9, $settings) {
|
||||
$success = FALSE;
|
||||
// Try command line gzip first.
|
||||
|
||||
if (!empty($settings->filters['use_cli'])) {
|
||||
$success = backup_migrate_exec("gzip -c -$level %input > %dest", array('%input' => $source, '%dest' => $dest, '%level' => $level));
|
||||
}
|
||||
if (!$success && @function_exists("gzopen")) {
|
||||
if (($fp_out = gzopen($dest, 'wb'. $level)) && ($fp_in = fopen($source, 'rb'))) {
|
||||
while (!feof($fp_in)) {
|
||||
gzwrite($fp_out, fread($fp_in, 1024 * 512));
|
||||
}
|
||||
$success = TRUE;
|
||||
}
|
||||
@fclose($fp_in);
|
||||
@gzclose($fp_out);
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gzip decode a file.
|
||||
*/
|
||||
function _backup_migrate_gzip_decode($source, $dest, $settings) {
|
||||
$success = FALSE;
|
||||
|
||||
if (!empty($settings->filters['use_cli'])) {
|
||||
$success = backup_migrate_exec("gzip -d -c %input > %dest", array('%input' => $source, '%dest' => $dest));
|
||||
}
|
||||
|
||||
if (!$success && @function_exists("gzopen")) {
|
||||
if (($fp_out = fopen($dest, 'wb')) && ($fp_in = gzopen($source, 'rb'))) {
|
||||
while (!feof($fp_in)) {
|
||||
fwrite($fp_out, gzread($fp_in, 1024 * 512));
|
||||
}
|
||||
$success = TRUE;
|
||||
}
|
||||
@gzclose($fp_in);
|
||||
@fclose($fp_out);
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bzip encode a file.
|
||||
*/
|
||||
function _backup_migrate_bzip_encode($source, $dest) {
|
||||
$success = FALSE;
|
||||
if (@function_exists("bzopen")) {
|
||||
if (($fp_out = bzopen($dest, 'w')) && ($fp_in = fopen($source, 'rb'))) {
|
||||
while (!feof($fp_in)) {
|
||||
bzwrite($fp_out, fread($fp_in, 1024 * 512));
|
||||
}
|
||||
$success = TRUE;
|
||||
}
|
||||
else {
|
||||
$error = TRUE;
|
||||
}
|
||||
@fclose($fp_in);
|
||||
@bzclose($fp_out);
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bzip decode a file.
|
||||
*/
|
||||
function _backup_migrate_bzip_decode($source, $dest) {
|
||||
$success = FALSE;
|
||||
if (@function_exists("bzopen")) {
|
||||
if (($fp_out = fopen($dest, 'w')) && ($fp_in = bzopen($source, 'r'))) {
|
||||
while (!feof($fp_in)) {
|
||||
fwrite($fp_out, gzread($fp_in, 1024 * 512));
|
||||
}
|
||||
$success = TRUE;
|
||||
}
|
||||
else {
|
||||
$error = TRUE;
|
||||
}
|
||||
@bzclose($fp_in);
|
||||
@fclose($fp_out);
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zip encode a file.
|
||||
*/
|
||||
function _backup_migrate_zip_encode($source, $dest, $filename) {
|
||||
$success = FALSE;
|
||||
if (class_exists('ZipArchive')) {
|
||||
$zip = new ZipArchive;
|
||||
$res = $zip->open($dest, constant("ZipArchive::CREATE"));
|
||||
if ($res === TRUE) {
|
||||
$zip->addFile($source, $filename);
|
||||
$success = $zip->close();
|
||||
}
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zip decode a file.
|
||||
*/
|
||||
function _backup_migrate_zip_decode($source, $dest) {
|
||||
$success = FALSE;
|
||||
if (class_exists('ZipArchive')) {
|
||||
$zip = new ZipArchive;
|
||||
if (($fp_out = fopen($dest, "w")) && ($zip->open($source))) {
|
||||
$filename = ($zip->getNameIndex(0));
|
||||
if ($fp_in = $zip->getStream($filename)) {
|
||||
while (!feof($fp_in)) {
|
||||
fwrite($fp_out, fread($fp_in, 1024 * 512));
|
||||
}
|
||||
$success = TRUE;
|
||||
}
|
||||
}
|
||||
@fclose($fp_in);
|
||||
@fclose($fp_out);
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compress a file with the given settings.
|
||||
* Also updates settings to reflect new file mime and file extension.
|
||||
*/
|
||||
function _backup_migrate_file_compress($file, $settings) {
|
||||
switch ($settings->filters['compression']) {
|
||||
case "gzip":
|
||||
$from = $file->push_type('gzip');
|
||||
if (!$success = $this->_backup_migrate_gzip_encode($from, $file->filepath(), 9, $settings)) {
|
||||
$file = NULL;
|
||||
}
|
||||
break;
|
||||
|
||||
case "bzip":
|
||||
$from = $file->push_type('bzip2');
|
||||
if (!$success = $this->_backup_migrate_bzip_encode($from, $file->filepath())) {
|
||||
$file = NULL;
|
||||
}
|
||||
break;
|
||||
|
||||
case "zip":
|
||||
$filename = $file->filename();
|
||||
$from = $file->push_type('zip');
|
||||
if (!$success = $this->_backup_migrate_zip_encode($from, $file->filepath(), $filename)) {
|
||||
$file = NULL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (!$file) {
|
||||
_backup_migrate_message("Could not compress backup file. Try backing up without compression.", array(), 'error');
|
||||
}
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompress a file with the given settings.
|
||||
* Also updates settings to reflect new file mime and file extension.
|
||||
*/
|
||||
function _backup_migrate_file_decompress($file, $settings) {
|
||||
$success = FALSE;
|
||||
|
||||
switch ($file->type_id()) {
|
||||
case "gzip":
|
||||
$from = $file->pop_type();
|
||||
$success = $this->_backup_migrate_gzip_decode($from->filepath(), $file->filepath(), $settings);
|
||||
break;
|
||||
|
||||
case "bzip":
|
||||
case "bzip2":
|
||||
$from = $file->pop_type();
|
||||
$success = $this->_backup_migrate_bzip_decode($from->filepath(), $file->filepath());
|
||||
break;
|
||||
|
||||
case "zip":
|
||||
$from = $file->pop_type();
|
||||
$success = $this->_backup_migrate_zip_decode($from->filepath(), $file->filepath());
|
||||
break;
|
||||
|
||||
default:
|
||||
return $file;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!$success) {
|
||||
_backup_migrate_message("Could not decompress backup file. Please check that the file is valid.", array(), 'error');
|
||||
}
|
||||
return $success ? $file : NULL;
|
||||
}
|
||||
}
|
||||
|
177
sites/all/modules/backup_migrate/includes/filters.encryption.inc
Normal file
177
sites/all/modules/backup_migrate/includes/filters.encryption.inc
Normal file
|
@ -0,0 +1,177 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* A filter for encrypting bckups with AES.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A filter for encrypting backup files.
|
||||
*
|
||||
* @ingroup backup_migrate_filters
|
||||
*/
|
||||
class backup_migrate_filter_encryption extends backup_migrate_filter {
|
||||
var $op_weights = array('backup' => 170, 'restore' => -170);
|
||||
|
||||
/**
|
||||
* This function is called on a backup file after the backup has been completed.
|
||||
*/
|
||||
function backup($file, $settings) {
|
||||
return $this->file_encrypt($file, $settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called on a backup file before importing it.
|
||||
*/
|
||||
function restore($file, $settings) {
|
||||
return $this->file_decrypt($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function backup_settings_default() {
|
||||
return array('encryption' => 'none');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function backup_settings_form($settings) {
|
||||
$form = array();
|
||||
$options = $this->_backup_migrate_get_encryption_form_item_options();
|
||||
if (count($options) > 1) {
|
||||
$form['file']['encryption'] = array(
|
||||
"#type" => "select",
|
||||
"#title" => t("File Encryption"),
|
||||
"#options" => $options,
|
||||
"#default_value" => @$settings['encryption'],
|
||||
'#description' => t('Encrypted files can only be restored by Backup and Migrate and only on sites with the same encryption key.'),
|
||||
);
|
||||
}
|
||||
else {
|
||||
$form['file']['encryption'] = array(
|
||||
"#type" => 'item',
|
||||
"#title" => t("File Encryption"),
|
||||
"#markup" => t('Install the !link to enable backup file encryption.', array('!link' => l(t('AES Encryption Module'), 'http://drupal.org/project/aes'))),
|
||||
);
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of backup filetypes.
|
||||
*/
|
||||
function file_types() {
|
||||
return array(
|
||||
"aes" => array(
|
||||
"extension" => "aes",
|
||||
"filemime" => "application/octet-stream",
|
||||
"backup" => TRUE,
|
||||
"restore" => TRUE,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the compression options as an options array for a form item.
|
||||
*/
|
||||
function _backup_migrate_get_encryption_form_item_options() {
|
||||
$options = array();
|
||||
$options = array('' => t('No Encryption'));
|
||||
if (@function_exists("aes_encrypt")) {
|
||||
$options['aes'] = t("AES Encryption");
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* AES encrypt a file.
|
||||
*/
|
||||
function aes_encrypt($source, $dest) {
|
||||
$success = FALSE;
|
||||
if (function_exists('aes_encrypt')) {
|
||||
if ($data = $source->get_contents()) {
|
||||
// Add a marker to the end of the data so we can trim the padding on decrpypt.
|
||||
$data = pack("a*H2", $data, "80");
|
||||
if ($data = aes_encrypt($data, FALSE)) {
|
||||
$dest->put_contents($data);
|
||||
$success = TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gzip decode a file.
|
||||
*/
|
||||
function aes_decrypt($source, $dest) {
|
||||
$success = FALSE;
|
||||
if (function_exists('aes_decrypt')) {
|
||||
if ($data = $source->get_contents()) {
|
||||
if ($data = aes_decrypt($data, FALSE)) {
|
||||
// Trim all the padding zeros plus our non-zero marker.
|
||||
$data = substr(rtrim($data, "\0"), 0, -1);
|
||||
$dest->put_contents($data);
|
||||
$success = TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compress a file with the given settings.
|
||||
* Also updates settings to reflect new file mime and file extension.
|
||||
*/
|
||||
function file_encrypt($file, $settings) {
|
||||
if (!empty($settings->filters['encryption'])) {
|
||||
switch ($settings->filters['encryption']) {
|
||||
case "aes":
|
||||
$from = $file->push_type('aes');
|
||||
$from = new backup_file(array('filepath' => $from));
|
||||
if (!$success = $this->aes_encrypt($from, $file)) {
|
||||
$file = NULL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (!$file) {
|
||||
_backup_migrate_message("Could not encrypt backup file. Try backing up without encryption.", array(), 'error');
|
||||
}
|
||||
}
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decompress a file with the given settings.
|
||||
* Also updates settings to reflect new file mime and file extension.
|
||||
*/
|
||||
function file_decrypt($file) {
|
||||
$success = FALSE;
|
||||
if ($file) {
|
||||
switch ($file->type_id()) {
|
||||
case "aes":
|
||||
$from = $file->pop_type();
|
||||
$success = $this->aes_decrypt($from, $file);
|
||||
break;
|
||||
default:
|
||||
return $file;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!$success) {
|
||||
if (function_exists('aes_decrypt')) {
|
||||
_backup_migrate_message("Could not decrpyt backup file. Please check that the file is valid and that the encryption key of the server matches the server that created the backup.", array(), 'error');
|
||||
}
|
||||
else {
|
||||
_backup_migrate_message('You must install the !link to restore encrypted backkups.', array('!link' => l(t('AES Encryption Module'), 'http://drupal.org/project/aes')), 'error');
|
||||
}
|
||||
}
|
||||
}
|
||||
return $success ? $file : NULL;
|
||||
}
|
||||
}
|
||||
|
332
sites/all/modules/backup_migrate/includes/filters.inc
Normal file
332
sites/all/modules/backup_migrate/includes/filters.inc
Normal file
|
@ -0,0 +1,332 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* All of the filter handling code needed for Backup and Migrate.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get the available destination types.
|
||||
*/
|
||||
function backup_migrate_get_filters($op = NULL) {
|
||||
$filters = &drupal_static('backup_migrate_get_filters', NULL);
|
||||
if ($filters === NULL) {
|
||||
$filters = array();
|
||||
$definitions = module_invoke_all('backup_migrate_filters');
|
||||
foreach ($definitions as $definition) {
|
||||
// Include the necessary file if specified by the filter.
|
||||
if (!empty($definition['file'])) {
|
||||
require_once './'. $definition['file'];
|
||||
}
|
||||
$filters[] = new $definition['class'];
|
||||
}
|
||||
}
|
||||
$sort = array();
|
||||
// Sort the filters based on the weight for the given operation.
|
||||
foreach ($filters as $filter) {
|
||||
$sort[] = $filter->weight($op);
|
||||
}
|
||||
array_multisort($sort, SORT_ASC, SORT_NUMERIC, $filters);
|
||||
return $filters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_backup_migrate_filters().
|
||||
*
|
||||
* Get the built in Backup and Migrate filters.
|
||||
*/
|
||||
function backup_migrate_backup_migrate_filters() {
|
||||
return array(
|
||||
'backup_restore' => array(
|
||||
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.backup_restore.inc',
|
||||
'class' => 'backup_migrate_filter_backup_restore',
|
||||
),
|
||||
'compression' => array(
|
||||
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.compression.inc',
|
||||
'class' => 'backup_migrate_filter_compression',
|
||||
),
|
||||
'encryption' => array(
|
||||
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.encryption.inc',
|
||||
'class' => 'backup_migrate_filter_encryption',
|
||||
),
|
||||
'statusnotify' => array(
|
||||
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.statusnotify.inc',
|
||||
'class' => 'backup_migrate_filter_statusnotify',
|
||||
),
|
||||
'utils' => array(
|
||||
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.utils.inc',
|
||||
'class' => 'backup_migrate_filter_utils',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke the given method on all of the available filters.
|
||||
*/
|
||||
function backup_migrate_filters_invoke_all() {
|
||||
$args = func_get_args();
|
||||
$op = array_shift($args);
|
||||
$out = array();
|
||||
$filters = backup_migrate_get_filters($op);
|
||||
foreach ($filters as $filter) {
|
||||
if (method_exists($filter, $op)) {
|
||||
/* call_user_func_array() ignores the function signature, so we cannot
|
||||
* use it to pass references. (Call-time pass-by-reference is deprecated
|
||||
* in PHP5.3.) Work around it, since we have unknown function signatures.
|
||||
*/
|
||||
switch (count($args)) {
|
||||
case 1:
|
||||
$ret = $filter->$op($args[0]);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
$ret = $filter->$op($args[0], $args[1]);
|
||||
break;
|
||||
|
||||
default:
|
||||
// This assumes that no functions with more than 2 arguments expect a
|
||||
// reference as argument. If so, add another 'case block'.
|
||||
$ret = call_user_func_array(array($filter, $op), $args);
|
||||
}
|
||||
$out = array_merge_recursive($out, (array) $ret);
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter a backup file before sending it to the destination.
|
||||
*/
|
||||
function backup_migrate_filters_backup($file, &$settings) {
|
||||
backup_migrate_filters_invoke_all('pre_backup', $file, $settings);
|
||||
$filters = backup_migrate_get_filters('backup');
|
||||
foreach ($filters as $filter) {
|
||||
if ($file) {
|
||||
$file = $filter->backup($file, $settings);
|
||||
}
|
||||
}
|
||||
backup_migrate_filters_invoke_all('post_backup', $file, $settings);
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter a backup file before sending it to the destination.
|
||||
*/
|
||||
function backup_migrate_filters_restore($file, &$settings) {
|
||||
backup_migrate_filters_invoke_all('pre_restore', $file, $settings);
|
||||
$filters = backup_migrate_get_filters('restore');
|
||||
foreach ($filters as $filter) {
|
||||
if ($file) {
|
||||
$file = $filter->restore($file, $settings);
|
||||
}
|
||||
}
|
||||
backup_migrate_filters_invoke_all('post_restore', $file, $settings);
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the backup settings for all of the filters.
|
||||
*/
|
||||
function backup_migrate_filters_settings_form($settings, $op) {
|
||||
$out = backup_migrate_filters_invoke_all($op .'_settings_form', $settings);
|
||||
$out = backup_migrate_filters_settings_form_set_parents($out);
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a form parent to the filter settings so that the filter values are saved in the right table.
|
||||
*/
|
||||
function backup_migrate_filters_settings_form_set_parents($form) {
|
||||
foreach (element_children($form) as $key) {
|
||||
if (!isset($form[$key]['#parents'])) {
|
||||
$form[$key]['#parents'] = array('filters', $key);
|
||||
$form[$key] = backup_migrate_filters_settings_form_set_parents($form[$key]);
|
||||
}
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate all the filters.
|
||||
*/
|
||||
function backup_migrate_filters_settings_form_validate($op, $form, &$form_state) {
|
||||
backup_migrate_filters_invoke_all($op .'_settings_form_validate', $form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit all of the filters.
|
||||
*/
|
||||
function backup_migrate_filters_settings_form_submit($op, $form, &$form_state) {
|
||||
backup_migrate_filters_invoke_all($op .'_settings_form_submit', $form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default settings for the filters.
|
||||
*/
|
||||
function backup_migrate_filters_settings_default($op) {
|
||||
return backup_migrate_filters_invoke_all($op .'_settings_default');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the backup settings for all of the filters.
|
||||
*/
|
||||
function backup_migrate_filters_before_action_form($settings, $op) {
|
||||
$out = array();
|
||||
$out += backup_migrate_filters_invoke_all('before_action_form', $op, $settings);
|
||||
$out += backup_migrate_filters_invoke_all('before_' . $op .'_form', $settings);
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the backup settings for all of the filters.
|
||||
*/
|
||||
function backup_migrate_filters_before_action_form_validate($settings, $op, $form, &$form_state) {
|
||||
backup_migrate_filters_invoke_all('before_action_form_validate', $op, $settings, $form, $form_state);
|
||||
backup_migrate_filters_invoke_all('before_' . $op .'_form_validate', $settings, $form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the backup settings for all of the filters.
|
||||
*/
|
||||
function backup_migrate_filters_before_action_form_submit($settings, $op, $form, &$form_state) {
|
||||
backup_migrate_filters_invoke_all('before_action_form_submit', $op, $settings, $form, $form_state);
|
||||
backup_migrate_filters_invoke_all('before_' . $op .'_form_submit', $settings, $form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file types for all of the filters.
|
||||
*/
|
||||
function backup_migrate_filters_file_types() {
|
||||
return backup_migrate_filters_invoke_all('file_types');
|
||||
}
|
||||
|
||||
/**
|
||||
* A base class for basing filters on.
|
||||
*/
|
||||
class backup_migrate_filter {
|
||||
var $weight = 0;
|
||||
var $op_weights = array();
|
||||
|
||||
/**
|
||||
* Get the weight of the filter for the given op.
|
||||
*/
|
||||
function weight($op = NULL) {
|
||||
if ($op && isset($this->op_weights[$op])) {
|
||||
return $this->op_weights[$op];
|
||||
}
|
||||
return $this->weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function backup_settings_default() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function backup_settings_form($settings) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function backup_settings_form_validate($form, &$form_state) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the settings form. Any values returned will be saved.
|
||||
*/
|
||||
function backup_settings_form_submit($form, &$form_state) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function restore_settings_default() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function restore_settings_form($settings) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function restore_settings_form_validate($form, &$form_state) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the settings form. Any values returned will be saved.
|
||||
*/
|
||||
function restore_settings_form_submit($form, &$form_state) {
|
||||
return $form_state['values'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of file types handled by this filter.
|
||||
*/
|
||||
function file_types() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare any default destinations for this filter.
|
||||
*/
|
||||
function destinations() {
|
||||
return array();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This function is called on a backup file after the backup has been completed.
|
||||
*/
|
||||
function backup($file, $settings) {
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called immediately prior to backup.
|
||||
*/
|
||||
function pre_backup($file, $settings) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called immediately post backup.
|
||||
*/
|
||||
function post_backup($file, $settings) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called on a backup file before importing it.
|
||||
*/
|
||||
function restore($file, $settings) {
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called immediately prior to restore.
|
||||
*/
|
||||
function pre_restore($file, $settings) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called immediately post restore.
|
||||
*/
|
||||
function post_restore($file, $settings) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* A filter for compressing bckups with zip, gz bzip etc.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A filter to send a notification email on success or failure of backup.
|
||||
*
|
||||
* @ingroup backup_migrate_filters
|
||||
*/
|
||||
class backup_migrate_filter_statusnotify extends backup_migrate_filter {
|
||||
|
||||
/**
|
||||
* Get the default backup settings for this filter.
|
||||
*/
|
||||
function backup_settings_default() {
|
||||
return array(
|
||||
'notify_success_enable' => FALSE,
|
||||
'notify_failure_enable' => FALSE,
|
||||
'notify_success_email' => variable_get('site_mail', ''),
|
||||
'notify_failure_email' => variable_get('site_mail', ''),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function backup_settings_form($settings) {
|
||||
$form = array();
|
||||
$form['advanced']['notify_success_enable'] = array(
|
||||
"#type" => 'checkbox',
|
||||
"#title" => t("Send an email if backup succeeds"),
|
||||
"#default_value" => @$settings['notify_success_enable'],
|
||||
);
|
||||
$form['advanced']['notify_success_email_wrapper'] = array(
|
||||
'#type' => 'backup_migrate_dependent',
|
||||
'#dependencies' => array(
|
||||
'filters[notify_success_enable]' => TRUE,
|
||||
),
|
||||
);
|
||||
$form['advanced']['notify_success_email_wrapper']['notify_success_email'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Email Address for Success Notices"),
|
||||
"#default_value" => @$settings['notify_success_email'],
|
||||
);
|
||||
$form['advanced']['notify_failure_enable'] = array(
|
||||
"#type" => 'checkbox',
|
||||
"#title" => t("Send an email if backup fails"),
|
||||
"#default_value" => @$settings['notify_failure_enable'],
|
||||
);
|
||||
$form['advanced']['notify_failure_email_wrapper'] = array(
|
||||
'#type' => 'backup_migrate_dependent',
|
||||
'#dependencies' => array(
|
||||
'filters[notify_failure_enable]' => TRUE,
|
||||
),
|
||||
);
|
||||
$form['advanced']['notify_failure_email_wrapper']['notify_failure_email'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Email Address for Failure Notices"),
|
||||
"#default_value" => @$settings['notify_failure_email'],
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the success email.
|
||||
*/
|
||||
function backup_succeed($settings) {
|
||||
if (@$settings->filters['notify_success_enable'] && $to = @$settings->filters['notify_success_email']) {
|
||||
$messages = $this->get_messages();
|
||||
$subject = t('!site backup succeeded', array('!site' => variable_get('site_name', 'Drupal site')));
|
||||
if ($messages = $this->get_messages()) {
|
||||
$body = t("The site backup has completed successfully with the following messages:\n!messages", array('!messages' => $messages));
|
||||
}
|
||||
else {
|
||||
$body = t("The site backup has completed successfully.\n");
|
||||
}
|
||||
mail($settings->filters['notify_success_email'], $subject, $body);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the failure email.
|
||||
*/
|
||||
function backup_fail($settings) {
|
||||
if (@$settings->filters['notify_failure_enable'] && $to = @$settings->filters['notify_failure_email']) {
|
||||
$messages = $this->get_messages();
|
||||
$subject = t('!site backup failed', array('!site' => variable_get('site_name', 'Drupal site')));
|
||||
if ($messages = $this->get_messages()) {
|
||||
$body = t("The site backup has failed with the following messages:\n!messages", array('!messages' => $messages));
|
||||
}
|
||||
else {
|
||||
$body = t("The site backup has failed for an unknown reason.");
|
||||
}
|
||||
mail($settings->filters['notify_failure_email'], $subject, $body);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the messages and errors for the email.
|
||||
*/
|
||||
function get_messages() {
|
||||
$out = "";
|
||||
$messages = _backup_migrate_messages();
|
||||
foreach ($messages as $message) {
|
||||
$out .= strip_tags(t($message['message'], $message['replace'])) . "\n";
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
239
sites/all/modules/backup_migrate/includes/filters.utils.inc
Normal file
239
sites/all/modules/backup_migrate/includes/filters.utils.inc
Normal file
|
@ -0,0 +1,239 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* A filter to run some basic utility functions. Basically any useful option not big enough to justify it's own class.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A filter to run some basic utility functions.
|
||||
*
|
||||
* @ingroup backup_migrate_filters
|
||||
*/
|
||||
class backup_migrate_filter_utils extends backup_migrate_filter {
|
||||
var $op_weights = array('pre_backup' => -1000, 'post_backup' => 1000);
|
||||
var $saved_devel_query = NULL;
|
||||
|
||||
/**
|
||||
* Get the default backup settings for this filter.
|
||||
*/
|
||||
function backup_settings_default() {
|
||||
return array(
|
||||
'utils_disable_query_log' => TRUE,
|
||||
'utils_site_offline' => FALSE,
|
||||
'utils_description' => '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default restore settings for this filter.
|
||||
*/
|
||||
function restore_settings_default() {
|
||||
return array(
|
||||
'utils_disable_query_log' => TRUE,
|
||||
'utils_site_offline' => FALSE,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the backup settings for this filter.
|
||||
*/
|
||||
function backup_settings_form($settings) {
|
||||
$form = array();
|
||||
if (module_exists('devel') && variable_get('dev_query', 0)) {
|
||||
$form['database']['utils_disable_query_log'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Disable query log'),
|
||||
'#default_value' => !empty($settings['utils_disable_query_log']) ? $settings['utils_disable_query_log'] : NULL,
|
||||
'#description' => t('Disable the devel module\'s query logging during the backup operation. It will be enabled again after backup is complete. This is very highly recommended.'),
|
||||
);
|
||||
}
|
||||
$form['advanced']['utils_site_offline'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Take site offline'),
|
||||
'#default_value' => !empty($settings['utils_site_offline']) ? $settings['utils_site_offline'] : NULL,
|
||||
'#description' => t('Take the site offline during backup and show a maintenance message. Site will be taken back online once the backup is complete.'),
|
||||
);
|
||||
$form['advanced']['utils_site_offline_message_wrapper'] = array(
|
||||
'#type' => 'backup_migrate_dependent',
|
||||
'#dependencies' => array(
|
||||
'filters[utils_site_offline]' => TRUE,
|
||||
),
|
||||
);
|
||||
$form['advanced']['utils_site_offline_message_wrapper']['utils_site_offline_message'] = array(
|
||||
'#type' => 'textarea',
|
||||
'#title' => t('Site off-line message'),
|
||||
'#default_value' => !empty($settings['utils_site_offline_message']) ? $settings['utils_site_offline_message'] : variable_get('site_offline_message', t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal')))),
|
||||
'#description' => t('Message to show visitors when the site is in off-line mode.')
|
||||
);
|
||||
$form['advanced']['utils_description'] = array(
|
||||
'#type' => 'textarea',
|
||||
'#title' => t('Add a note'),
|
||||
'#default_value' => !empty($settings['utils_description']) ? $settings['utils_description'] : NULL,
|
||||
'#description' => t('Add a short note to the backup file.'),
|
||||
);
|
||||
$form['advanced']['use_cli'] = array(
|
||||
"#type" => "checkbox",
|
||||
"#title" => t("Use cli commands"),
|
||||
"#default_value" => !empty($settings['use_cli']),
|
||||
"#description" => t("Use the command line tools (mysqldump, tar, gzip etc.) if available. This can be faster for large sites but will not work on all servers. EXPERIMENTAL"),
|
||||
);
|
||||
$form['advanced']['ignore_errors'] = array(
|
||||
"#type" => "checkbox",
|
||||
"#title" => t("Ignore errors"),
|
||||
"#default_value" => !empty($settings['ignore_errors']),
|
||||
"#description" => t("Will attempt to complete backup even if certain recoverable errors occur. This may make the backup files invalid. Enable this if you have unreadable files that you want to ignore during backup."),
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the restore settings for this filter.
|
||||
*/
|
||||
function restore_settings_form($settings) {
|
||||
$form = array();
|
||||
if (module_exists('devel') && variable_get('dev_query', 0)) {
|
||||
$form['advanced']['utils_disable_query_log'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Disable query log'),
|
||||
'#default_value' => @$settings['utils_disable_query_log'],
|
||||
'#description' => t('Disable the devel module\'s query logging during the restore operation. It will be enabled again after restore is complete. This is very highly recommended.'),
|
||||
);
|
||||
}
|
||||
$form['advanced']['utils_site_offline'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Take site offline'),
|
||||
'#default_value' => !empty($settings['utils_site_offline']) ? $settings['utils_site_offline'] : NULL,
|
||||
'#description' => t('Take the site offline during restore and show a maintenance message. Site will be taken back online once the restore is complete.'),
|
||||
);
|
||||
$form['advanced']['utils_site_offline_message_wrapper'] = array(
|
||||
'#type' => 'backup_migrate_dependent',
|
||||
'#dependencies' => array(
|
||||
'filters[utils_site_offline]' => TRUE,
|
||||
),
|
||||
);
|
||||
$form['advanced']['utils_site_offline_message_wrapper']['utils_site_offline_message'] = array(
|
||||
'#type' => 'textarea',
|
||||
'#title' => t('Site off-line message'),
|
||||
'#default_value' => !empty($settings['utils_site_offline_message']) ? $settings['utils_site_offline_message'] : variable_get('site_offline_message', t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal')))),
|
||||
'#description' => t('Message to show visitors when the site is in off-line mode.')
|
||||
);
|
||||
$form['advanced']['use_cli'] = array(
|
||||
"#type" => "checkbox",
|
||||
"#title" => t("Use cli commands"),
|
||||
"#default_value" => !empty($settings['use_cli']),
|
||||
"#description" => t("Use the command line tools (mysqldump, tar, gzip etc.) if available. This can be faster for large sites but will not work on all servers. EXPERIMENTAL"),
|
||||
);
|
||||
// $form['advanced']['ignore_errors'] = array(
|
||||
// "#type" => "checkbox",
|
||||
// "#title" => t("Ignore errors"),
|
||||
// "#default_value" => !empty($settings['ignore_errors']),
|
||||
// "#description" => t("Will attempt to complete restore even if certain recoverable errors occur. This may could corrupt your site."),
|
||||
// );
|
||||
return $form;
|
||||
}
|
||||
|
||||
function pre_backup($file, $settings) {
|
||||
$this->take_site_offline($settings);
|
||||
$this->disable_devel_query($settings);
|
||||
}
|
||||
|
||||
function post_backup($file, $settings) {
|
||||
$this->enable_devel_query($settings);
|
||||
$this->take_site_online($settings);
|
||||
if ($file) {
|
||||
$this->add_file_info($file, $settings);
|
||||
}
|
||||
}
|
||||
|
||||
function pre_restore($file, $settings) {
|
||||
$this->disable_devel_query($settings);
|
||||
$this->take_site_offline($settings);
|
||||
}
|
||||
|
||||
function post_restore($file, $settings) {
|
||||
$this->enable_devel_query($settings);
|
||||
$this->take_site_online($settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable devel query logging if it's active and the user has chosen to do so.
|
||||
*/
|
||||
function disable_devel_query($settings) {
|
||||
$this->saved_devel_query = variable_get('dev_query', 0);
|
||||
if (module_exists('devel') && variable_get('dev_query', 0) && !empty($settings->filters['utils_disable_query_log'])) {
|
||||
variable_set('dev_query', 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore devel query to previous state.
|
||||
*/
|
||||
function enable_devel_query($settings) {
|
||||
if (module_exists('devel')) {
|
||||
variable_set('dev_query', $this->saved_devel_query);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the backup metadata to the file.
|
||||
*/
|
||||
function add_file_info($file, $settings) {
|
||||
$file->file_info['description'] = $settings->filters['utils_description'];
|
||||
$file->file_info['datestamp'] = time();
|
||||
$file->file_info['generator'] = 'Backup and Migrate (http://drupal.org/project/backup_migrate)';
|
||||
$file->file_info['generatorversion'] = BACKUP_MIGRATE_VERSION;
|
||||
$file->file_info['siteurl'] = url('', array('absolute' => TRUE));
|
||||
$file->file_info['sitename'] = variable_get('site_name', '');
|
||||
$file->file_info['drupalversion'] = VERSION;
|
||||
$file->calculate_filesize();
|
||||
|
||||
$source = $settings->get('source');
|
||||
$file->file_info['bam_sourceid'] = $source->get('id');
|
||||
$file->file_info['bam_sourcetype'] = $source->get('subtype');
|
||||
$file->file_info['bam_sourcename'] = $source->get('name');
|
||||
|
||||
// Add any additional info that has been added to the settings by other plugins.
|
||||
if (!empty($settings->file_info)) {
|
||||
$file->file_info += $settings->file_info;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Take the site offline if configured to do so.
|
||||
*/
|
||||
function take_site_offline($settings) {
|
||||
// If the site is already offline then don't do anything.
|
||||
if (!variable_get('maintenance_mode', 0)) {
|
||||
// Save the current state of the site in case a restore overwrites it.
|
||||
if (!empty($settings->filters['utils_site_offline'])) {
|
||||
$this->saved_site_offline = TRUE;
|
||||
$this->saved_site_offline_message = variable_get('maintenance_mode_message', NULL);
|
||||
if (!empty($settings->filters['utils_site_offline_message'])) {
|
||||
$this->saved_site_offline_message = variable_get('maintenance_mode_message', NULL);
|
||||
variable_set('maintenance_mode_message', $settings->filters['utils_site_offline_message']);
|
||||
}
|
||||
variable_set('maintenance_mode', 1);
|
||||
_backup_migrate_message('Site was taken offline.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Take the site online again after backup or restore.
|
||||
*/
|
||||
function take_site_online($settings) {
|
||||
// Take the site back off/online because the restored db may have changed that setting.
|
||||
if (variable_get('maintenance_mode', 0) && !empty($this->saved_site_offline)) {
|
||||
variable_set('maintenance_mode', 0);
|
||||
if ($settings->filters['utils_site_offline']) {
|
||||
if (!empty($this->saved_site_offline_message)) {
|
||||
variable_set('maintenance_mode_message', $this->saved_site_offline_message);
|
||||
}
|
||||
_backup_migrate_message('Site was taken online.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
543
sites/all/modules/backup_migrate/includes/locations.inc
Normal file
543
sites/all/modules/backup_migrate/includes/locations.inc
Normal file
|
@ -0,0 +1,543 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* All of the location handling code needed for Backup and Migrate.
|
||||
*/
|
||||
|
||||
backup_migrate_include('crud');
|
||||
|
||||
/**
|
||||
* Get the available location types.
|
||||
*/
|
||||
function backup_migrate_get_location_subtypes() {
|
||||
backup_migrate_include('crud', 'destinations', 'sources');
|
||||
return backup_migrate_crud_subtypes('destination') + backup_migrate_crud_subtypes('source');
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_backup_migrate_locations().
|
||||
*
|
||||
* Get the built in backup locations and those in the db.
|
||||
*/
|
||||
function backup_migrate_backup_migrate_locations() {
|
||||
$out = array();
|
||||
|
||||
// Allow the filter plugins to declare the default locations.
|
||||
backup_migrate_include('filters');
|
||||
$out += backup_migrate_filters_invoke_all('locations');
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the available backup location.
|
||||
*
|
||||
* @param $op
|
||||
* The operation which will be performed on the location. Hooks can use this
|
||||
* to return only those locations appropriate for the given op.
|
||||
* Options include:
|
||||
* 'manual backup' - locations available for manual backup
|
||||
* 'scheduled backup' - locations available for schedules backup
|
||||
* 'list files' - locations whose backup files can be listed
|
||||
* 'restore' - locations whose files can be restored from
|
||||
* 'all' - all available locations should be returned
|
||||
*/
|
||||
function backup_migrate_get_locations($op = 'all') {
|
||||
$locations = &drupal_static('backup_migrate_get_locations', NULL);
|
||||
|
||||
// Get the list of locations and cache them locally.
|
||||
if ($locations === NULL) {
|
||||
$locations = backup_migrate_crud_get_items('location');
|
||||
}
|
||||
|
||||
// Return all if that's what was asked for.
|
||||
if ($op == 'all') {
|
||||
return $locations;
|
||||
}
|
||||
|
||||
// Return only those locations which support the given op.
|
||||
$out = array();
|
||||
if ($locations) {
|
||||
foreach ($locations as $key => $location) {
|
||||
if ($location->op($op)) {
|
||||
$out[$key] = $location;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the location of the given id.
|
||||
*/
|
||||
function backup_migrate_get_location($id) {
|
||||
$locations = backup_migrate_get_locations('all');
|
||||
return empty($locations[$id]) ? NULL : $locations[$id];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A base class for creating locations.
|
||||
*/
|
||||
class backup_migrate_location extends backup_migrate_item {
|
||||
var $db_table = "backup_migrate_destinations";
|
||||
var $type_name = "location";
|
||||
var $default_values = array('settings' => array());
|
||||
var $singular = 'location';
|
||||
var $plural = 'locations';
|
||||
var $title_plural = 'Locations';
|
||||
var $title_singular = 'Location';
|
||||
|
||||
var $subtype = "";
|
||||
var $supported_ops = array();
|
||||
|
||||
/**
|
||||
* This function is not supposed to be called. It is just here to help the po extractor out.
|
||||
*/
|
||||
function strings() {
|
||||
// Help the pot extractor find these strings.
|
||||
t('location');
|
||||
t('locations');
|
||||
t('Location');
|
||||
t('Locations');
|
||||
}
|
||||
|
||||
function ops() {
|
||||
return $this->supported_ops;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does this location support the given operation.
|
||||
*/
|
||||
function op($op) {
|
||||
$ops = (array)$this->ops();
|
||||
return in_array($op, $ops);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the given op from the support list.
|
||||
*/
|
||||
function remove_op($op) {
|
||||
$key = array_search($op, $this->supported_ops);
|
||||
if ($key !== FALSE) {
|
||||
unset($this->supported_ops[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
function get_name() {
|
||||
return @$this->name;
|
||||
}
|
||||
|
||||
function set_name($name) {
|
||||
return $this->name = $name;
|
||||
}
|
||||
|
||||
function set_location($location) {
|
||||
$this->location = $location;
|
||||
}
|
||||
|
||||
function get_location() {
|
||||
return @$this->location;
|
||||
}
|
||||
|
||||
function get_display_location() {
|
||||
return $this->get_location();
|
||||
}
|
||||
|
||||
function settings($key = NULL) {
|
||||
$out = $this->settings;
|
||||
if ($key) {
|
||||
$out = isset($out[$key]) ? $out[$key] : NULL;
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the type name of this location for display to the user.
|
||||
*/
|
||||
function get_subtype_name() {
|
||||
if ($type = $this->get('subtype')) {
|
||||
$types = $this->location_types();
|
||||
return isset($types[$type]['type_name']) ? $types[$type]['type_name'] : $type;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the edit form for the item.
|
||||
*/
|
||||
function edit_form() {
|
||||
if (!empty($this->supported_ops)) {
|
||||
$form = parent::edit_form();
|
||||
$form['subtype'] = array(
|
||||
"#type" => "value",
|
||||
"#default_value" => $this->get('subtype'),
|
||||
);
|
||||
}
|
||||
else {
|
||||
$types = $this->location_types();
|
||||
$items = array();
|
||||
// If no (valid) node type has been provided, display a node type overview.
|
||||
foreach ($types as $key => $type) {
|
||||
if (@$type['can_create']) {
|
||||
$type_url_str = str_replace('_', '-', $key);
|
||||
$out = '<dt>'. l($type['type_name'], BACKUP_MIGRATE_MENU_PATH . "/settings/$this->type_name/add/$type_url_str", array('attributes' => array('title' => t('Add a new @s location.', array('@s' => $type['type_name']))))) .'</dt>';
|
||||
$out .= '<dd>'. filter_xss_admin($type['description']) .'</dd>';
|
||||
$items[] = $out;
|
||||
}
|
||||
}
|
||||
if (count($items)) {
|
||||
$output = t('Choose the type of location you would like to create:') .'<dl>'. implode('', $items) .'</dl>';
|
||||
}
|
||||
else {
|
||||
$output = t('No types available.');
|
||||
}
|
||||
$form['select_type'] = array(
|
||||
'#type' => 'markup',
|
||||
'#markup' => $output,
|
||||
);
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the available location types.
|
||||
*/
|
||||
function location_types() {
|
||||
return backup_migrate_get_location_subtypes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message to send to the user when confirming the deletion of the item.
|
||||
*/
|
||||
function delete_confirm_message() {
|
||||
return t('Are you sure you want to delete the %name?', array('%name' => $this->get_name()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the columns needed to list the type.
|
||||
*/
|
||||
function get_list_column_info() {
|
||||
$out = parent::get_list_column_info();
|
||||
$out = array(
|
||||
'name' => array('title' => t('Name')),
|
||||
'subtype_name' => array('title' => t('Type')),
|
||||
'display_location' => array('title' => t('Location')),
|
||||
) + $out;
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a row of data to be used in a list of items of this type.
|
||||
*/
|
||||
function get_list_row() {
|
||||
$out = parent::get_list_row();
|
||||
|
||||
// Suppress locations with no actions as there's no value in showing them (and they may confuse new users).
|
||||
if (empty($out['actions'])) {
|
||||
return NULL;
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the action links for a location.
|
||||
*/
|
||||
function get_action_links() {
|
||||
$out = parent::get_action_links();
|
||||
$item_id = $this->get_id();
|
||||
|
||||
// Don't display the download/delete/restore ops if they are not available for this location.
|
||||
if ($this->op('list files') && user_access("access backup files")) {
|
||||
$out = array('list files' => l(t("list files"), BACKUP_MIGRATE_MENU_PATH . "/$this->type_name/list/files/". $item_id)) + $out;
|
||||
}
|
||||
if (!$this->op('configure') || !user_access('administer backup and migrate')) {
|
||||
unset($out['edit']);
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if we can read the given file.
|
||||
*/
|
||||
function can_read_file($file_id) {
|
||||
return $this->op('restore');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this location type.
|
||||
*/
|
||||
function settings_default() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this location.
|
||||
*/
|
||||
function settings_form($form) {
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the form for the settings for this location.
|
||||
*/
|
||||
function settings_form_validate($form_values) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the settings form. Any values returned will be saved.
|
||||
*/
|
||||
function settings_form_submit($form_values) {
|
||||
return $form_values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function backup_settings_default() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function backup_settings_form($settings) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function backup_settings_form_validate($form, &$form_state) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the settings form. Any values returned will be saved.
|
||||
*/
|
||||
function backup_settings_form_submit($form, &$form_state) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function restore_settings_default() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function restore_settings_form($settings) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this filter.
|
||||
*/
|
||||
function restore_settings_form_validate($form_values) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the settings form. Any values returned will be saved.
|
||||
*/
|
||||
function restore_settings_form_submit($form_values) {
|
||||
return $form_values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new location of the correct type.
|
||||
*/
|
||||
function create($params = array()) {
|
||||
$out = NULL;
|
||||
$types = backup_migrate_get_location_subtypes();
|
||||
// Get the type passed in in the params, or if none, check the url for a valid type name.
|
||||
// This is to allow new location type to be specified in the path.
|
||||
$location_type = !empty($params['subtype']) ? $params['subtype'] : NULL;
|
||||
|
||||
if ($location_type && ($type = @$types[$location_type])) {
|
||||
// Include the necessary file if specified by the type.
|
||||
if (!empty($type['file'])) {
|
||||
require_once './'. $type['file'];
|
||||
}
|
||||
$out = new $type['class']($params + array('subtype' => $location_type));
|
||||
}
|
||||
|
||||
if (empty($out)) {
|
||||
$out = parent::create($params);
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a url from the parts.
|
||||
*/
|
||||
function url($hide_password = TRUE) {
|
||||
return $this->glue_url($this->dest_url, $hide_password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Glue a URLs component parts back into a URL.
|
||||
*/
|
||||
function glue_url($parts, $hide_password = TRUE) {
|
||||
// Obscure the password if we need to.
|
||||
$parts['pass'] = $hide_password ? "" : $parts['pass'];
|
||||
|
||||
// Assemble the URL.
|
||||
$out = "";
|
||||
$out .= $parts['scheme'] .'://';
|
||||
$out .= $parts['user'] ? urlencode($parts['user']) : '';
|
||||
$out .= ($parts['user'] && $parts['pass']) ? ":". urlencode($parts['pass']) : '';
|
||||
$out .= ($parts['user'] || $parts['pass']) ? "@" : "";
|
||||
$out .= $parts['host'];
|
||||
$out .= !empty($parts['port']) ? ':'. $parts['port'] : '';
|
||||
$out .= "/". $parts['path'];
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Break a URL into it's component parts.
|
||||
*/
|
||||
function set_url($url) {
|
||||
$parts = (array)parse_url($url);
|
||||
$parts['user'] = urldecode(@$parts['user']);
|
||||
$parts['pass'] = urldecode(@$parts['pass']);
|
||||
$parts['path'] = urldecode(@$parts['path']);
|
||||
$parts['path'] = ltrim(@$parts['path'], "/");
|
||||
$this->dest_url = $parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a list of filetypes supported by this source/destination.
|
||||
*/
|
||||
function file_types() {
|
||||
return array();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A base class for creating locations.
|
||||
*/
|
||||
class backup_migrate_location_remote extends backup_migrate_location {
|
||||
/**
|
||||
* The location is a URI so parse it and store the parts.
|
||||
*/
|
||||
function get_location() {
|
||||
return $this->url(FALSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* The location to display is the url without the password.
|
||||
*/
|
||||
function get_display_location() {
|
||||
return $this->url(TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the location with the password.
|
||||
*/
|
||||
function set_location($location) {
|
||||
$this->location = $location;
|
||||
$this->set_url($location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a url from the parts.
|
||||
*/
|
||||
function url($hide_password = TRUE) {
|
||||
return $this->glue_url($this->dest_url, $hide_password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Glue a URLs component parts back into a URL.
|
||||
*/
|
||||
function glue_url($parts, $hide_password = TRUE) {
|
||||
// Obscure the password if we need to.
|
||||
$parts['pass'] = $hide_password ? "" : $parts['pass'];
|
||||
|
||||
// Assemble the URL.
|
||||
$out = "";
|
||||
$out .= $parts['scheme'] .'://';
|
||||
$out .= $parts['user'] ? urlencode($parts['user']) : '';
|
||||
$out .= ($parts['user'] && $parts['pass']) ? ":". urlencode($parts['pass']) : '';
|
||||
$out .= ($parts['user'] || $parts['pass']) ? "@" : "";
|
||||
$out .= $parts['host'];
|
||||
$out .= !empty($parts['port']) ? ':'. $parts['port'] : '';
|
||||
$out .= "/". $parts['path'];
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Break a URL into it's component parts.
|
||||
*/
|
||||
function set_url($url) {
|
||||
$parts = (array)parse_url($url);
|
||||
$parts['user'] = urldecode(@$parts['user']);
|
||||
$parts['pass'] = urldecode(@$parts['pass']);
|
||||
$parts['path'] = urldecode(@$parts['path']);
|
||||
$parts['path'] = ltrim(@$parts['path'], "/");
|
||||
$this->dest_url = $parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* location configuration callback.
|
||||
*/
|
||||
function edit_form() {
|
||||
$form = parent::edit_form();
|
||||
$form['scheme'] = array(
|
||||
"#type" => "select",
|
||||
"#title" => t("Scheme"),
|
||||
"#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : 'mysql',
|
||||
"#required" => TRUE,
|
||||
"#options" => array($GLOBALS['db_type'] => $GLOBALS['db_type']),
|
||||
"#weight" => 0,
|
||||
);
|
||||
$form['host'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Host"),
|
||||
"#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost',
|
||||
"#required" => TRUE,
|
||||
"#weight" => 10,
|
||||
);
|
||||
$form['path'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Path"),
|
||||
"#default_value" => @$this->dest_url['path'],
|
||||
"#required" => TRUE,
|
||||
"#weight" => 20,
|
||||
);
|
||||
$form['user'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Username"),
|
||||
"#default_value" => @$this->dest_url['user'],
|
||||
"#required" => TRUE,
|
||||
"#weight" => 30,
|
||||
);
|
||||
$form['pass'] = array(
|
||||
"#type" => "password",
|
||||
"#title" => t("Password"),
|
||||
"#default_value" => @$this->dest_url['pass'],
|
||||
'#description' => '',
|
||||
"#weight" => 40,
|
||||
);
|
||||
if (@$this->dest_url['pass']) {
|
||||
$form['old_password'] = array(
|
||||
"#type" => "value",
|
||||
"#value" => @$this->dest_url['pass'],
|
||||
);
|
||||
$form['pass']["#description"] .= t(' You do not need to enter a password unless you wish to change the currently saved password.');
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the configuration form. Glue the url together and add the old password back if a new one was not specified.
|
||||
*/
|
||||
function edit_form_submit($form, &$form_state) {
|
||||
$form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password'];
|
||||
$form_state['values']['location'] = $this->glue_url($form_state['values'], FALSE);
|
||||
parent::edit_form_submit($form, $form_state);
|
||||
}
|
||||
}
|
||||
|
388
sites/all/modules/backup_migrate/includes/profiles.inc
Normal file
388
sites/all/modules/backup_migrate/includes/profiles.inc
Normal file
|
@ -0,0 +1,388 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* All of the settings profiles handling code for Backup and Migrate.
|
||||
*/
|
||||
|
||||
backup_migrate_include('crud');
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of hook_backup_migrate_profile_subtypes().
|
||||
*
|
||||
* Get the built in Backup and Migrate profile types.
|
||||
*/
|
||||
function backup_migrate_backup_migrate_profile_subtypes() {
|
||||
$out = array();
|
||||
$out += array(
|
||||
'backup' => array(
|
||||
'include' => 'profiles',
|
||||
'type_name' => t('Backup Settings Profile'),
|
||||
'class' => 'backup_migrate_profile',
|
||||
),
|
||||
);
|
||||
return $out;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all the available backup profiles.
|
||||
*/
|
||||
function backup_migrate_get_profiles() {
|
||||
backup_migrate_include('filters');
|
||||
$profiles = &drupal_static('backup_migrate_get_profiles', NULL);
|
||||
|
||||
// Get the list of profiles and cache them locally.
|
||||
if ($profiles === NULL) {
|
||||
$profiles = backup_migrate_crud_get_items('profile');
|
||||
}
|
||||
return $profiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_backup_migrate_profiles_alter().
|
||||
*
|
||||
* Add default settings for any plugins which didn't exist when the profile was saved.
|
||||
*/
|
||||
function backup_migrate_backup_migrate_profiles_alter(&$profiles) {
|
||||
foreach ($profiles as $id => $profile) {
|
||||
// Set the default values for filter setting which don't exist in the profile.
|
||||
$profiles[$id]->filters = (array)@$profile->filters + (array)backup_migrate_filters_settings_default('backup');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the profile info for the profile with the given ID, or NULL if none exists.
|
||||
*/
|
||||
function backup_migrate_get_profile($profile_id) {
|
||||
$profiles = backup_migrate_get_profiles();
|
||||
return @$profiles[$profile_id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_backup_migrate_profiles().
|
||||
*/
|
||||
function backup_migrate_backup_migrate_profiles() {
|
||||
$out = array();
|
||||
|
||||
// Get the module default profile.
|
||||
$out['default'] = backup_migrate_crud_create_item('profile', array('name' => t("Default Settings"), 'machine_name' => 'default'));
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/* Utilities */
|
||||
|
||||
/**
|
||||
* Get the available profiles as an options array for a form item.
|
||||
*/
|
||||
function _backup_migrate_get_profile_form_item_options() {
|
||||
$out = array();
|
||||
foreach ((array)backup_migrate_get_profiles() as $key => $profile) {
|
||||
$out[$key] = $profile->get('name');
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a form to configure the profile.
|
||||
*/
|
||||
function _backup_migrate_ui_backup_settings_form($profile) {
|
||||
drupal_add_js(array('backup_migrate' => array('checkboxLinkText' => t('View as checkboxes'))), array('type' => 'setting'));
|
||||
drupal_add_js(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.js', array('type' => 'file', 'scope' => 'footer'));
|
||||
drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css');
|
||||
|
||||
backup_migrate_include('files', 'destinations', 'filters');
|
||||
|
||||
$form = array();
|
||||
|
||||
$form['file'] = array(
|
||||
"#type" => "fieldset",
|
||||
"#title" => t("Backup File"),
|
||||
"#collapsible" => TRUE,
|
||||
"#collapsed" => FALSE,
|
||||
"#tree" => FALSE,
|
||||
);
|
||||
$form['file']['filename'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Backup file name"),
|
||||
"#default_value" => $profile->filename,
|
||||
);
|
||||
if (module_exists('token')) {
|
||||
$form['file']['token_help'] = array(
|
||||
'#title' => t('Replacement patterns'),
|
||||
'#type' => 'fieldset',
|
||||
'#collapsible' => TRUE,
|
||||
'#collapsed' => TRUE,
|
||||
);
|
||||
$form['file']['token_help']['help'] = array(
|
||||
'#theme' => 'token_tree',
|
||||
'#token_types' => array('current-date', 'site'),
|
||||
'#global_types' => FALSE,
|
||||
);
|
||||
$form['file']['filename']['#description'] = t('You can use tokens in the file name.');
|
||||
}
|
||||
|
||||
$form['file']['append_timestamp'] = array(
|
||||
"#type" => "checkbox",
|
||||
"#title" => t("Append a timestamp."),
|
||||
"#default_value" => $profile->append_timestamp,
|
||||
);
|
||||
$form['file']['timestamp_format_wrapper'] = array(
|
||||
'#type' => 'backup_migrate_dependent',
|
||||
'#dependencies' => array(
|
||||
'append_timestamp' => TRUE,
|
||||
),
|
||||
);
|
||||
$form['file']['timestamp_format_wrapper']['timestamp_format'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Timestamp format"),
|
||||
"#default_value" => $profile->timestamp_format,
|
||||
"#description" => t('Should be a PHP <a href="!url">date()</a> format string.', array('!url' => 'http://www.php.net/date')),
|
||||
);
|
||||
|
||||
$form['advanced'] = array('#weight' => 10);
|
||||
$form = array_merge_recursive($form, backup_migrate_filters_settings_form($profile->filters, 'backup'));
|
||||
|
||||
// Add the advanced fieldset if there are any fields in it.
|
||||
if ($form['advanced']) {
|
||||
$form['advanced']['#type'] = 'fieldset';
|
||||
$form['advanced']['#title'] = t('Advanced Options');
|
||||
$form['advanced']['#collapsed'] = true;
|
||||
$form['advanced']['#collapsible'] = true;
|
||||
}
|
||||
|
||||
$form['#validate'][] = '_backup_migrate_ui_backup_settings_form_validate';
|
||||
$form['#submit'][] = '_backup_migrate_ui_backup_settings_form_submit';
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the profile form.
|
||||
*/
|
||||
function _backup_migrate_ui_backup_settings_form_validate($form, &$form_state) {
|
||||
backup_migrate_filters_settings_form_validate('backup', $form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the profile form.
|
||||
*/
|
||||
function _backup_migrate_ui_backup_settings_form_submit($form, &$form_state) {
|
||||
backup_migrate_filters_settings_form_submit('backup', $form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default profile.
|
||||
*/
|
||||
function _backup_migrate_profile_default_profile() {
|
||||
backup_migrate_include('files', 'filters');
|
||||
return array(
|
||||
'source_id' => 'db',
|
||||
'filename' => _backup_migrate_default_filename(),
|
||||
'append_timestamp' => 1,
|
||||
'timestamp_format' => 'Y-m-d\TH-i-s',
|
||||
'filters' => backup_migrate_filters_settings_default('backup'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default profile saved by the user (or the module default if none exists).
|
||||
*/
|
||||
function _backup_migrate_profile_saved_default_profile($profile_id = NULL) {
|
||||
$profile_id = $profile_id ? $profile_id : variable_get("backup_migrate_profile_id", 'default');
|
||||
$profile = NULL;
|
||||
if ($profile_id) {
|
||||
$profile = backup_migrate_get_profile($profile_id);
|
||||
}
|
||||
if (!$profile) {
|
||||
$profile = backup_migrate_get_profile('default');
|
||||
}
|
||||
return $profile;
|
||||
}
|
||||
|
||||
/**
|
||||
* A profile class for crud operations.
|
||||
*/
|
||||
class backup_migrate_profile extends backup_migrate_item {
|
||||
var $db_table = "backup_migrate_profiles";
|
||||
var $type_name = "profile";
|
||||
var $singular = 'settings profile';
|
||||
var $plural = 'settings profiles';
|
||||
var $title_plural = 'Settings Profiles';
|
||||
var $title_singular = 'Settings Profile';
|
||||
|
||||
/**
|
||||
* Perform a shallow merge of the defaults and the parameters.
|
||||
*
|
||||
* This is needed because otherwise it will *combine* the nested arrays and
|
||||
* make it impossible to deselect database tables from the 'nodata' setting.
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function __construct(array $params = array()) {
|
||||
$params = (array)$params;
|
||||
$defaults = (array)$this->get_default_values();
|
||||
foreach ($defaults as $key => $val) {
|
||||
if (!isset($params[$key])) {
|
||||
$params[$key] = $val;
|
||||
}
|
||||
}
|
||||
$this->from_array($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is not supposed to be called. It is just here to help the po extractor out.
|
||||
*/
|
||||
function strings() {
|
||||
// Help the pot extractor find these strings.
|
||||
t('Settings Profile');
|
||||
t('Settings Profiles');
|
||||
t('settings profile');
|
||||
t('settings profiles');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default values for standard parameters.
|
||||
*/
|
||||
function get_default_values() {
|
||||
return _backup_migrate_profile_default_profile() + array('name' => t("Untitled Profile"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a table of all items of this type.
|
||||
*/
|
||||
function get_list() {
|
||||
drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css');
|
||||
return parent::get_list();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the columns needed to list the type.
|
||||
*/
|
||||
function get_list_column_info() {
|
||||
$out = parent::get_list_column_info();
|
||||
$out = array(
|
||||
'name' => array('title' => t('Name')),
|
||||
'source_name' => array('title' => t('Source')),
|
||||
'filename' => array('title' => t('Filename')),
|
||||
) + $out;
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the source of this setings profile. Takes either a source object or source id.
|
||||
*/
|
||||
function set_source($source) {
|
||||
if (is_object($source)) {
|
||||
$this->source = $source;
|
||||
$this->source_id = $source->get_id();
|
||||
}
|
||||
else {
|
||||
$this->source_id = $source;
|
||||
unset($this->source);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the source of the profile.
|
||||
*/
|
||||
function get_source() {
|
||||
backup_migrate_include('locations');
|
||||
if (!empty($this->source_id) && (empty($this->source) || $this->source->get_id() !== $this->source_id)) {
|
||||
$this->source = backup_migrate_get_source($this->source_id);
|
||||
}
|
||||
return empty($this->source) ? NULL : $this->source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the source.
|
||||
*/
|
||||
function get_source_name() {
|
||||
if ($source = $this->get_source()) {
|
||||
return $source->get_name();
|
||||
}
|
||||
return t("Missing");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the destination of the profile.
|
||||
*/
|
||||
function get_destination() {
|
||||
$destinations = (array)$this->get_destinations();
|
||||
return reset($destinations);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the destination of the profile.
|
||||
*/
|
||||
function get_destinations() {
|
||||
backup_migrate_include('destinations');
|
||||
if (empty($this->destinations)) {
|
||||
$this->destinations = array();
|
||||
$ids = $weights = array();
|
||||
if (!empty($this->destination_id)) {
|
||||
foreach ((array)$this->destination_id as $destination_id) {
|
||||
if (!in_array($destination_id, $ids) && $destination = backup_migrate_get_destination($destination_id)) {
|
||||
$this->destinations[] = $destination;
|
||||
$weights[] = $destination->get('weight');
|
||||
$ids[] = $destination_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Sort the destinations by weight.
|
||||
array_multisort($weights, SORT_NUMERIC, $this->destinations);
|
||||
}
|
||||
|
||||
return $this->destinations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the destination.
|
||||
*/
|
||||
function get_destination_name() {
|
||||
$out = array();
|
||||
foreach ($this->get_destinations() as $destination) {
|
||||
$out[] = $destination->get_name();
|
||||
}
|
||||
if ($out) {
|
||||
return implode(', ', $out);
|
||||
}
|
||||
return t("Missing");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the source and destinations specified in the given settings profile
|
||||
*/
|
||||
function get_all_locations() {
|
||||
$out = array();
|
||||
$out += $this->get('destinations');
|
||||
$out[] = $this->get('source');
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the edit form.
|
||||
*/
|
||||
function edit_form() {
|
||||
$form = parent::edit_form();
|
||||
$form['name'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Profile Name"),
|
||||
'#required' => TRUE,
|
||||
"#default_value" => $this->get('name'),
|
||||
);
|
||||
$form += _backup_migrate_ui_backup_settings_form($this);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message to send to the user when confirming the deletion of the item.
|
||||
*/
|
||||
function delete_confirm_message() {
|
||||
return t('Are you sure you want to delete the profile %name? Any schedules using this profile will be disabled.', array('%name' => $this->get('name')));
|
||||
}
|
||||
}
|
||||
|
807
sites/all/modules/backup_migrate/includes/schedules.inc
Normal file
807
sites/all/modules/backup_migrate/includes/schedules.inc
Normal file
|
@ -0,0 +1,807 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* All of the schedule handling code needed for Backup and Migrate.
|
||||
*/
|
||||
|
||||
define('BACKUP_MIGRATE_KEEP_ALL', 0);
|
||||
define('BACKUP_MIGRATE_STANDARD_DELETE', -1);
|
||||
define('BACKUP_MIGRATE_SMART_DELETE', -2);
|
||||
|
||||
define('BACKUP_MIGRATE_CRON_BUILTIN', 'builtin');
|
||||
define('BACKUP_MIGRATE_CRON_ELYSIA', 'elysia');
|
||||
define('BACKUP_MIGRATE_CRON_NONE', 'none');
|
||||
|
||||
|
||||
define('BACKUP_MIGRATE_SMART_KEEP_SUBHOURLY', 3600);
|
||||
define('BACKUP_MIGRATE_SMART_KEEP_HOURLY', 24);
|
||||
define('BACKUP_MIGRATE_SMART_KEEP_DAILY', 30);
|
||||
define('BACKUP_MIGRATE_SMART_KEEP_WEEKLY', PHP_INT_MAX);
|
||||
|
||||
define('BACKUP_MIGRATE_KEEP_DEFAULT', 100);
|
||||
|
||||
backup_migrate_include('crud');
|
||||
|
||||
/**
|
||||
* Implementation of hook_backup_migrate_destination_types().
|
||||
*
|
||||
* Get the built in Backup and Migrate destination types.
|
||||
*/
|
||||
function backup_migrate_backup_migrate_schedule_types() {
|
||||
$out = array();
|
||||
$out += array(
|
||||
'backup' => array(
|
||||
'include' => 'schedule',
|
||||
'type_name' => t('Backup Schedule'),
|
||||
'class' => 'backup_migrate_schedule',
|
||||
),
|
||||
);
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the preconfigured schedules. Called on cron.
|
||||
*/
|
||||
function backup_migrate_schedules_cron() {
|
||||
backup_migrate_include('profiles');
|
||||
foreach (backup_migrate_get_schedules() as $schedule) {
|
||||
$schedule->cron();
|
||||
}
|
||||
backup_migrate_cleanup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the preconfigured schedules regardless of scheduled time settings.
|
||||
*/
|
||||
function backup_migrate_schedules_run() {
|
||||
backup_migrate_include('profiles');
|
||||
foreach (backup_migrate_get_schedules() as $schedule) {
|
||||
$schedule->run();
|
||||
}
|
||||
backup_migrate_cleanup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the preconfigured schedules. Called on cron.
|
||||
*/
|
||||
function backup_migrate_schedule_run($schedule_id) {
|
||||
backup_migrate_include('profiles');
|
||||
if (($schedule = backup_migrate_get_schedule($schedule_id)) && $schedule->is_enabled()) {
|
||||
$schedule->run();
|
||||
}
|
||||
backup_migrate_cleanup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the available backup schedules.
|
||||
*/
|
||||
function backup_migrate_get_schedules() {
|
||||
$schedules = &drupal_static('backup_migrate_get_schedules');
|
||||
// Get the list of schedules and cache them locally.
|
||||
if ($schedules === NULL) {
|
||||
$schedules = backup_migrate_crud_get_items('schedule');
|
||||
}
|
||||
return $schedules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the schedule info for the schedule with the given ID, or NULL if none exists.
|
||||
*/
|
||||
function backup_migrate_get_schedule($schedule_id) {
|
||||
$schedules = backup_migrate_get_schedules();
|
||||
return @$schedules[$schedule_id];
|
||||
}
|
||||
|
||||
/**
|
||||
* A schedule class for crud operations.
|
||||
*/
|
||||
class backup_migrate_schedule extends backup_migrate_item {
|
||||
var $db_table = "backup_migrate_schedules";
|
||||
var $type_name = 'schedule';
|
||||
var $singular = 'schedule';
|
||||
var $plural = 'schedules';
|
||||
var $title_plural = 'Schedules';
|
||||
var $title_singular = 'Schedule';
|
||||
var $default_values = array();
|
||||
|
||||
/**
|
||||
* This function is not supposed to be called. It is just here to help the po extractor out.
|
||||
*/
|
||||
function strings() {
|
||||
// Help the pot extractor find these strings.
|
||||
t('Schedule');
|
||||
t('Schedules');
|
||||
t('schedule');
|
||||
t('schedules');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default values for this item.
|
||||
*/
|
||||
function get_default_values() {
|
||||
return array(
|
||||
'name' => t("Untitled Schedule"),
|
||||
'source_id' => 'db',
|
||||
'enabled' => 1,
|
||||
'keep' => BACKUP_MIGRATE_KEEP_ALL,
|
||||
'period' => 60 * 60 * 24,
|
||||
'storage' => BACKUP_MIGRATE_STORAGE_NONE,
|
||||
'cron' => BACKUP_MIGRATE_CRON_BUILTIN,
|
||||
'cron_schedule' => '0 4 * * *',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return as an array of values.
|
||||
*/
|
||||
function to_array() {
|
||||
$out = parent::to_array();
|
||||
unset($out['last_run']);
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the columns needed to list the type.
|
||||
*/
|
||||
function get_list_column_info() {
|
||||
$out = parent::get_list_column_info();
|
||||
$out = array(
|
||||
'name' => array('title' => t('Name')),
|
||||
'destination_name' => array('title' => t('Destinations'), 'html' => TRUE),
|
||||
'profile_name' => array('title' => t('Profile'), 'html' => TRUE),
|
||||
'frequency_description' => array('title' => t('Frequency')),
|
||||
'keep_description' => array('title' => t('Keep')),
|
||||
'enabled_description' => array('title' => t('Enabled')),
|
||||
'last_run_description' => array('title' => t('Last run')),
|
||||
) + $out;
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the columns needed to list the type.
|
||||
*/
|
||||
function get_settings_path() {
|
||||
// Pull the schedules tab up a level to the top.
|
||||
return BACKUP_MIGRATE_MENU_PATH . '/' . $this->type_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the menu items for manipulating this type.
|
||||
*/
|
||||
function get_menu_items() {
|
||||
$items = parent::get_menu_items();
|
||||
$path = $this->get_settings_path();
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a row of data to be used in a list of items of this type.
|
||||
*/
|
||||
function get_list_row() {
|
||||
drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css');
|
||||
$row = parent::get_list_row();
|
||||
if (!$this->is_enabled()) {
|
||||
foreach ($row as $key => $field) {
|
||||
if (!is_array($field)) {
|
||||
$row[$key] = array('data' => $field, 'class' => 'schedule-list-disabled');
|
||||
}
|
||||
else if (isset($row[$key]['class'])) {
|
||||
$row[$key]['class'] .= ' schedule-list-disabled';
|
||||
}
|
||||
else {
|
||||
$row[$key]['class'] = 'schedule-list-disabled';
|
||||
}
|
||||
}
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the schedule enabled and valid.
|
||||
*/
|
||||
function is_enabled() {
|
||||
$destination = $this->get_destination();
|
||||
$profile = $this->get_profile();
|
||||
return (!empty($this->enabled) && !empty($destination) && !empty($profile));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the destination object of the schedule.
|
||||
*/
|
||||
function get_destination() {
|
||||
$destinations = (array)$this->get_destinations();
|
||||
return reset($destinations);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the destination object of the schedule.
|
||||
*/
|
||||
function get_destination_ids() {
|
||||
$out = array();
|
||||
foreach (array('destination_id', 'copy_destination_id') as $key) {
|
||||
if ($id = $this->get($key)) {
|
||||
$out[$key] = $id;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the destination object of the schedule.
|
||||
*/
|
||||
function get_destinations() {
|
||||
backup_migrate_include('destinations');
|
||||
$out = array();
|
||||
foreach ($this->get_destination_ids() as $id) {
|
||||
if ($dest = backup_migrate_get_destination($id)) {
|
||||
$out[$id] = $dest;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the destination object of the schedule.
|
||||
*/
|
||||
function get_destination_remote() {
|
||||
backup_migrate_include('destinations');
|
||||
return backup_migrate_get_destination($this->get('destination_remote_id'));
|
||||
}
|
||||
/**
|
||||
* Get the destination object of the schedule.
|
||||
*/
|
||||
function get_destination_local() {
|
||||
backup_migrate_include('destinations');
|
||||
return backup_migrate_get_destination($this->get('destination_local_id'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the destination.
|
||||
*/
|
||||
function get_destination_name() {
|
||||
if ($destinations = $this->get_destinations()) {
|
||||
$out = array();
|
||||
foreach ((array)$destinations as $destination) {
|
||||
$out[] = check_plain($destination->get_name());
|
||||
}
|
||||
return implode(', ', $out);
|
||||
}
|
||||
return '<div class="row-error">'. t("Missing") .'</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the destination of the schedule.
|
||||
*/
|
||||
function get_profile() {
|
||||
backup_migrate_include('profiles');
|
||||
if ($settings = backup_migrate_get_profile($this->get('profile_id'))) {
|
||||
$settings->file_info = empty($settings->file_info) ? array() : $settings->file_info;
|
||||
$settings->file_info += array(
|
||||
'schedule_id' => $this->get_id(),
|
||||
'schedule_name' => $this->get('name'),
|
||||
);
|
||||
}
|
||||
return $settings;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the source.
|
||||
*/
|
||||
function get_profile_name() {
|
||||
if ($profile = $this->get_profile()) {
|
||||
return check_plain($profile->get_name());
|
||||
}
|
||||
return '<div class="row-error">'. t("Missing") .'</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a frequency in human-readable form.
|
||||
*/
|
||||
function get_frequency_description() {
|
||||
$period = $this->get_frequency_period();
|
||||
$cron = $this->get('cron');
|
||||
if ($cron == BACKUP_MIGRATE_CRON_BUILTIN) {
|
||||
$out = format_plural(($this->period / $period['seconds']), $period['singular'], $period['plural']);
|
||||
}
|
||||
else if ($cron == BACKUP_MIGRATE_CRON_ELYSIA) {
|
||||
$out = $this->get('cron_schedule');
|
||||
}
|
||||
else {
|
||||
$out = t('None');
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the number to keep in human-readable form.
|
||||
*/
|
||||
function get_keep_description() {
|
||||
return $this->generate_keep_description($this->keep);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a number to keep in human readable from
|
||||
*/
|
||||
function generate_keep_description($keep, $terse = TRUE) {
|
||||
if ($keep == BACKUP_MIGRATE_KEEP_ALL) {
|
||||
return t('all backups');
|
||||
}
|
||||
else if ($keep == BACKUP_MIGRATE_SMART_DELETE) {
|
||||
$keep_hourly = variable_get('backup_migrate_smart_keep_hourly', BACKUP_MIGRATE_SMART_KEEP_HOURLY);
|
||||
$keep_daily = variable_get('backup_migrate_smart_keep_daily', BACKUP_MIGRATE_SMART_KEEP_DAILY);
|
||||
$keep_weekly = variable_get('backup_migrate_smart_keep_weekly', BACKUP_MIGRATE_SMART_KEEP_WEEKLY);
|
||||
if ($terse) {
|
||||
return t('!hours hourly, !days daily, !weeks weekly backups',
|
||||
array(
|
||||
'!hours' => $keep_hourly == PHP_INT_MAX ? t('all') : $keep_hourly,
|
||||
'!days' => $keep_daily == PHP_INT_MAX ? t('all') : $keep_daily,
|
||||
'!weeks' => $keep_weekly == PHP_INT_MAX ? t('all') : $keep_weekly,
|
||||
));
|
||||
}
|
||||
else {
|
||||
return t('hourly backups !hours, daily backups !days and weekly backups !weeks',
|
||||
array(
|
||||
'!hours' => $keep_hourly == PHP_INT_MAX ? t('forever') : format_plural($keep_hourly, 'for 1 hour', 'for the past @count hours'),
|
||||
'!days' => $keep_daily == PHP_INT_MAX ? t('forever') : format_plural($keep_daily, 'for 1 day', 'for the past @count days'),
|
||||
'!weeks' => $keep_weekly == PHP_INT_MAX ? t('forever') : format_plural($keep_weekly, 'for 1 week', 'for the past @count weeks'),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return format_plural($keep, 'last 1 backup', 'last @count backups');
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the enabled status in human-readable form.
|
||||
*/
|
||||
function get_enabled_description() {
|
||||
return !empty($this->enabled) ? t('Enabled') : t('Disabled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the enabled status in human-readable form.
|
||||
*/
|
||||
function get_last_run_description() {
|
||||
$last_run = $this->get('last_run');
|
||||
return !empty($last_run) ? format_date($last_run, 'small') : t('Never');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of excluded tables.
|
||||
*/
|
||||
function get_exclude_tables_count() {
|
||||
return count($this->exclude_tables) ? count($this->exclude_tables) : t("No tables excluded");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of excluded tables.
|
||||
*/
|
||||
function get_nodata_tables_count() {
|
||||
return count($this->nodata_tables) ? count($this->nodata_tables) : t("No data omitted");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the edit form.
|
||||
*/
|
||||
function edit_form() {
|
||||
$form = parent::edit_form();
|
||||
backup_migrate_include('destinations', 'sources', 'profiles');
|
||||
|
||||
$form['name'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Schedule Name"),
|
||||
"#default_value" => $this->get('name'),
|
||||
);
|
||||
|
||||
$form += _backup_migrate_get_source_form($this->get('source_id'));
|
||||
|
||||
$form['profile_id'] = array(
|
||||
"#type" => "select",
|
||||
"#title" => t("Settings Profile"),
|
||||
"#options" => _backup_migrate_get_profile_form_item_options(),
|
||||
"#default_value" => $this->get('profile_id'),
|
||||
);
|
||||
$form['profile_id']['#description'] = ' '. l(t('Create new profile'), BACKUP_MIGRATE_MENU_PATH . '/settings/profile/add');
|
||||
if (!$form['profile_id']['#options']) {
|
||||
$form['profile_id']['#options'] = array('0' => t('-- None Available --'));
|
||||
}
|
||||
|
||||
$period_options = array();
|
||||
foreach ($this->frequency_periods() as $type => $period) {
|
||||
$period_options[$type] = $period['title'];
|
||||
}
|
||||
$default_period = $this->get_frequency_period();
|
||||
$default_period_num = $this->get('period') / $default_period['seconds'];
|
||||
|
||||
$form['enabled'] = array(
|
||||
'#type' => "checkbox",
|
||||
'#title' => t('Enabled'),
|
||||
'#default_value' => $this->get('enabled'),
|
||||
);
|
||||
$form['cron_settings'] = array(
|
||||
'#type' => 'backup_migrate_dependent',
|
||||
'#dependencies' => array(
|
||||
'enabled' => TRUE,
|
||||
),
|
||||
);
|
||||
|
||||
$cron = $this->get('cron');
|
||||
$form['cron_settings']['cron_builtin'] = array(
|
||||
"#type" => "radio",
|
||||
"#title" => t('Run using Drupal\'s cron'),
|
||||
'#return_value' => BACKUP_MIGRATE_CRON_BUILTIN,
|
||||
"#description" => t('Run this schedule when !cron runs.', array('!cron' => l(t('your cron task'), 'http://drupal.org/cron'))),
|
||||
"#default_value" => $cron ? $cron : BACKUP_MIGRATE_CRON_BUILTIN,
|
||||
'#parents' => array('cron'),
|
||||
);
|
||||
|
||||
$form['cron_settings']['period_settings'] = array(
|
||||
'#type' => 'backup_migrate_dependent',
|
||||
'#dependencies' => array(
|
||||
'cron' => BACKUP_MIGRATE_CRON_BUILTIN,
|
||||
),
|
||||
);
|
||||
$form['cron_settings']['period_settings']['period'] = array(
|
||||
"#type" => "item",
|
||||
"#title" => t("Backup every"),
|
||||
"#prefix" => '<div class="container-inline">',
|
||||
"#suffix" => '</div>',
|
||||
"#tree" => TRUE,
|
||||
'#parents' => array('period'),
|
||||
);
|
||||
$form['cron_settings']['period_settings']['period']['number'] = array(
|
||||
"#type" => "textfield",
|
||||
"#size" => 6,
|
||||
"#default_value" => $default_period_num,
|
||||
'#parents' => array('period', 'number'),
|
||||
);
|
||||
$form['cron_settings']['period_settings']['period']['type'] = array(
|
||||
"#type" => "select",
|
||||
"#options" => $period_options,
|
||||
"#default_value" => $default_period['type'],
|
||||
'#parents' => array('period', 'type'),
|
||||
);
|
||||
|
||||
|
||||
$form['cron_settings']['cron_elysia'] = array(
|
||||
"#type" => "radio",
|
||||
"#title" => t('Run using Elysia cron'),
|
||||
'#return_value' => BACKUP_MIGRATE_CRON_ELYSIA,
|
||||
"#description" => t('You can specify exactly when this schedule should run using !elysia.', array('!elysia' => l(t('the Elysia Cron module'), 'http://drupal.org/project/elysia_cron'))),
|
||||
"#default_value" => $cron ? $cron : BACKUP_MIGRATE_CRON_BUILTIN,
|
||||
'#parents' => array('cron'),
|
||||
);
|
||||
if (!module_exists('elysia_cron') && !module_exists('ultimate_cron')) {
|
||||
$form['cron_settings']['cron_elysia']['#disabled'] = TRUE;
|
||||
$form['cron_settings']['cron_elysia']['#description'] .= ' ' . t('Install !elysia to enable this option.', array('!elysia' => l(t('Elysia Cron'), 'http://drupal.org/project/elysia_cron')));
|
||||
}
|
||||
$form['cron_settings']['cron_schedule_settings'] = array(
|
||||
'#type' => 'backup_migrate_dependent',
|
||||
'#dependencies' => array(
|
||||
'cron' => BACKUP_MIGRATE_CRON_ELYSIA,
|
||||
),
|
||||
);
|
||||
$form['cron_settings']['cron_schedule_settings']['cron_schedule'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t('Cron Schedule'),
|
||||
'#length' => 10,
|
||||
"#description" => t('Specify the frequecy of the schedule using standard cron notation. For more information see the !elysiareadme.', array('!elysiareadme' => l(t('the Elysia Cron README'), 'http://drupalcode.org/project/elysia_cron.git/blob/refs/heads/7.x-1.x:/README.txt'))),
|
||||
"#default_value" => $this->get('cron_schedule'),
|
||||
'#parents' => array('cron_schedule'),
|
||||
);
|
||||
|
||||
$form['cron_settings']['cron_none'] = array(
|
||||
"#type" => "radio",
|
||||
"#title" => t('Do not run automatically'),
|
||||
'#return_value' => 'none',
|
||||
"#description" => t('Do not run this schedule automatically. You can still run it using !drush.', array('!drush' => l(t('Drush'), 'http://drupal.org/project/drush'))),
|
||||
"#default_value" => $cron ? $cron : BACKUP_MIGRATE_CRON_BUILTIN,
|
||||
'#parents' => array('cron'),
|
||||
);
|
||||
|
||||
|
||||
|
||||
$keep = $this->get('keep');
|
||||
$form['delete'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#default_value' => $keep != 0,
|
||||
'#title' => t('Automatically delete old backups'),
|
||||
);
|
||||
$form['delete_settings'] = array(
|
||||
'#type' => 'backup_migrate_dependent',
|
||||
'#dependencies' => array(
|
||||
'delete' => TRUE,
|
||||
),
|
||||
);
|
||||
|
||||
$keep_hourly = variable_get('backup_migrate_smart_keep_hourly', BACKUP_MIGRATE_SMART_KEEP_HOURLY);
|
||||
$keep_daily = variable_get('backup_migrate_smart_keep_daily', BACKUP_MIGRATE_SMART_KEEP_DAILY);
|
||||
$keep_weekly = variable_get('backup_migrate_smart_keep_weekly', BACKUP_MIGRATE_SMART_KEEP_WEEKLY);
|
||||
$form['delete_settings']['smartdelete'] = array(
|
||||
"#type" => "radio",
|
||||
"#title" => t('Smart Delete'),
|
||||
'#return_value' => BACKUP_MIGRATE_SMART_DELETE,
|
||||
"#description" => t('Keep !keep. <strong>Recommended</strong>', array('!keep' => $this->generate_keep_description(BACKUP_MIGRATE_SMART_DELETE, FALSE))),
|
||||
"#default_value" => $keep ? $keep : BACKUP_MIGRATE_SMART_DELETE,
|
||||
'#parents' => array('deletetype'),
|
||||
);
|
||||
$form['delete_settings']['standarddelete'] = array(
|
||||
"#type" => "radio",
|
||||
"#title" => t('Simple Delete'),
|
||||
'#return_value' => BACKUP_MIGRATE_STANDARD_DELETE,
|
||||
"#description" => t("Keep a specified number of files deleting the oldest ones first."),
|
||||
"#default_value" => $keep > 0 ? BACKUP_MIGRATE_STANDARD_DELETE : 0,
|
||||
'#parents' => array('deletetype'),
|
||||
);
|
||||
$form['delete_settings']['keep-settings'] = array(
|
||||
'#type' => 'backup_migrate_dependent',
|
||||
'#dependencies' => array(
|
||||
'deletetype' => BACKUP_MIGRATE_STANDARD_DELETE,
|
||||
),
|
||||
);
|
||||
$form['delete_settings']['keep-settings']['keep'] = array(
|
||||
"#type" => "textfield",
|
||||
"#size" => 6,
|
||||
"#title" => t("Number of Backup files to keep"),
|
||||
"#description" => t("The number of backup files to keep before deleting old ones."),
|
||||
"#default_value" => $keep > 0 ? $keep : BACKUP_MIGRATE_KEEP_DEFAULT,
|
||||
);
|
||||
|
||||
$form['destination'] = _backup_migrate_get_destination_pulldown('scheduled backup', $this->get('destination_id'), $this->get('copy_destination_id'));
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the edit form.
|
||||
*/
|
||||
function edit_form_validate($form, &$form_state) {
|
||||
if (!is_numeric($form_state['values']['period']['number']) || $form_state['values']['period']['number'] <= 0) {
|
||||
form_set_error('period][number', t('Backup period must be a number greater than 0.'));
|
||||
}
|
||||
|
||||
if (!$form_state['values']['delete']) {
|
||||
$form_state['values']['keep'] = 0;
|
||||
}
|
||||
else if ($form_state['values']['deletetype'] == BACKUP_MIGRATE_SMART_DELETE) {
|
||||
$form_state['values']['keep'] = BACKUP_MIGRATE_SMART_DELETE;
|
||||
}
|
||||
else if (!is_numeric($form_state['values']['keep']) || $form_state['values']['keep'] <= 0) {
|
||||
form_set_error('keep', t('Number to keep must be a number greater than 0.'));
|
||||
}
|
||||
parent::edit_form_validate($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the edit form.
|
||||
*/
|
||||
function edit_form_submit($form, &$form_state) {
|
||||
$periods = $this->frequency_periods();
|
||||
$period = $periods[$form_state['values']['period']['type']];
|
||||
$form_state['values']['period'] = $form_state['values']['period']['number'] * $period['seconds'];
|
||||
parent::edit_form_submit($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the period of the frequency (ie: seconds, minutes etc.)
|
||||
*/
|
||||
function get_frequency_period() {
|
||||
foreach (array_reverse($this->frequency_periods()) as $period) {
|
||||
if ($period['seconds'] && ($this->period % $period['seconds']) === 0) {
|
||||
return $period;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of available backup periods. Only returns time periods which have a
|
||||
* (reasonably) consistent number of seconds (ie: no months).
|
||||
*/
|
||||
function frequency_periods() {
|
||||
return array(
|
||||
'seconds' => array('type' => 'seconds', 'seconds' => 1, 'title' => t('Seconds'), 'singular' => t('Once a second'), 'plural' => t('Every @count seconds')),
|
||||
'minutes' => array('type' => 'minutes', 'seconds' => 60, 'title' => t('Minutes'), 'singular' => t('Once a minute'), 'plural' => t('Every @count minutes')),
|
||||
'hours' => array('type' => 'hours', 'seconds' => 3600, 'title' => t('Hours'), 'singular' => t('Once an hour'), 'plural' => t('Every @count hours')),
|
||||
'days' => array('type' => 'days', 'seconds' => 86400, 'title' => t('Days'), 'singular' => t('Once a day'), 'plural' => t('Every @count days')),
|
||||
'weeks' => array('type' => 'weeks', 'seconds' => 604800, 'title' => t('Weeks'), 'singular' => t('Once a week'), 'plural' => t('Every @count weeks')),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message to send to the user when confirming the deletion of the item.
|
||||
*/
|
||||
function delete_confirm_message() {
|
||||
return t('Are you sure you want to delete the schedule %name? Backups made with this schedule will not be deleted.', array('%name' => $this->get('name')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the cron action. Run the backup if enough time has elapsed.
|
||||
*/
|
||||
function cron() {
|
||||
$now = time();
|
||||
|
||||
// Add a small negative buffer (1% of the entire period) to the time to account for slight difference in cron run length.
|
||||
$wait_time = $this->period - ($this->period * variable_get('backup_migrate_schedule_buffer', 0.01));
|
||||
|
||||
$cron = $this->get('cron');
|
||||
if ($cron == BACKUP_MIGRATE_CRON_BUILTIN && $this->is_enabled() && ($now - $this->get('last_run')) >= $wait_time) {
|
||||
$this->run();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the actual schedule.
|
||||
*/
|
||||
function run() {
|
||||
// Clear cached profile data which could have been altered by previous
|
||||
// schedule run; see #2672478
|
||||
drupal_static_reset('backup_migrate_get_profiles');
|
||||
|
||||
if ($settings = $this->get_profile()) {
|
||||
$settings->source_id = $this->get('source_id');
|
||||
$settings->destination_id = $this->get('destination_ids');
|
||||
|
||||
$this->update_last_run(time());
|
||||
backup_migrate_perform_backup($settings);
|
||||
$this->remove_expired_backups();
|
||||
}
|
||||
else {
|
||||
backup_migrate_backup_fail("Schedule '%schedule' could not be run because requires a profile which is missing.", array('%schedule' => $schedule->get_name()), $settings);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the last run time of a schedule to the given timestamp, or now if none specified.
|
||||
*/
|
||||
function update_last_run($timestamp = NULL) {
|
||||
if ($timestamp === NULL) {
|
||||
$timestamp = time();
|
||||
}
|
||||
variable_set('backup_migrate_schedule_last_run_' . $this->get('id'), $timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the last run time of a schedule to the given timestamp, or now if none specified.
|
||||
*/
|
||||
function get_last_run() {
|
||||
return variable_get('backup_migrate_schedule_last_run_' . $this->get('id'), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove older backups keeping only the number specified by the aministrator.
|
||||
*/
|
||||
function remove_expired_backups() {
|
||||
backup_migrate_include('destinations');
|
||||
|
||||
$num_to_keep = $this->keep;
|
||||
// If num to keep is not 0 (0 is infinity).
|
||||
foreach ((array)$this->get_destinations() as $destination) {
|
||||
if ($destination && $destination->op('delete') && $destination_files = $destination->list_files()) {
|
||||
if ($num_to_keep == BACKUP_MIGRATE_SMART_DELETE) {
|
||||
$this->smart_delete_backups(
|
||||
$destination,
|
||||
$destination_files,
|
||||
variable_get('backup_migrate_smart_keep_subhourly', BACKUP_MIGRATE_SMART_KEEP_SUBHOURLY),
|
||||
variable_get('backup_migrate_smart_keep_hourly', BACKUP_MIGRATE_SMART_KEEP_HOURLY),
|
||||
variable_get('backup_migrate_smart_keep_daily', BACKUP_MIGRATE_SMART_KEEP_DAILY),
|
||||
variable_get('backup_migrate_smart_keep_weekly', BACKUP_MIGRATE_SMART_KEEP_WEEKLY)
|
||||
);
|
||||
}
|
||||
else if ($num_to_keep != BACKUP_MIGRATE_KEEP_ALL) {
|
||||
$this->delete_backups($destination, $destination_files, $num_to_keep);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove older backups keeping only the number specified by the aministrator.
|
||||
*/
|
||||
function delete_backups($destination, $files, $num_to_keep) {
|
||||
backup_migrate_include('destinations');
|
||||
|
||||
$num_to_keep = $this->keep;
|
||||
|
||||
// Sort the files by modified time.
|
||||
$i = 0;
|
||||
foreach ($files as $id => $file) {
|
||||
if ($file->is_recognized_type()) {
|
||||
$time = $file->info('filetime');
|
||||
$sorted[$id] = $time;
|
||||
}
|
||||
}
|
||||
asort($sorted);
|
||||
|
||||
// If we are beyond our limit, remove as many as we need.
|
||||
$num_files = count($files);
|
||||
|
||||
if ($num_files > $num_to_keep) {
|
||||
$num_to_delete = $num_files - $num_to_keep;
|
||||
// Delete from the start of the list (earliest).
|
||||
foreach ($sorted as $id => $time) {
|
||||
if (!$num_to_delete--) {
|
||||
break;
|
||||
}
|
||||
$destination->delete_file($id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete files keeping the specified number of hourly, daily, weekly and monthly backups.
|
||||
*/
|
||||
function smart_delete_backups($destination, $files, $keep_subhourly = 3600, $keep_hourly = 24, $keep_daily = 14, $keep_weekly = PHP_INT_MAX, $keep_monthly = PHP_INT_MAX) {
|
||||
$now = time();
|
||||
$periods = array(
|
||||
'subhourly' => array(
|
||||
'delta' => 1,
|
||||
'keep' => $keep_subhourly,
|
||||
'last_time' => 0,
|
||||
'files' => array(),
|
||||
),
|
||||
'hourly' => array(
|
||||
'delta' => 60*60,
|
||||
'keep' => $keep_hourly,
|
||||
'last_time' => 0,
|
||||
'files' => array(),
|
||||
),
|
||||
'daily' => array(
|
||||
'delta' => 60*60*24,
|
||||
'keep' => $keep_daily,
|
||||
'last_time' => 0,
|
||||
'files' => array(),
|
||||
),
|
||||
'weekly' => array(
|
||||
'delta' => 60*60*24*7,
|
||||
'keep' => $keep_weekly,
|
||||
'last_time' => 0,
|
||||
'files' => array(),
|
||||
),
|
||||
/*
|
||||
'monthly' => array(
|
||||
'delta' => 60*60*24*7*4,
|
||||
'keep' => $keep_monthly,
|
||||
'last_time' => 0,
|
||||
'files' => array(),
|
||||
),
|
||||
*/
|
||||
);
|
||||
|
||||
$keep_files = $filetimes = $times = $groups = $sorted = $saved = array();
|
||||
foreach ($files as $id => $file) {
|
||||
if ($file->is_recognized_type()) {
|
||||
$time = $file->info('filetime');
|
||||
$sorted[$id] = $time;
|
||||
}
|
||||
}
|
||||
arsort($sorted);
|
||||
|
||||
$now = time();
|
||||
foreach ($periods as $i => $period) {
|
||||
foreach ($sorted as $id => $time) {
|
||||
if ($time < ($now - ($period['delta'] * $period['keep']))) {
|
||||
break;
|
||||
}
|
||||
if ($period['last_time'] == 0 || $time <= ($period['last_time'] - $period['delta'])) {
|
||||
$period['last_time'] = $time;
|
||||
$keep_files[$id] = $id;
|
||||
}
|
||||
}
|
||||
// Keep oldest backup or it will get deleted if it doesn't fall on an exact multiple of the period
|
||||
if ($id) {
|
||||
$keep_files[$id] = $id;
|
||||
}
|
||||
}
|
||||
|
||||
// Do the delete.
|
||||
foreach ($files as $id => $file) {
|
||||
if (!isset($keep_files[$id])) {
|
||||
$destination->delete_file($file->file_id());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
287
sites/all/modules/backup_migrate/includes/sources.archivesource.inc
Executable file
287
sites/all/modules/backup_migrate/includes/sources.archivesource.inc
Executable file
|
@ -0,0 +1,287 @@
|
|||
<?php
|
||||
backup_migrate_include('sources.filesource');
|
||||
|
||||
/**
|
||||
* @file
|
||||
* A destination type for saving locally to the server.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A destination type for saving locally to the server.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
|
||||
class backup_migrate_files_destination_archivesource extends backup_migrate_destination_filesource {
|
||||
var $supported_ops = array('source');
|
||||
|
||||
function type_name() {
|
||||
return t("Site Archive Source");
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare the current files directory as a backup source..
|
||||
*/
|
||||
function sources() {
|
||||
$out = array();
|
||||
$out['archive'] = backup_migrate_create_destination('archive', array('machine_name' => 'archive', 'location' => '.', 'name' => t('Entire Site (code, files & DB)'), 'show_in_list' => FALSE));
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of backup filetypes.
|
||||
*/
|
||||
function file_types() {
|
||||
return array(
|
||||
"sitearchive" => array(
|
||||
"extension" => "sitearchive.tar",
|
||||
"filemime" => "application/x-tar",
|
||||
"backup" => TRUE,
|
||||
"restore" => FALSE,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this destination.
|
||||
*/
|
||||
function backup_settings_default() {
|
||||
$out = parent::backup_settings_default();
|
||||
$excludes = explode("\n", $out['exclude_filepaths']);
|
||||
foreach ($excludes as $i => $exclude) {
|
||||
$excludes[$i] = 'public://' . $exclude;
|
||||
}
|
||||
$excludes[] = 'private://backup_migrate';
|
||||
$excludes[] = conf_path() . '/settings.php';
|
||||
$excludes[] = file_directory_temp();
|
||||
|
||||
return array(
|
||||
'exclude_filepaths' => implode("\n", $excludes),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup from this source.
|
||||
*/
|
||||
function _backup_to_file_php($file, $settings) {
|
||||
if ($this->check_libs()) {
|
||||
$base_dir = $this->get_realpath();
|
||||
|
||||
$excluded_paths = empty($settings->filters['exclude_filepaths']) ? '' : $settings->filters['exclude_filepaths'];
|
||||
$exclude = $this->get_excluded_paths($settings);
|
||||
$files = $this->get_files_to_backup($this->get_realpath(), $settings, $exclude);
|
||||
if ($files) {
|
||||
$manifest = $this->generate_manifest();
|
||||
$db = $this->get_db();
|
||||
|
||||
$file->push_type('sitearchive');
|
||||
$gz = new Archive_Tar($file->filepath(), false);
|
||||
|
||||
$gz->addModify(array($manifest), $file->name .'/', dirname($manifest));
|
||||
$gz->addModify($files, $file->name .'/docroot', $base_dir);
|
||||
$gz->addModify($db, $file->name . '/', dirname($db));
|
||||
|
||||
unlink($manifest);
|
||||
rmdir(dirname($manifest));
|
||||
unlink($db);
|
||||
rmdir(dirname($db));
|
||||
|
||||
return $file;
|
||||
}
|
||||
backup_migrate_backup_fail('No files available.', array(), $settings);
|
||||
return FALSE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup from this source.
|
||||
*/
|
||||
function _backup_to_file_cli($file, $settings) {
|
||||
if (!empty($settings->filters['use_cli']) && function_exists('backup_migrate_exec') && function_exists('escapeshellarg')) {
|
||||
$excluded_paths = empty($settings->filters['exclude_filepaths']) ? '' : $settings->filters['exclude_filepaths'];
|
||||
foreach ($this->get_excluded_paths($excluded_paths) as $path) {
|
||||
$exclude[] = '--exclude=' . escapeshellarg($path);
|
||||
}
|
||||
$exclude = implode(' ', $exclude);
|
||||
|
||||
// Create a symlink in a temp directory so we can rename the file in the archive.
|
||||
$temp = backup_migrate_temp_directory();
|
||||
|
||||
$manifest = $this->generate_manifest();
|
||||
$db = $this->get_db();
|
||||
rename($db, $temp . '/database.sql');
|
||||
rename($manifest, $temp . '/MANIFEST.ini');
|
||||
|
||||
$file->push_type('sitearchive');
|
||||
$link = $temp . '/docroot';
|
||||
$input = realpath($this->get_location());
|
||||
backup_migrate_exec("ln -s %input %link; tar --dereference -C %temp -rf %output $exclude .", array('%output' => $file->filepath(), '%input' => $input, '%temp' => $temp, '%link' => $link));
|
||||
|
||||
return $file;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a manifest file.
|
||||
*/
|
||||
function generate_manifest() {
|
||||
$info = array(
|
||||
'Global' => array(
|
||||
'datestamp' => time(),
|
||||
'formatversion' => '2011-07-02',
|
||||
'generator' => 'Backup and Migrate (http://drupal.org/project/backup_migrate)',
|
||||
'generatorversion' => BACKUP_MIGRATE_VERSION,
|
||||
),
|
||||
'Site 0' => array(
|
||||
'version' => VERSION,
|
||||
'name' => variable_get('site_name', ''),
|
||||
'docroot' => 'docroot',
|
||||
'sitedir' => 'docroot/' . conf_path(),
|
||||
'database-file-default' => 'database.sql',
|
||||
'database-file-driver' => 'mysql',
|
||||
),
|
||||
);
|
||||
if ($private = variable_get('file_private_path', FALSE)) {
|
||||
$info['Site 0']['files-private'] = 'docroot/' . $private;
|
||||
}
|
||||
$info['Site 0']['files-public'] = 'docroot/' . variable_get('file_public_path', FALSE);
|
||||
$ini = $this->_array_to_ini($info);
|
||||
|
||||
$tmpdir = backup_migrate_temp_directory();
|
||||
$filepath = $tmpdir . '/MANIFEST.ini';
|
||||
file_put_contents($filepath, $ini);
|
||||
return $filepath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a database dump to add to the archive.
|
||||
*/
|
||||
function get_db() {
|
||||
backup_migrate_include('destinations', 'files', 'filters', 'profiles');
|
||||
|
||||
$file = new backup_file();
|
||||
// Clone the default settings so we can make changes without them leaking out of this function.
|
||||
$settings = clone _backup_migrate_profile_saved_default_profile();
|
||||
$settings->source_id = 'db';
|
||||
$settings->filters['compression'] = 'none';
|
||||
|
||||
// Execute the backup on the db with the default settings.
|
||||
$file = backup_migrate_filters_backup($file, $settings);
|
||||
|
||||
// Generate a tmp file with the correct final title (because ArchiveTar doesn't seem to allow renaming).
|
||||
$tmpdir = backup_migrate_temp_directory();
|
||||
$filepath = $tmpdir . '/database.sql';
|
||||
rename($file->filepath(), $filepath);
|
||||
|
||||
return $filepath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore to this source.
|
||||
*/
|
||||
function _restore_from_file_php($file, &$settings) {
|
||||
$success = false;
|
||||
if ($this->check_libs()) {
|
||||
$from = $file->pop_type();
|
||||
$temp = backup_migrate_temp_directory();
|
||||
|
||||
$tar = new Archive_Tar($from->filepath());
|
||||
$tar->extractModify($temp, $file->name);
|
||||
|
||||
// Parse the manifest
|
||||
$manifest = $this->read_manifest($temp);
|
||||
|
||||
// Currently only the first site in the archive is supported.
|
||||
$site = $manifest['Site 0'];
|
||||
|
||||
$docroot = $temp . '/' . $site['docroot'];
|
||||
$sqlfile = $temp . '/' . $site['database-file-default'];
|
||||
$filepath = NULL;
|
||||
if (isset($site['files-private'])) {
|
||||
$filepath = $temp . '/' . $site['files-private'];
|
||||
}
|
||||
else if (isset($site['files-public'])) {
|
||||
$filepath = $temp . '/' . $site['files-public'];
|
||||
}
|
||||
|
||||
// Move the files from the temp directory.
|
||||
if ($filepath && file_exists($filepath)) {
|
||||
_backup_migrate_move_files($filepath, variable_get('file_public_path', conf_path() . '/files') );
|
||||
}
|
||||
else {
|
||||
_backup_migrate_message('Files were not restored because the archive did not seem to contain a files directory or was in a format that Backup and Migrate couldn\'t read', array(), 'warning');
|
||||
}
|
||||
|
||||
// Restore the sql db.
|
||||
if ($sqlfile && file_exists($sqlfile)) {
|
||||
$db_settings = clone $settings;
|
||||
$db_settings->source_id = 'db';
|
||||
$file = new backup_file(array('filepath' => $sqlfile));
|
||||
$success = backup_migrate_filters_restore($file, $db_settings);
|
||||
}
|
||||
else {
|
||||
_backup_migrate_message('The database was not restored because the archive did not seem to contain a database backup or was in a format that Backup and Migrate couldn\'t read', array(), 'warning');
|
||||
}
|
||||
|
||||
if ($docroot) {
|
||||
_backup_migrate_message('Backup and Migrate cannot restore the php code of the site for security reasons. You will have to copy the code to the server by hand if you wish to restore the full site.', array(), 'warning');
|
||||
}
|
||||
|
||||
return $success && $file;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore to this source.
|
||||
*/
|
||||
function _restore_from_file_cli($file, &$settings) {
|
||||
// @TODO: implement the cli version of the restore.
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a manifest file.
|
||||
*/
|
||||
function read_manifest($directory) {
|
||||
// Assume some defaults if values ore the manifest is missing.
|
||||
$defaults = array(
|
||||
'docroot' => 'docroot',
|
||||
'database-file-default' => 'database.sql',
|
||||
'database-file-driver' => 'mysql',
|
||||
);
|
||||
|
||||
$out = $this->_ini_to_array($directory . '/MANIFEST.ini');
|
||||
|
||||
// Set the defaults.
|
||||
$out['Site 0'] = isset($out['Site 0']) ? $out['Site 0'] : array();
|
||||
$out['Site 0'] += $defaults;
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an associated array to an ini format string. Only allows 2 levels of depth to allow parse_ini_file to parse.
|
||||
*/
|
||||
function _array_to_ini($sections) {
|
||||
$content = "";
|
||||
foreach ($sections as $section => $data) {
|
||||
$content .= '['. $section .']' . "\n";
|
||||
foreach ($data as $key => $val) {
|
||||
$content .= $key . " = \"". $val ."\"\n";
|
||||
}
|
||||
$content .= "\n";
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an associated array to an ini format string. Only allows 2 levels of depth to allow parse_ini_file to parse.
|
||||
*/
|
||||
function _ini_to_array($path) {
|
||||
return parse_ini_file($path, TRUE);
|
||||
}
|
||||
}
|
||||
|
325
sites/all/modules/backup_migrate/includes/sources.db.inc
Normal file
325
sites/all/modules/backup_migrate/includes/sources.db.inc
Normal file
|
@ -0,0 +1,325 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Functions to handle the direct to database destination.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A destination type for saving to a database server.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
class backup_migrate_source_db extends backup_migrate_source_remote {
|
||||
var $supported_ops = array('configure', 'source');
|
||||
var $db_target = 'default';
|
||||
var $connection = null;
|
||||
|
||||
|
||||
function type_name() {
|
||||
return t("Database");
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the info by importing it into the database.
|
||||
*/
|
||||
function save_file($file, $settings) {
|
||||
backup_migrate_include('files');
|
||||
|
||||
// Set the source_id to the destination_id in the settings since for a restore, the source_id is the
|
||||
// database that gets restored to.
|
||||
$settings->set_source($this->get_id());
|
||||
|
||||
// Restore the file to the source database.
|
||||
$file = backup_migrate_perform_restore($this->get_id(), $file, $settings);
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destination configuration callback.
|
||||
*/
|
||||
function edit_form() {
|
||||
$form = parent::edit_form();
|
||||
$form['scheme']['#default_value'] = $this->default_scheme();
|
||||
$form['scheme']['#access'] = FALSE;
|
||||
$form['path']['#title'] = t('Database name');
|
||||
$form['path']['#description'] = t('The name of the database. The database must exist, it will not be created for you.');
|
||||
$form['user']['#description'] = t('Enter the name of a user who has write access to the database.');
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the configuration form. Make sure the db info is valid.
|
||||
*/
|
||||
function edit_form_validate($form, &$form_state) {
|
||||
if (!preg_match('/[a-zA-Z0-9_\$]+/', $form_state['values']['path'])) {
|
||||
form_set_error('path', t('The database name is not valid.'));
|
||||
}
|
||||
parent::edit_form_validate($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default settings for this object.
|
||||
*
|
||||
* @return array
|
||||
* The default tables whose data can be ignored. These tables mostly
|
||||
* contain info which can be easily reproducted (such as cache or search
|
||||
* index) but also tables which can become quite bloated but are not
|
||||
* necessarily extremely important to back up or migrate during development
|
||||
* (such as access log and watchdog).
|
||||
*/
|
||||
function backup_settings_default() {
|
||||
$all_tables = $this->_get_table_names();
|
||||
|
||||
// Basic modules that should be excluded.
|
||||
$basic = array(
|
||||
// Default core tables.
|
||||
'accesslog',
|
||||
'sessions',
|
||||
'watchdog',
|
||||
// Search module.
|
||||
'search_dataset',
|
||||
'search_index',
|
||||
'search_keywords_log',
|
||||
'search_total',
|
||||
// Devel module.
|
||||
'devel_queries',
|
||||
'devel_times',
|
||||
);
|
||||
|
||||
// Identify all cache tables.
|
||||
$cache = array('cache');
|
||||
foreach ($all_tables as $table_name) {
|
||||
if (strpos($table_name, 'cache_') === 0) {
|
||||
$cache[] = $table_name;
|
||||
}
|
||||
}
|
||||
|
||||
// Simpletest can create a lot of tables that do not need to be backed up,
|
||||
// but all of them start with the string 'simpletest' so they can be easily
|
||||
// excluded.
|
||||
$simpletest = array();
|
||||
foreach ($all_tables as $table_name) {
|
||||
if (strpos($table_name, 'simpletest') === 0) {
|
||||
$simpletest[] = $table_name;
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'nodata_tables' => drupal_map_assoc(array_merge($basic, $cache, module_invoke_all('devel_caches'))),
|
||||
'exclude_tables' => $simpletest,
|
||||
'utils_lock_tables' => FALSE,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the backup settings for this destination.
|
||||
*/
|
||||
function backup_settings_form($settings) {
|
||||
$objects = $this->get_object_names();
|
||||
$form['#description'] = t("You may omit specific tables, or specific table data from the backup file. Only omit data that you know you will not need such as cache data, or tables from other applications. Excluding tables can break your Drupal install, so <strong>do not change these settings unless you know what you're doing</strong>.");
|
||||
$form['exclude_tables'] = array(
|
||||
"#type" => "select",
|
||||
"#multiple" => TRUE,
|
||||
"#title" => t("Exclude the following tables altogether"),
|
||||
"#options" => $objects,
|
||||
"#default_value" => $settings['exclude_tables'],
|
||||
"#description" => t("The selected tables will not be added to the backup file."),
|
||||
);
|
||||
$form['nodata_tables'] = array(
|
||||
"#type" => "select",
|
||||
"#multiple" => TRUE,
|
||||
"#title" => t("Exclude the data from the following tables"),
|
||||
"#options" => $objects,
|
||||
"#default_value" => $settings['nodata_tables'],
|
||||
"#description" => t("The selected tables will have their structure backed up but not their contents. This is useful for excluding cache data to reduce file size."),
|
||||
);
|
||||
$form['utils_lock_tables'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Lock tables during backup'),
|
||||
'#default_value' => !empty($settings['utils_lock_tables']) ? $settings['utils_lock_tables'] : NULL,
|
||||
'#description' => t('This can help reduce data corruption, but will make your site unresponsive.'),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup from this source.
|
||||
*/
|
||||
function backup_to_file($file, $settings) {
|
||||
$file->push_type($this->get_file_type_id());
|
||||
|
||||
//$this->lock_tables($settings);
|
||||
|
||||
// Switch to a different db if specified.
|
||||
$success = $this->_backup_db_to_file($file, $settings);
|
||||
|
||||
//$this->unlock_tables($settings);
|
||||
|
||||
return $success ? $file : FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore to this source.
|
||||
*/
|
||||
function restore_from_file($file, &$settings) {
|
||||
$num = 0;
|
||||
$type = $this->get_file_type_id();
|
||||
// Open the file using the file wrapper. Check that the dump is of the right type (allow .sql for legacy reasons).
|
||||
if ($file->type_id() !== $this->get_file_type_id() && $file->type_id() !== 'sql') {
|
||||
_backup_migrate_message("Unable to restore from file %file because a %type file can't be restored to this database.", array("%file" => $file->filepath(), '%type' => $file->type_id()), 'error');
|
||||
}
|
||||
else {
|
||||
backup_migrate_filters_invoke_all('pre_restore', $file, $settings);
|
||||
|
||||
// Restore the database.
|
||||
$num = $this->_restore_db_from_file($file, $settings);
|
||||
$settings->performed_action = $num ? t('%num SQL commands executed.', array('%num' => $num)) : '';
|
||||
|
||||
backup_migrate_filters_invoke_all('post_restore', $file, $settings, $num);
|
||||
}
|
||||
return $num;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the db connection for the specified db.
|
||||
*/
|
||||
function _get_db_connection() {
|
||||
if (!$this->connection) {
|
||||
$target = $key = '';
|
||||
$parts = explode(':', $this->get_id());
|
||||
// One of the predefined databases (set in settings.php)
|
||||
if ($parts[0] == 'db') {
|
||||
$key = empty($parts[1]) ? 'default' : $parts[1];
|
||||
$target = empty($parts[2]) ? 'default' : $parts[2];
|
||||
}
|
||||
// Another db url.
|
||||
else {
|
||||
// If the url is specified build it into a connection info array.
|
||||
if (!empty($this->dest_url)) {
|
||||
$info = array(
|
||||
'driver' => empty($this->dest_url['scheme']) ? NULL : $this->dest_url['scheme'],
|
||||
'host' => empty($this->dest_url['host']) ? NULL : $this->dest_url['host'],
|
||||
'port' => empty($this->dest_url['port']) ? NULL : $this->dest_url['port'],
|
||||
'username' => empty($this->dest_url['user']) ? NULL : $this->dest_url['user'],
|
||||
'password' => empty($this->dest_url['pass']) ? NULL : $this->dest_url['pass'],
|
||||
'database' => empty($this->dest_url['path']) ? NULL : $this->dest_url['path'],
|
||||
);
|
||||
$key = uniqid('backup_migrate_tmp_');
|
||||
$target = 'default';
|
||||
Database::addConnectionInfo($key, $target, $info);
|
||||
}
|
||||
// No database selected. Assume the default.
|
||||
else {
|
||||
$key = $target = 'default';
|
||||
}
|
||||
}
|
||||
if ($target && $key) {
|
||||
$this->connection = Database::getConnection($target, $key);
|
||||
}
|
||||
}
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup the databases to a file.
|
||||
*/
|
||||
function _backup_db_to_file($file, $settings) {
|
||||
// Must be overridden.
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup the databases to a file.
|
||||
*/
|
||||
function _restore_db_from_file($file, $settings) {
|
||||
// Must be overridden.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of objects in the database.
|
||||
*/
|
||||
function get_object_names() {
|
||||
// Must be overridden.
|
||||
$out = $this->_get_table_names();
|
||||
if (method_exists($this, '_get_view_names')) {
|
||||
$out += $this->_get_view_names();
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of tables in the database.
|
||||
*/
|
||||
function get_table_names() {
|
||||
// Must be overridden.
|
||||
$out = $this->_get_table_names();
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of tables in the database.
|
||||
*/
|
||||
function _get_table_names() {
|
||||
// Must be overridden.
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock the database in anticipation of a backup.
|
||||
*/
|
||||
function lock_tables($settings) {
|
||||
if ($settings->filters['utils_lock_tables']) {
|
||||
$tables = array();
|
||||
foreach ($this->get_table_names() as $table) {
|
||||
// There's no need to lock excluded or structure only tables because it doesn't matter if they change.
|
||||
if (empty($settings->filters['exclude_tables']) || !in_array($table, (array)$settings->filters['exclude_tables'])) {
|
||||
$tables[] = $table;
|
||||
}
|
||||
}
|
||||
$this->_lock_tables($tables);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock the list of given tables in the database.
|
||||
*/
|
||||
function _lock_tables($tables) {
|
||||
// Must be overridden.
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock any tables that have been locked.
|
||||
*/
|
||||
function unlock_tables($settings) {
|
||||
if ($settings->filters['utils_lock_tables']) {
|
||||
$this->_unlock_tables();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock the list of given tables in the database.
|
||||
*/
|
||||
function _unlock_tables($tables) {
|
||||
// Must be overridden.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file type for to backup this destination to.
|
||||
*/
|
||||
function get_file_type_id() {
|
||||
return 'sql';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version info for the given DB.
|
||||
*/
|
||||
function _db_info() {
|
||||
return array(
|
||||
'type' => FALSE,
|
||||
'version' => t('Unknown'),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
497
sites/all/modules/backup_migrate/includes/sources.db.mysql.inc
Normal file
497
sites/all/modules/backup_migrate/includes/sources.db.mysql.inc
Normal file
|
@ -0,0 +1,497 @@
|
|||
<?php
|
||||
|
||||
backup_migrate_include('sources.db');
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Functions to handle the direct to database source.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A source type for backing up from database server.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
|
||||
class backup_migrate_source_db_mysql extends backup_migrate_source_db {
|
||||
function type_name() {
|
||||
return t("MySQL Database");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of backup filetypes.
|
||||
*/
|
||||
function file_types() {
|
||||
return array(
|
||||
"sql" => array(
|
||||
"extension" => "sql",
|
||||
"filemime" => "text/x-sql",
|
||||
"backup" => TRUE,
|
||||
"restore" => TRUE,
|
||||
),
|
||||
"mysql" => array(
|
||||
"extension" => "mysql",
|
||||
"filemime" => "text/x-sql",
|
||||
"backup" => TRUE,
|
||||
"restore" => TRUE,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the scheme for this db type.
|
||||
*/
|
||||
function default_scheme() {
|
||||
return 'mysql';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Declare any mysql databases defined in the settings.php file as a possible source.
|
||||
*/
|
||||
function sources() {
|
||||
$out = array();
|
||||
global $databases;
|
||||
foreach ((array)$databases as $db_key => $target) {
|
||||
foreach ((array)$target as $tgt_key => $info) {
|
||||
// Only mysql/mysqli supported by this source.
|
||||
$key = $db_key . ':' . $tgt_key;
|
||||
if ($info['driver'] === 'mysql') {
|
||||
$url = $info['driver'] . '://' . $info['username'] . ':' . $info['password'] . '@' . $info['host'] . (isset($info['port']) ? ':' . $info['port'] : '') . '/' . $info['database'];
|
||||
if ($source = backup_migrate_create_destination('mysql', array('url' => $url))) {
|
||||
// Treat the default database differently because it is probably the only one available.
|
||||
if ($key == 'default:default') {
|
||||
$source->set_id('db');
|
||||
$source->set_name(t('Default Database'));
|
||||
// Dissalow backing up to the default database because that's confusing and potentially dangerous.
|
||||
$source->remove_op('scheduled backup');
|
||||
$source->remove_op('manual backup');
|
||||
}
|
||||
else {
|
||||
$source->set_id('db:'. $key);
|
||||
$source->set_name($key .": ". $source->get_display_location());
|
||||
}
|
||||
$out[$source->get_id()] = $source;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file type for to backup this source to.
|
||||
*/
|
||||
function get_file_type_id() {
|
||||
return 'mysql';
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup the databases to a file.
|
||||
*
|
||||
* Returns a list of sql commands, one command per line.
|
||||
* That makes it easier to import without loading the whole file into memory.
|
||||
* The files are a little harder to read, but human-readability is not a priority
|
||||
*/
|
||||
function _backup_db_to_file($file, $settings) {
|
||||
if (!empty($settings->filters['use_cli']) && $this->_backup_db_to_file_mysqldump($file, $settings)) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
$lines = 0;
|
||||
$exclude = !empty($settings->filters['exclude_tables']) ? $settings->filters['exclude_tables'] : array();
|
||||
$nodata = !empty($settings->filters['nodata_tables']) ? $settings->filters['nodata_tables'] : array();
|
||||
if ($file->open(TRUE)) {
|
||||
$file->write($this->_get_sql_file_header());
|
||||
$alltables = $this->_get_tables();
|
||||
$allviews = $this->_get_views();
|
||||
|
||||
foreach ($alltables as $table) {
|
||||
if (_backup_migrate_check_timeout()) {
|
||||
return FALSE;
|
||||
}
|
||||
if ($table['name'] && !isset($exclude[$table['name']])) {
|
||||
$file->write($this->_get_table_structure_sql($table));
|
||||
$lines++;
|
||||
if (!in_array($table['name'], $nodata)) {
|
||||
$lines += $this->_dump_table_data_sql_to_file($file, $table);
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($allviews as $view) {
|
||||
if (_backup_migrate_check_timeout()) {
|
||||
return FALSE;
|
||||
}
|
||||
if ($view['name'] && !isset($exclude[$view['name']])) {
|
||||
$file->write($this->_get_view_create_sql($view));
|
||||
}
|
||||
}
|
||||
$file->write($this->_get_sql_file_footer());
|
||||
$file->close();
|
||||
return $lines;
|
||||
}
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Backup the databases to a file using the mysqldump command.
|
||||
*/
|
||||
function _backup_db_to_file_mysqldump($file, $settings) {
|
||||
$success = FALSE;
|
||||
$nodata_tables = array();
|
||||
$alltables = $this->_get_tables();
|
||||
|
||||
|
||||
$command = 'mysqldump --result-file=%file --opt -Q --host=%host --port=%port --user=%user --password=%pass %db';
|
||||
$args = array(
|
||||
'%file' => $file->filepath(),
|
||||
'%host' => $this->dest_url['host'],
|
||||
'%port' => isset($this->dest_url['port']) ? $this->dest_url['port'] : '3306',
|
||||
'%user' => $this->dest_url['user'],
|
||||
'%pass' => $this->dest_url['pass'],
|
||||
'%db' => $this->dest_url['path'],
|
||||
);
|
||||
|
||||
// Ignore the excluded and no-data tables.
|
||||
if (!empty($settings->filters['exclude_tables'])) {
|
||||
$db = $this->dest_url['path'];
|
||||
foreach ((array)$settings->filters['exclude_tables'] as $table) {
|
||||
if (isset($alltables[$table])) {
|
||||
$command .= ' --ignore-table='. $db .'.'. $table;
|
||||
}
|
||||
}
|
||||
foreach ((array)$settings->filters['nodata_tables'] as $table) {
|
||||
if (isset($alltables[$table])) {
|
||||
$nodata_tables[] = $table;
|
||||
$command .= ' --ignore-table='. $db .'.'. $table;
|
||||
}
|
||||
}
|
||||
}
|
||||
$success = backup_migrate_exec($command, $args);
|
||||
|
||||
// Get the nodata tables.
|
||||
if ($success && !empty($nodata_tables)) {
|
||||
$tables = implode(' ', array_unique($nodata_tables));
|
||||
$command = "mysqldump --no-data --opt -Q --host=%host --port=%port --user=%user --password=%pass %db $tables >> %file";
|
||||
$success = backup_migrate_exec($command, $args);
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup the databases to a file.
|
||||
*/
|
||||
function _restore_db_from_file($file, $settings) {
|
||||
$num = 0;
|
||||
|
||||
if ($file->open() && $conn = $this->_get_db_connection()) {
|
||||
// Read one line at a time and run the query.
|
||||
while ($line = $this->_read_sql_command_from_file($file)) {
|
||||
if (_backup_migrate_check_timeout()) {
|
||||
return FALSE;
|
||||
}
|
||||
if ($line) {
|
||||
// Prepeare and exexute the statement instead of the api function to avoid substitution of '{' etc.
|
||||
$stmt = $conn->prepare($line);
|
||||
$stmt->execute();
|
||||
$num++;
|
||||
}
|
||||
}
|
||||
// Close the file with fclose/gzclose.
|
||||
$file->close();
|
||||
}
|
||||
else {
|
||||
drupal_set_message(t("Unable to open file %file to restore database", array("%file" => $file->filepath())), 'error');
|
||||
$num = FALSE;
|
||||
}
|
||||
return $num;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read a multiline sql command from a file.
|
||||
*
|
||||
* Supports the formatting created by mysqldump, but won't handle multiline comments.
|
||||
*/
|
||||
function _read_sql_command_from_file($file) {
|
||||
$out = '';
|
||||
while ($line = $file->read()) {
|
||||
$first2 = substr($line, 0, 2);
|
||||
$first3 = substr($line, 0, 3);
|
||||
|
||||
// Ignore single line comments. This function doesn't support multiline comments or inline comments.
|
||||
if ($first2 != '--' && ($first2 != '/*' || $first3 == '/*!')) {
|
||||
$out .= ' ' . trim($line);
|
||||
// If a line ends in ; or */ it is a sql command.
|
||||
if (substr($out, strlen($out) - 1, 1) == ';') {
|
||||
return trim($out);
|
||||
}
|
||||
}
|
||||
}
|
||||
return trim($out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of tables in the database.
|
||||
*/
|
||||
function _get_table_names() {
|
||||
$out = array();
|
||||
foreach ($this->_get_tables() as $table) {
|
||||
$out[$table['name']] = $table['name'];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of views in the database.
|
||||
*/
|
||||
function _get_view_names() {
|
||||
$out = array();
|
||||
foreach ($this->_get_views() as $view) {
|
||||
$out[$view['name']] = $view['name'];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lock the list of given tables in the database.
|
||||
*/
|
||||
function _lock_tables($tables) {
|
||||
if ($tables) {
|
||||
$tables_escaped = array();
|
||||
foreach ($tables as $table) {
|
||||
$tables_escaped[] = '`'. db_escape_table($table) .'` WRITE';
|
||||
}
|
||||
$this->query('LOCK TABLES '. implode(', ', $tables_escaped));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlock all tables in the database.
|
||||
*/
|
||||
function _unlock_tables($settings) {
|
||||
$this->query('UNLOCK TABLES');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of tables in the db.
|
||||
*/
|
||||
function _get_tables() {
|
||||
$out = array();
|
||||
// get auto_increment values and names of all tables
|
||||
$tables = $this->query("show table status", array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
foreach ($tables as $table) {
|
||||
// Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed.
|
||||
// See: http://drupal.org/node/1171866
|
||||
$table = array_change_key_case($table);
|
||||
if (!empty($table['engine'])) {
|
||||
$out[$table['name']] = $table;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of views in the db.
|
||||
*/
|
||||
function _get_views() {
|
||||
$out = array();
|
||||
// get auto_increment values and names of all tables
|
||||
$tables = $this->query("show table status", array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
foreach ($tables as $table) {
|
||||
// Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed.
|
||||
// See: http://drupal.org/node/1171866
|
||||
$table = array_change_key_case($table);
|
||||
if (empty($table['engine'])) {
|
||||
$out[$table['name']] = $table;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sql for the structure of the given view.
|
||||
*/
|
||||
function _get_view_create_sql($view) {
|
||||
$out = "";
|
||||
// Switch SQL mode to get rid of "CREATE ALGORITHM..." what requires more permissions + troubles with the DEFINER user
|
||||
$sql_mode = $this->query("SELECT @@SESSION.sql_mode")->fetchField();
|
||||
$this->query("SET sql_mode = 'ANSI'");
|
||||
$result = $this->query("SHOW CREATE VIEW `" . $view['name'] . "`", array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
$this->query("SET SQL_mode = :mode", array(':mode' => $sql_mode));
|
||||
foreach ($result as $create) {
|
||||
// Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed.
|
||||
// See: http://drupal.org/node/1171866
|
||||
$create = array_change_key_case($create);
|
||||
$out .= "DROP VIEW IF EXISTS `". $view['name'] ."`;\n";
|
||||
$out .= "SET sql_mode = 'ANSI';\n";
|
||||
$out .= strtr($create['create view'], "\n", " ") . ";\n";
|
||||
$out .= "SET sql_mode = '$sql_mode';\n";
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the sql for the structure of the given table.
|
||||
*/
|
||||
function _get_table_structure_sql($table) {
|
||||
$out = "";
|
||||
$result = $this->query("SHOW CREATE TABLE `". $table['name'] ."`", array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
foreach ($result as $create) {
|
||||
// Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed.
|
||||
// See: http://drupal.org/node/1171866
|
||||
$create = array_change_key_case($create);
|
||||
$out .= "DROP TABLE IF EXISTS `". $table['name'] ."`;\n";
|
||||
// Remove newlines and convert " to ` because PDO seems to convert those for some reason.
|
||||
$out .= strtr($create['create table'], array("\n" => ' ', '"' => '`'));
|
||||
if ($table['auto_increment']) {
|
||||
$out .= " AUTO_INCREMENT=". $table['auto_increment'];
|
||||
}
|
||||
$out .= ";\n";
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sql to insert the data for a given table
|
||||
*/
|
||||
function _dump_table_data_sql_to_file($file, $table) {
|
||||
$rows_per_line = variable_get('backup_migrate_data_rows_per_line', 30);
|
||||
$bytes_per_line = variable_get('backup_migrate_data_bytes_per_line', 2000);
|
||||
|
||||
$lines = 0;
|
||||
$data = $this->query("SELECT * FROM `". $table['name'] ."`", array(), array('fetch' => PDO::FETCH_ASSOC));
|
||||
$rows = $bytes = 0;
|
||||
|
||||
// Escape backslashes, PHP code, special chars
|
||||
$search = array('\\', "'", "\x00", "\x0a", "\x0d", "\x1a");
|
||||
$replace = array('\\\\', "''", '\0', '\n', '\r', '\Z');
|
||||
|
||||
$line = array();
|
||||
foreach ($data as $row) {
|
||||
// DB Escape the values.
|
||||
$items = array();
|
||||
foreach ($row as $key => $value) {
|
||||
$items[] = is_null($value) ? "null" : "'". str_replace($search, $replace, $value) ."'";
|
||||
}
|
||||
|
||||
// If there is a row to be added.
|
||||
if ($items) {
|
||||
// Start a new line if we need to.
|
||||
if ($rows == 0) {
|
||||
$file->write("INSERT INTO `". $table['name'] ."` VALUES ");
|
||||
$bytes = $rows = 0;
|
||||
}
|
||||
// Otherwise add a comma to end the previous entry.
|
||||
else {
|
||||
$file->write(",");
|
||||
}
|
||||
|
||||
// Write the data itself.
|
||||
$sql = implode(',', $items);
|
||||
$file->write('('. $sql .')');
|
||||
$bytes += strlen($sql);
|
||||
$rows++;
|
||||
|
||||
// Finish the last line if we've added enough items
|
||||
if ($rows >= $rows_per_line || $bytes >= $bytes_per_line) {
|
||||
$file->write(";\n");
|
||||
$lines++;
|
||||
$bytes = $rows = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Finish any unfinished insert statements.
|
||||
if ($rows > 0) {
|
||||
$file->write(";\n");
|
||||
$lines++;
|
||||
}
|
||||
|
||||
return $lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the db connection for the specified db.
|
||||
*/
|
||||
function _get_db_connection() {
|
||||
if (!$this->connection) {
|
||||
$this->connection = parent::_get_db_connection();
|
||||
// Set the sql mode because the default is ANSI,TRADITIONAL which is not aware of collation or storage engine.
|
||||
$this->connection->exec("SET sql_mode=''");
|
||||
}
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a db query on this destination's db.
|
||||
*/
|
||||
function query($query, $args = array(), $options = array()) {
|
||||
if ($conn = $this->_get_db_connection()) {
|
||||
return $conn->query($query, $args, $options);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The header for the top of the sql dump file. These commands set the connection
|
||||
* character encoding to help prevent encoding conversion issues.
|
||||
*/
|
||||
function _get_sql_file_header() {
|
||||
$info = $this->_db_info();
|
||||
|
||||
return "-- Backup and Migrate (Drupal) MySQL Dump
|
||||
-- Backup and Migrate Version: ". BACKUP_MIGRATE_VERSION ."
|
||||
-- http://drupal.org/project/backup_migrate
|
||||
-- Drupal Version: " . VERSION . "
|
||||
-- http://drupal.org/
|
||||
--
|
||||
-- Host: " . url('', array('absolute' => TRUE)) . "
|
||||
-- Site Name: " . url('', array('absolute' => TRUE)) . "
|
||||
-- Generation Time: " . format_date(time(), 'custom', 'r') . "
|
||||
-- MySQL Version: " . $info['version'] . "
|
||||
|
||||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
|
||||
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
|
||||
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
|
||||
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=NO_AUTO_VALUE_ON_ZERO */;
|
||||
|
||||
SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";
|
||||
SET NAMES utf8;
|
||||
|
||||
";
|
||||
}
|
||||
|
||||
/**
|
||||
* The footer of the sql dump file.
|
||||
*/
|
||||
function _get_sql_file_footer() {
|
||||
return "
|
||||
|
||||
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
|
||||
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
|
||||
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
|
||||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||
";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version info for the given DB.
|
||||
*/
|
||||
function _db_info() {
|
||||
$db = $this->_get_db_connection();
|
||||
return array(
|
||||
'type' => 'mysql',
|
||||
'version' => $db ? $db->version() : t('Unknown'),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
314
sites/all/modules/backup_migrate/includes/sources.filesource.inc
Normal file
314
sites/all/modules/backup_migrate/includes/sources.filesource.inc
Normal file
|
@ -0,0 +1,314 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* A destination type for saving locally to the server.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A destination type for saving locally to the server.
|
||||
*
|
||||
* @ingroup backup_migrate_destinations
|
||||
*/
|
||||
|
||||
class backup_migrate_destination_filesource extends backup_migrate_source {
|
||||
var $supported_ops = array('restore', 'configure', 'delete', 'source');
|
||||
|
||||
function type_name() {
|
||||
return t("Files Directory");
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare the current files directory as a backup source..
|
||||
*/
|
||||
function sources() {
|
||||
$out = array();
|
||||
$out['files'] = backup_migrate_create_destination('filesource', array('machine_name' => 'files', 'location' => 'public://', 'name' => t('Public Files Directory'), 'show_in_list' => FALSE));
|
||||
if (variable_get('file_private_path', FALSE)) {
|
||||
$out['files_private'] = backup_migrate_create_destination('filesource', array('machine_name' => 'files', 'location' => 'private://', 'name' => t('Private Files Directory'), 'show_in_list' => FALSE));
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for the files destination.
|
||||
*/
|
||||
function edit_form() {
|
||||
$form = parent::edit_form();
|
||||
$form['location'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Directory path"),
|
||||
"#default_value" => $this->get_location(),
|
||||
"#required" => TRUE,
|
||||
"#description" => t('Enter the path to the directory to back up. Use a relative path to pick a path relative to your Drupal root directory. The web server must be able to read from this path.'),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of backup filetypes.
|
||||
*/
|
||||
function file_types() {
|
||||
return array(
|
||||
"tar" => array(
|
||||
"extension" => "tar",
|
||||
"filemime" => "application/x-tar",
|
||||
"backup" => TRUE,
|
||||
"restore" => TRUE,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the settings for this destination.
|
||||
*
|
||||
* Return the default directories whose data can be ignored. These directories contain
|
||||
* info which can be easily reproducted. Also exclude the backup and migrate folder
|
||||
* to prevent exponential bloat.
|
||||
*/
|
||||
function backup_settings_default() {
|
||||
return array(
|
||||
'exclude_filepaths' => "backup_migrate\nstyles\ncss\njs\nctools\nless",
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the form for the backup settings for this destination.
|
||||
*/
|
||||
function backup_settings_form($settings) {
|
||||
$form['exclude_filepaths'] = array(
|
||||
"#type" => "textarea",
|
||||
"#multiple" => TRUE,
|
||||
"#title" => t("Exclude the following files or directories"),
|
||||
"#default_value" => $settings['exclude_filepaths'],
|
||||
"#description" => t("A list of files or directories to be excluded from backups. Add one path per line relative to the directory being backed up."),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup from this source.
|
||||
*/
|
||||
function backup_to_file($file, $settings) {
|
||||
if ($out = $this->_backup_to_file_cli($file, $settings)) {
|
||||
return $out;
|
||||
}
|
||||
else {
|
||||
return $this->_backup_to_file_php($file, $settings);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup from this source.
|
||||
*/
|
||||
function _backup_to_file_php($file, $settings) {
|
||||
if ($this->check_libs()) {
|
||||
$excluded = $this->get_excluded_paths($settings);
|
||||
$files = $this->get_files_to_backup($this->get_realpath(), $settings, $excluded);
|
||||
if ($files) {
|
||||
$file->push_type('tar');
|
||||
$gz = new Archive_Tar($file->filepath(), false);
|
||||
$gz->addModify($files, '', $this->get_realpath());
|
||||
return $file;
|
||||
}
|
||||
backup_migrate_backup_fail('No files available.', array(), $settings);
|
||||
return FALSE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup from this source.
|
||||
*/
|
||||
function _backup_to_file_cli($file, $settings) {
|
||||
if (!empty($settings->filters['use_cli']) && function_exists('backup_migrate_exec') && function_exists('escapeshellarg')) {
|
||||
$excluded = $this->get_excluded_paths($settings);
|
||||
$exclude = array();
|
||||
foreach ($excluded as $path) {
|
||||
$exclude[] = '--exclude=' . escapeshellarg($path);
|
||||
}
|
||||
$exclude = implode(' ', $exclude);
|
||||
|
||||
// Create a symlink in a temp directory so we can rename the file in the archive.
|
||||
$temp = backup_migrate_temp_directory();
|
||||
|
||||
$file->push_type('tar');
|
||||
backup_migrate_exec("tar --dereference -C %input -rf %output $exclude .", array('%output' => $file->filepath(), '%input' => $this->get_realpath(), '%temp' => $temp));
|
||||
return $file;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore to this source.
|
||||
*/
|
||||
function restore_from_file($file, &$settings) {
|
||||
if ($out = $this->_restore_from_file_cli($file, $settings)) {
|
||||
return $out;
|
||||
}
|
||||
else {
|
||||
return $this->_restore_from_file_php($file, $settings);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore to this source.
|
||||
*/
|
||||
function _restore_from_file_php($file, &$settings) {
|
||||
if ($this->check_libs()) {
|
||||
$from = $file->pop_type();
|
||||
$temp = backup_migrate_temp_directory();
|
||||
|
||||
$tar = new Archive_Tar($from->filepath());
|
||||
$tar->extractModify($temp, $file->name);
|
||||
|
||||
// Older B&M Files format included a base 'files' directory.
|
||||
if (file_exists($temp .'/files')) {
|
||||
$temp = $temp . '/files';
|
||||
}
|
||||
if (file_exists($temp .'/'. $file->name .'/files')) {
|
||||
$temp = $temp . '/files';
|
||||
}
|
||||
|
||||
// Move the files from the temp directory.
|
||||
_backup_migrate_move_files($temp, $this->get_realpath());
|
||||
|
||||
return $file;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore to this source.
|
||||
*/
|
||||
function _restore_from_file_cli($file, &$settings) {
|
||||
if (!empty($settings->filters['use_cli']) && function_exists('backup_migrate_exec')) {
|
||||
$temp = backup_migrate_temp_directory();
|
||||
backup_migrate_exec("tar -C %temp -xf %input", array('%input' => $file->filepath(), '%temp' => $temp));
|
||||
|
||||
// Older B&M Files format included a base 'files' directory.
|
||||
if (file_exists($temp .'/files')) {
|
||||
$temp = $temp . '/files';
|
||||
}
|
||||
if (file_exists($temp .'/'. $file->name .'/files')) {
|
||||
$temp = $temp . '/files';
|
||||
}
|
||||
|
||||
// Move the files from the temp directory.
|
||||
backup_migrate_exec("mv -rf %temp/* %output", array('%output' => $this->get_realpath(), '%temp' => $temp));
|
||||
return $file;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of files to backup from the given set if dirs. Exclude any that match the array $exclude.
|
||||
*/
|
||||
function get_files_to_backup($dir, $settings, $exclude = array()) {
|
||||
$out = $errors = array();
|
||||
|
||||
if (!file_exists($dir)) {
|
||||
backup_migrate_backup_fail('Directory %dir does not exist.', array('%dir' => $dir), $settings);
|
||||
return FALSE;
|
||||
}
|
||||
if ($handle = @opendir($dir)) {
|
||||
while (($file = readdir($handle)) !== FALSE) {
|
||||
if ($file != '.' && $file != '..' && !in_array($file, $exclude)) {
|
||||
$real = realpath($dir . '/' . $file);
|
||||
// If the path is not excluded.
|
||||
if (!in_array($real, $exclude)) {
|
||||
if (is_dir($real)) {
|
||||
$subdir = $this->get_files_to_backup($real, $settings, $exclude);
|
||||
// If there was an error reading the subdirectory then abort the backup.
|
||||
if ($subdir === FALSE) {
|
||||
closedir($handle);
|
||||
return FALSE;
|
||||
}
|
||||
// If the directory is empty, add an empty directory.
|
||||
if (count($subdir) == 0) {
|
||||
$out[] = $real;
|
||||
}
|
||||
$out = array_merge($out, $subdir);
|
||||
}
|
||||
else {
|
||||
if (is_readable($real)) {
|
||||
$out[] = $real;
|
||||
}
|
||||
else {
|
||||
$errors[] = $dir . '/' . $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir($handle);
|
||||
}
|
||||
else {
|
||||
backup_migrate_backup_fail('Could not open directory %dir', array('%dir' => $dir), $settings);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Alert the user to any errors there might have been.
|
||||
if ($errors) {
|
||||
if (count($errors < 5)) {
|
||||
$filesmsg = t('The following files: !files', array('!files' => theme('item_list', array('items' => $errors))));
|
||||
}
|
||||
else {
|
||||
$filesmsg = t('!count files', array('!count' => count($errors)));
|
||||
}
|
||||
|
||||
if (empty($settings->filters['ignore_errors'])) {
|
||||
backup_migrate_backup_fail('The backup could not be completed because !files could not be read. If you want to skip unreadable files use the \'Ignore Errors\' setting under \'Advanced Options\' in \'Advanced Backup\' or in your schedule settings profile.', array('!files' => $filesmsg), 'error');
|
||||
$out = FALSE;
|
||||
}
|
||||
else {
|
||||
backup_migrate_backup_fail('!files could not be read and were skipped', array('!files' => $filesmsg), 'error');
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Break the excpluded paths string into a usable list of paths.
|
||||
*/
|
||||
function get_excluded_paths($settings) {
|
||||
$base_dir = $this->get_realpath() . '/';
|
||||
$paths = empty($settings->filters['exclude_filepaths']) ? '' : $settings->filters['exclude_filepaths'];
|
||||
$out = explode("\n", $paths);
|
||||
foreach ($out as $key => $val) {
|
||||
$path = trim($val, "/ \t\r\n");
|
||||
// If the path specified is a stream url or absolute path add the normalized version.
|
||||
if ($real = drupal_realpath($path)) {
|
||||
$out[$key] = $real;
|
||||
}
|
||||
// If the path is a relative path add it.
|
||||
else if ($real = drupal_realpath($base_dir . $path)) {
|
||||
$out[$key] = $real;
|
||||
}
|
||||
// Otherwise add it as is even though it probably won't match any files.
|
||||
else {
|
||||
$out[$key] = $path;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the required libraries are installed.
|
||||
*/
|
||||
function check_libs() {
|
||||
$result = true;
|
||||
// Drupal 7 has Archive_Tar built in so there should be no need to include anything here.
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file location.
|
||||
*/
|
||||
function get_realpath() {
|
||||
return drupal_realpath($this->get_location());
|
||||
}
|
||||
}
|
||||
|
268
sites/all/modules/backup_migrate/includes/sources.inc
Normal file
268
sites/all/modules/backup_migrate/includes/sources.inc
Normal file
|
@ -0,0 +1,268 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* @file
|
||||
* All of the source handling code needed for Backup and Migrate.
|
||||
*/
|
||||
|
||||
backup_migrate_include('crud', 'locations', 'destinations');
|
||||
|
||||
/**
|
||||
* Get all the available backup sources.
|
||||
*/
|
||||
function backup_migrate_get_sources() {
|
||||
return backup_migrate_crud_get_items('source');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the available source types.
|
||||
*/
|
||||
function backup_migrate_get_source_subtypes() {
|
||||
return backup_migrate_crud_subtypes('source');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the destination of the given id.
|
||||
*/
|
||||
function backup_migrate_get_source($id) {
|
||||
$sources = backup_migrate_get_sources();
|
||||
return empty($sources[$id]) ? NULL : $sources[$id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a source object of the given type with the given params.
|
||||
*/
|
||||
function backup_migrate_create_source($subtype, $params = array()) {
|
||||
$params['subtype'] = $subtype;
|
||||
return backup_migrate_crud_create_item('source', $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_backup_migrate_source_subtypes().
|
||||
*
|
||||
* Get the built in Backup and Migrate source types.
|
||||
*/
|
||||
function backup_migrate_backup_migrate_source_subtypes() {
|
||||
$out = array();
|
||||
$out += array(
|
||||
'db' => array(
|
||||
'type_name' => t('Database'),
|
||||
'description' => t('Import the backup directly into another database. Database sources can also be used as a source to backup from.'),
|
||||
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/sources.db.inc',
|
||||
'class' => 'backup_migrate_source_db',
|
||||
'can_create' => FALSE,
|
||||
),
|
||||
'mysql' => array(
|
||||
'type_name' => t('MySQL Database'),
|
||||
'description' => t('Import the backup directly into another MySQL database. Database sources can also be used as a source to backup from.'),
|
||||
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/sources.db.mysql.inc',
|
||||
'class' => 'backup_migrate_source_db_mysql',
|
||||
'can_create' => TRUE,
|
||||
),
|
||||
'filesource' => array(
|
||||
'description' => t('A files directory which can be backed up from.'),
|
||||
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/sources.filesource.inc',
|
||||
'class' => 'backup_migrate_destination_filesource',
|
||||
'type_name' => t('File Directory'),
|
||||
'can_create' => TRUE,
|
||||
),
|
||||
'archive' => array(
|
||||
'description' => t('Create an archive of your entire site.'),
|
||||
'file' => drupal_get_path('module', 'backup_migrate') .'/includes/sources.archivesource.inc',
|
||||
'class' => 'backup_migrate_files_destination_archivesource',
|
||||
'type_name' => t('Site Archive'),
|
||||
'can_create' => FALSE,
|
||||
),
|
||||
);
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_backup_migrate_sources().
|
||||
*
|
||||
* Get the built in backup sources and those in the db.
|
||||
*/
|
||||
function backup_migrate_backup_migrate_sources() {
|
||||
$out = array();
|
||||
|
||||
// Expose the configured databases as sources.
|
||||
backup_migrate_include('filters');
|
||||
$out += backup_migrate_filters_invoke_all('sources');
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the source options as a form element.
|
||||
*/
|
||||
function _backup_migrate_get_source_form($source_id = 'db') {
|
||||
backup_migrate_include('destinations');
|
||||
|
||||
$form = array();
|
||||
$sources = _backup_migrate_get_source_pulldown($source_id);
|
||||
if (count($sources['#options']) > 1) {
|
||||
$form['source'] = array(
|
||||
"#type" => "fieldset",
|
||||
"#title" => t("Backup Source"),
|
||||
"#collapsible" => TRUE,
|
||||
"#collapsed" => FALSE,
|
||||
"#tree" => FALSE,
|
||||
);
|
||||
$sources['#description'] = t("Choose the database to backup. Any database destinations you have created and any databases specified in your settings.php can be backed up.");
|
||||
|
||||
$form['source']['source_id'] = $sources;
|
||||
}
|
||||
else {
|
||||
$form = array();
|
||||
$form['source']['source_id'] = array(
|
||||
"#type" => "value",
|
||||
"#value" => $source_id,
|
||||
);
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get pulldown to select existing source options.
|
||||
*/
|
||||
function _backup_migrate_get_source_pulldown($source_id = NULL) {
|
||||
$sources = _backup_migrate_get_source_form_item_options();
|
||||
$form = array(
|
||||
"#type" => "select",
|
||||
"#title" => t("Backup Source"),
|
||||
"#options" => _backup_migrate_get_source_form_item_options(),
|
||||
"#default_value" => $source_id,
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the location options as an options array for a form item.
|
||||
*/
|
||||
function _backup_migrate_get_source_form_item_options() {
|
||||
$out = array();
|
||||
foreach (backup_migrate_get_sources() as $key => $location) {
|
||||
$out[$key] = $location->get_name();
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* A base class for creating sources.
|
||||
*/
|
||||
class backup_migrate_source extends backup_migrate_location {
|
||||
var $db_table = "backup_migrate_sources";
|
||||
var $type_name = 'source';
|
||||
var $singular = 'source';
|
||||
var $plural = 'sources';
|
||||
var $title_plural = 'Sources';
|
||||
var $title_singular = 'Source';
|
||||
|
||||
/**
|
||||
* This function is not supposed to be called. It is just here to help the po extractor out.
|
||||
*/
|
||||
function strings() {
|
||||
// Help the pot extractor find these strings.
|
||||
t('source');
|
||||
t('sources');
|
||||
t('Sources');
|
||||
t('Source');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the available location types.
|
||||
*/
|
||||
function location_types() {
|
||||
return backup_migrate_get_source_subtypes();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A base class for creating sources.
|
||||
*/
|
||||
class backup_migrate_source_remote extends backup_migrate_source {
|
||||
/**
|
||||
* The location is a URI so parse it and store the parts.
|
||||
*/
|
||||
function get_location() {
|
||||
return $this->url(FALSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* The location to display is the url without the password.
|
||||
*/
|
||||
function get_display_location() {
|
||||
return $this->url(TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the location with the password.
|
||||
*/
|
||||
function set_location($location) {
|
||||
$this->location = $location;
|
||||
$this->set_url($location);
|
||||
}
|
||||
|
||||
/**
|
||||
* source configuration callback.
|
||||
*/
|
||||
function edit_form() {
|
||||
$form = parent::edit_form();
|
||||
$form['scheme'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Scheme"),
|
||||
"#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : '',
|
||||
"#required" => TRUE,
|
||||
"#weight" => 0,
|
||||
);
|
||||
$form['host'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Host"),
|
||||
"#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost',
|
||||
"#required" => TRUE,
|
||||
"#weight" => 10,
|
||||
);
|
||||
$form['path'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Path"),
|
||||
"#default_value" => @$this->dest_url['path'],
|
||||
"#required" => TRUE,
|
||||
"#weight" => 20,
|
||||
);
|
||||
$form['user'] = array(
|
||||
"#type" => "textfield",
|
||||
"#title" => t("Username"),
|
||||
"#default_value" => @$this->dest_url['user'],
|
||||
"#required" => TRUE,
|
||||
"#weight" => 30,
|
||||
);
|
||||
$form['pass'] = array(
|
||||
"#type" => "password",
|
||||
"#title" => t("Password"),
|
||||
"#default_value" => @$this->dest_url['pass'],
|
||||
'#description' => '',
|
||||
"#weight" => 40,
|
||||
);
|
||||
if (@$this->dest_url['pass']) {
|
||||
$form['old_password'] = array(
|
||||
"#type" => "value",
|
||||
"#value" => @$this->dest_url['pass'],
|
||||
);
|
||||
$form['pass']["#description"] .= t(' You do not need to enter a password unless you wish to change the currently saved password.');
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit the configuration form. Glue the url together and add the old password back if a new one was not specified.
|
||||
*/
|
||||
function edit_form_submit($form, &$form_state) {
|
||||
$form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password'];
|
||||
$form_state['values']['location'] = $this->glue_url($form_state['values'], FALSE);
|
||||
parent::edit_form_submit($form, $form_state);
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue