/[drupal]/contributions/modules/backup/backup_lib.php
ViewVC logotype

Contents of /contributions/modules/backup/backup_lib.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Fri Mar 16 04:23:42 2007 UTC (2 years, 8 months ago) by dmuth
Branch: MAIN
CVS Tags: DRUPAL-5--1-1, DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Drupal 5 release!

http://drupal.org/node/120593
1 <?php
2 /**
3 * This file contains a function library for both the backup module and
4 * the backup script.
5 *
6 */
7
8
9 /**
10 * This function backs up our database.
11 *
12 * @param string $db_file This is set to the name of the file which holds
13 * the database.
14 *
15 * @return mixed NULL is returned on success. Otherwise an error is returned.
16 */
17 function backup_database(&$db_file) {
18
19 //
20 // Parse our database URL into credentials and remove the leading
21 // slash from the path as well.
22 //
23 $db_data = parse_url($GLOBALS["db_url"]);
24 $db_data["path"][0] = " ";
25
26 //
27 // Create our database temp file
28 //
29 $db_file_tmp = tempnam("/tmp", "drupal-backup-db-");
30
31 //
32 // Create our date string for filenames
33 //
34 $date_string = date("YmdHis");
35
36 //
37 // The name of our final db file.
38 // This includes a random number to keep attackers from guessing
39 // the filename.
40 //
41 $db_file = "drupal-backup-db-" . $date_string
42 . "-" . mt_rand(0, 999999999) . ".tgz";
43
44 if (empty($db_file_tmp)) {
45 $error = "Call to tempnam() failed";
46 return($error);
47 }
48
49 //
50 // Now dump our database to the temp file
51 //
52 $cmd = "mysqldump -u " . $db_data["user"]
53 . " -h " . $db_data["host"]
54 . " -p" . $db_data["pass"]
55 . " " . $db_data["path"]
56 . " |gzip >$db_file_tmp "
57 ;
58
59 $fp = popen($cmd, "r");
60
61 if (empty($fp)) {
62 $error = "Unable to run command '$cmd'";
63 return($error);
64 }
65
66 $retval = pclose($fp);
67
68 if ($retval != 0) {
69 $error = "Command '$cmd' returned value: $retval";
70 return($error);
71 }
72
73 //
74 // Move the temp file into the current directory (with a new name)
75 // so that it can be included in the tarball.
76 //
77 if (!rename($db_file_tmp, $db_file)) {
78 $error = "Renaming file '$db_file_tmp' to '$db_file' failed";
79 return($error);
80 }
81
82 // Assume success
83 return(null);
84
85 } // End of backup_database()
86
87
88 /**
89 * This function gets a list of files to back up. Any files that are
90 * already backups are excluded.
91 *
92 * @param array $files This is set to a list of filenames to be backed up.
93 *
94 * @return mixed NULL is returned on success. Otherwise an error is returned.
95 */
96 function backup_get_files(&$file_list) {
97
98 //
99 // Get a list of all files in the current directory, filter out the
100 // current and parent directories, and any existing backup files.
101 // Trying to make GNU tar's --exclude switch actually work for me
102 // was like trying to herd housecats that were hopped up on crack.
103 // It just wasn't going to happen. :-) Plus, this approach is
104 // more portable.
105 //
106 $file_list = "";
107
108 $fp = opendir(".");
109
110 while ($file = readdir($fp)) {
111 //
112 // Skip the current and parent directory
113 //
114 if ($file == "." || $file == "..") {
115 continue;
116 }
117
118 //
119 // Skip any backup files
120 //
121 if (strstr($file, "backup-")) {
122 continue;
123 }
124
125 $file_list .= $file . " ";
126
127 }
128
129 if (!$fp) {
130 $error = "Unable to open current directory";
131 return($error);
132 }
133
134 closedir($fp);
135
136 // Assume success
137 return(null);
138
139 } // End of backup_get_files()
140
141
142 /**
143 * This function backs up our file system under DOCUMENT_ROOT, which also
144 * includes the database dump.
145 *
146 * @param string $db_file The name of our database dump
147 *
148 * @param string $backup_file This is set to the name of the main backup
149 * file which is created.
150 *
151 * @return mixed NULL is returned on success. Otherwise an error is returned.
152 */
153 function backup_files($db_file, &$backup_file) {
154
155 $error = backup_get_files($file_list);
156 if (!empty($error)) {
157 $error = "backup_get_files(): " . $error;
158 return($error);
159 }
160
161 //
162 // Finally, add in our database backup. It's excluded since it's
163 // a backup file just in case there's multiple database dumps lying
164 // around from previous backups that were aborted. But we want to
165 // explicitly add in the dump from *this* run of backup.
166 //
167 $file_list .= $db_file;
168
169 //
170 // Now tar up the contents of this directory
171 //
172 $backup_tmp = tempnam("/tmp", "backup-htdocs-");
173
174 //
175 // This includes a random number to keep attackers from guessing
176 // the filename.
177 //
178 $date_string = date("YmdHis");
179 $backup_file = "drupal-backup-" . $date_string
180 . "-" . mt_rand(0, 999999999) . ".tgz";
181
182 $cmd = "tar cfz $backup_tmp $file_list 2>&1";
183
184 $fp = popen($cmd, "r");
185
186 if (empty($fp)) {
187 $error = "Unable to run command '$cmd'";
188 return($error);
189 }
190
191 while ($line = fgets($fp)) {
192 print "tar output: $line";
193 }
194
195 $retval = pclose($fp);
196
197 if ($retval != 0) {
198 $error = "Command '$cmd' returned value: $retval";
199 return($error);
200 }
201
202 //
203 // Now remove the database file, we don't need it anymore.
204 //
205 if (!unlink($db_file)) {
206 $error = "Unable to delete file '$db_file'";
207 return($error);
208 }
209
210 //
211 // Finally, move the tarball into this directory so the user can grab it
212 //
213 if (!rename($backup_tmp, $backup_file)) {
214 $error = "Renaming file '$backup_tmp' to '$backup_file' failed";
215 return($error);
216 }
217
218 //
219 // Make our backup file world-readable.
220 //
221 if (!chmod($backup_file, 0644)) {
222 $error = "chmod() failed";
223 return($error);
224 }
225
226 // Assume success
227 return(null);
228
229 } // End of backup_files()
230
231
232 /**
233 * This is the main function which does all of our work.
234 *
235 * @return mixed NULL is returned on success, otherwise an error is returned.
236 * This allows us to "catch" errors and let them bubble up the call stack,
237 * not unlike exceptions in PHP 5.
238 */
239 function main() {
240
241
242 } // End of main()
243
244
245 /*
246 if ($error = main()) {
247 print $argv[0] . ": Error: $error\n";
248 exit (1);
249 }
250 */
251
252
253 ?>

  ViewVC Help
Powered by ViewVC 1.1.2