]> andersk Git - sql.git/blob - libexec/afs-run-backup.php
6c9b75d59681d202fa0b1da457671508334f43d6
[sql.git] / libexec / afs-run-backup.php
1 #!/usr/bin/env php
2 <?php
3
4 $EXCEPTIONS = explode("\n",file_get_contents('/mit/sql/etc/db_no-daily-backup'));
5 $EXCEPTIONS = array_filter($EXCEPTIONS, 'strlen');
6
7 ini_set('memory_limit', '-1');
8 set_time_limit(0);
9 ignore_user_abort(1);
10
11 define('BACKUP_RUN_STATS', 0);
12 define('BACKUP_CRON', 1);
13
14 $bin_mysql='mysql -umit-backup';
15 $bin_mysqldump='mysqldump -umit-backup';
16 $dump_path='/afs/athena.mit.edu/contrib/sql/backup';
17 $partition_path='/afs/athena.mit.edu/contrib/sql/.backup';
18 $dump_exec = "$bin_mysqldump -efqQK %s | gzip > $dump_path/%s.sql.gz ";
19 $dump_host=':/srv/mysql/mysql.sock';
20 $dump_user='mit-backup';
21 $dump_pass='';
22 $sql_resource = mysql_connect($dump_host,$dump_user,$dump_pass);
23
24 $sql_databases = mysql_list_dbs($sql_resource);
25 $stat['nDatabases'] = mysql_num_rows($sql_databases);
26
27 @unlink("$partition_path/skipped");
28 while($row1 = mysql_fetch_row($sql_databases)) {
29         if ($row1[0] == 'information_schema')
30                 continue;
31         if (preg_match('/[^a-z0-9\+_.-]+/i', $row1[0])) {
32                 $f = fopen("$partition_path/skipped",'a');
33                 fwrite($f, "{$row1[0]}\n");
34                 fclose($f);
35         }
36         $databases[$row1[0]]['name'] = $row1[0];
37 }
38
39 $i_database = 1;
40 $time[1][0] = microtime(true);
41 foreach($databases as $name=>$info) {
42         if (in_array($name, $EXCEPTIONS)) {
43                 if (!BACKUP_CRON)
44                         printf("Skipping database: %s\n", $name);
45                 continue;
46         } else {
47                 if (!BACKUP_CRON)
48                         printf("Dumping database: %s\n", $name);
49         }
50         if (stristr($name,'+')) {
51                 $owner = array_shift(explode('+',$name));
52         } else {
53                 $owner = 'root';
54         }
55         $ownerl = strtolower($owner);
56         if (!is_link($dump_path.'/'.$owner)) {
57                 symlink($partition_path.'/'.substr($ownerl,0,1).'/'.$owner, $dump_path.'/'.$owner);
58         }
59         if (!is_dir(readlink($dump_path.'/'.$owner))) {
60                 mkdir(readlink($dump_path.'/'.$owner));
61         }
62         `fs sa $dump_path/$owner/ system:anyuser none`;
63         `fs sa $dump_path/$owner/ system:authuser none`;
64         if ($owner == 'root') {
65                 `fs sa $dump_path/$owner/ system:sql-backup none`;
66         }
67         $exec = sprintf($dump_exec, $name, $owner.'/'.$name);
68 /*
69         if ($owner != 'root' && file_exists("$dump_path/$owner/$name.sql.gz")) {
70                 if (!BACKUP_CRON)
71                         printf("Saving old dump file: %s\n",
72                         "$dump_path/$owner/$name.sql.gz");
73                 rename("$dump_path/$owner/$name.sql.gz", "$dump_path/OLD/$name.sql.gz");
74         }
75 */
76         if (!BACKUP_CRON)
77                 printf("Dumping %d of %d / %d%% (%s):\n",
78                 $i_database,
79                 $stat['nDatabases'],
80                 $i_database/$stat['nDatabases']*100,
81                 $name);
82         //printf("Executing $exec\n");
83
84         $time[$name][0] = microtime(true);
85         $dump_proc = popen($exec, 'r');
86         while(!feof($dump_proc))
87                 echo fread($dump_proc, 1024);
88         pclose($dump_proc);
89         $time[$name][1] = microtime(true);
90
91         if (!BACKUP_CRON)
92                 printf("\n");
93
94         $i_database++;
95 }
96 $time[1][1] = microtime(true);
97
98 if (!BACKUP_CRON)
99         printf("Finished.\n%s seconds.\n", $time[1][1]-$time[1][0]);
100
101 ?>
This page took 0.092493 seconds and 3 git commands to generate.