]>
Commit | Line | Data |
---|---|---|
a0ac57ea JP |
1 | #!/usr/bin/env php |
2 | <?php | |
3 | ||
4 | $EXCEPTIONS[] = 'presbrey+aol'; | |
5 | ||
6 | ini_set('memory_limit', '-1'); | |
7 | set_time_limit(0); | |
8 | ignore_user_abort(1); | |
9 | ||
10 | define('BACKUP_RUN_STATS', 0); | |
11 | define('BACKUP_CRON', 1); | |
12 | ||
13 | $bin_mysql='mysql -umit-backup';// --defaults-file=/afs/athena.mit.edu/user/p/r/presbrey/web/etc/sql-mit-edu.my.cnf'; | |
14 | $bin_mysqldump='mysqldump -umit-backup';// --defaults-file=/afs/athena.mit.edu/user/p/r/presbrey/web/etc/sql-mit-edu.my.cnf'; | |
15 | $dump_path='/afs/athena.mit.edu/contrib/sql/backup'; | |
16 | //$dump_exec = "$bin_mysqldump -CefqQK --single-transaction --result-file=$dump_path/%s %s 2>&1"; | |
17 | //$dump_exec = "$bin_mysqldump -CefqQK --result-file=$dump_path/%s %s 2>&1"; | |
18 | $dump_exec = "$bin_mysqldump -CefqQK %s | gzip > $dump_path/%s.sql.gz "; | |
19 | $dump_host='localhost'; | |
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 | if (BACKUP_RUN_STATS): | |
28 | if (!BACKUP_CRON) { | |
29 | printf("Polling statistics for {$stat['nDatabases']} databases...\n"); | |
30 | } | |
31 | ||
32 | $time[0][0] = microtime(true); | |
33 | while($row1 = mysql_fetch_row($sql_databases)) { | |
34 | $i_database++; | |
35 | $databases[$row1[0]]['name'] = $row1[0]; | |
36 | $sql_tables = mysql_query("SHOW TABLE STATUS FROM `{$row1[0]}`"); | |
37 | $databases[$row1[0]]['Tables'] = mysql_num_rows($sql_tables); | |
38 | while($row2 = mysql_fetch_assoc($sql_tables)) { | |
39 | $databases[$row1[0]]['Rows'] += $row2['Rows']; | |
40 | $databases[$row1[0]]['Data_length'] += $row2['Data_length']; | |
41 | $databases[$row1[0]]['Size'] += $row2['Data_length']; | |
42 | } | |
43 | if ($i_database%100==0) { | |
44 | if (!BACKUP_CRON) { | |
45 | printf("polled $i_database databases: %d%%\n", $i_database/$stat['nDatabases']*100); | |
46 | } | |
47 | break; | |
48 | } | |
49 | } | |
50 | $time[0][1] = microtime(true); | |
51 | ||
52 | foreach($databases as $name=>$info) { | |
53 | $stat['nTables'] += $info['Tables']; | |
54 | $stat['nRows'] += $info['Rows']; | |
55 | $stat['nSize'] += $info['Size']; | |
56 | } | |
57 | ||
58 | if (!BACKUP_CRON) { | |
59 | printf("\nPreparing backup...\n\nDatabases:\t%d\nTables:\t\t%d\nRows:\t\t%d\nSize:\t\t%d MB\n\n", | |
60 | $stat['nDatabases'], | |
61 | $stat['nTables'], | |
62 | $stat['nRows'], | |
63 | $stat['nSize']/1024/1024); | |
64 | } | |
65 | ||
66 | else: | |
67 | ||
68 | while($row1 = mysql_fetch_row($sql_databases)) { | |
69 | if ($row1[0] == 'information_schema') | |
70 | continue; | |
71 | $databases[$row1[0]]['name'] = $row1[0]; | |
72 | } | |
73 | ||
74 | endif; | |
75 | ||
76 | $i_database = 1; | |
77 | $time[1][0] = microtime(true); | |
78 | foreach($databases as $name=>$info) { | |
79 | if (in_array($name, $EXCEPTIONS)) { | |
80 | if (!BACKUP_CRON) { | |
81 | printf('Skipping EXCEPTIONed database: %s', $name); | |
82 | } | |
83 | continue; | |
84 | } | |
85 | if (stristr($name,'+')) { | |
86 | $owner = array_shift(explode('+',$name)).'/'; | |
87 | @mkdir("$dump_path/$owner"); | |
88 | `fs sa $dump_path/$owner $owner rl 2>/dev/null`; | |
89 | `fs sa $dump_path/$owner system:authuser none`; | |
90 | } else { | |
91 | $owner = 'root/'; | |
92 | @mkdir("$dump_path/$owner"); | |
93 | `fs sa $dump_path/$owner system:authuser none`; | |
94 | `fs sa $dump_path/$owner system:sql-backup none`; | |
95 | } | |
96 | $exec = sprintf($dump_exec, $name, $owner.$name); | |
97 | ||
98 | if (file_exists("$dump_path/$owner$name.sql.gz")) { | |
99 | if (!BACKUP_CRON) { | |
100 | printf("Saving old dump file: %s\n", | |
101 | "$dump_path/$owner$name.sql.gz"); | |
102 | } | |
103 | rename("$dump_path/$owner$name.sql.gz","$dump_path/OLD/$name.sql.gz"); | |
104 | } | |
105 | ||
106 | if (!BACKUP_CRON) { | |
107 | printf("Dumping %d of %d / %d%% (%s):\n", | |
108 | $i_database, | |
109 | $stat['nDatabases'], | |
110 | $i_database/$stat['nDatabases']*100, | |
111 | $name); | |
112 | } | |
113 | //printf("Executing $exec\n"); | |
114 | ||
115 | $time[$name][0] = microtime(true); | |
116 | $dump_proc = popen($exec, 'r'); | |
117 | while(!feof($dump_proc)) | |
118 | echo fread($dump_proc, 1024); | |
119 | pclose($dump_proc); | |
120 | $time[$name][1] = microtime(true); | |
121 | ||
122 | if (!BACKUP_CRON) { | |
123 | printf("\n"); | |
124 | } | |
125 | ||
126 | $i_database++; | |
127 | } | |
128 | $time[1][1] = microtime(true); | |
129 | ||
130 | if (!BACKUP_CRON) { | |
131 | printf("Finished.\n%s seconds.\n", $time[1][1]-$time[1][0]); | |
132 | } | |
133 | ||
134 | ?> |