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