| 23 | rodolico | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | /*
 | 
        
           |  |  | 4 |  * Requires php-pear and libyaml under Debian Wheezy
 | 
        
           |  |  | 5 |  *
 | 
        
           |  |  | 6 |  * apt-get install php-pear php5-dev libyaml-dev libyaml-0-2 libyaml-0-2-dbg
 | 
        
           |  |  | 7 |  * pecl install yaml
 | 
        
           |  |  | 8 |  * echo 'extension=yaml.so' >> /etc/php5/cli/php.ini
 | 
        
           |  |  | 9 |  * echo 'extension=yaml.so' >> /etc/apache2/cli/php.ini
 | 
        
           |  |  | 10 |  * 
 | 
        
           | 25 | rodolico | 11 |  * truncate table backups_run
 | 
        
           | 24 | rodolico | 12 |  * alter table backups_run add skipped bigint(20),add file_name varchar(60),add notes text;
 | 
        
           |  |  | 13 |  * alter table backups_run modify end_time  datetime null, modify start_time datetime null;
 | 
        
           | 25 | rodolico | 14 |  * alter table backups_run add added_date datetime not null;
 | 
        
           | 23 | rodolico | 15 |  * 
 | 
        
           | 28 | rodolico | 16 |  * Version 0.1.1 20160713 RWR
 | 
        
           |  |  | 17 |  * Fixed issue where report had no transfers. In that case, header lines
 | 
        
           |  |  | 18 |  * are terminated by two blank lines, not the constant 'sending incremental file
 | 
        
           |  |  | 19 |  * list'. Simply modified to terminate on an empty line OR the constant
 | 
        
           |  |  | 20 |  * 
 | 
        
           |  |  | 21 |  * Also set it up to only report on one file at a time, ie accept
 | 
        
           |  |  | 22 |  * only one parameter, and that is assumed to be a file name
 | 
        
           | 23 | rodolico | 23 | */
 | 
        
           |  |  | 24 |   | 
        
           | 25 | rodolico | 25 |    /*
 | 
        
           |  |  | 26 |     * This will return error codes
 | 
        
           |  |  | 27 |     * 1 - Invalid Data File Name
 | 
        
           |  |  | 28 |     * 2 - Could not open the file
 | 
        
           |  |  | 29 |     * 3 - Duplicate Report
 | 
        
           |  |  | 30 |     * 4 - Server Not Found
 | 
        
           |  |  | 31 |     */
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 |   | 
        
           | 23 | rodolico | 34 | require 'library.php';
 | 
        
           | 24 | rodolico | 35 | $VERSION='0.1.0';
 | 
        
           | 23 | rodolico | 36 |   | 
        
           | 24 | rodolico | 37 | $results = array();
 | 
        
           | 23 | rodolico | 38 |   | 
        
           | 24 | rodolico | 39 | function parseFile( $filename, $path, $maxLineLength = 4096 ) {
 | 
        
           |  |  | 40 |    $data = array ( 
 | 
        
           |  |  | 41 |                      'server_name' => '',
 | 
        
           |  |  | 42 |                      'report_date' => '',
 | 
        
           |  |  | 43 |                      'start_time' => '',
 | 
        
           |  |  | 44 |                      'version' => '',
 | 
        
           |  |  | 45 |                      'end_time' => '',
 | 
        
           | 28 | rodolico | 46 |                      'files_count' => 0,
 | 
        
           |  |  | 47 |                      'files_size' => 0,
 | 
        
           | 24 | rodolico | 48 |                      'transferred_count' => 0,
 | 
        
           | 28 | rodolico | 49 |                      'transferred_size' => 0,
 | 
        
           | 24 | rodolico | 50 |                      'files_deleted' => 0,
 | 
        
           |  |  | 51 |                      'skipped' => 0,
 | 
        
           | 28 | rodolico | 52 |                      'data_sent' => 0,
 | 
        
           |  |  | 53 |                      'data_received' => 0,
 | 
        
           | 24 | rodolico | 54 |                      'disk_used' => '',
 | 
        
           |  |  | 55 |                      'notes' => '',
 | 
        
           |  |  | 56 |    );
 | 
        
           | 23 | rodolico | 57 |   | 
        
           |  |  | 58 |   | 
        
           | 24 | rodolico | 59 |    $matches = array();
 | 
        
           | 23 | rodolico | 60 |   | 
        
           | 24 | rodolico | 61 |    $line;
 | 
        
           | 23 | rodolico | 62 |   | 
        
           | 24 | rodolico | 63 |    $data['file_name'] = $filename;
 | 
        
           |  |  | 64 |    # get server name from the file name, which should be
 | 
        
           |  |  | 65 |    # datetime_servername.backup.log
 | 
        
           | 25 | rodolico | 66 |    if ( preg_match( '/(\d+)_([a-z0-9_.-]+)\.backup\.log/', $filename, $matches ) ) {
 | 
        
           | 24 | rodolico | 67 |       $data['report_date'] = $matches[1];
 | 
        
           |  |  | 68 |       $data['server_name'] = $matches[2];
 | 
        
           |  |  | 69 |    } else {
 | 
        
           |  |  | 70 |       # ensure only three parts returned
 | 
        
           |  |  | 71 |       $data['error'] = "1\tInvalid Data File Name";
 | 
        
           |  |  | 72 |       return $data;
 | 
        
           | 23 | rodolico | 73 |    }
 | 
        
           |  |  | 74 |   | 
        
           | 24 | rodolico | 75 |    $log = fopen ( $path . '/' . $filename , 'rb' );
 | 
        
           |  |  | 76 |    if ( $log ) {
 | 
        
           | 28 | rodolico | 77 |       // get through the header lines, ended by either a blank line
 | 
        
           |  |  | 78 |       // or the constant 'sending incremental file list'
 | 
        
           | 24 | rodolico | 79 |       while (( $line = fgets( $log, $maxLineLength )) !== false ) {
 | 
        
           | 28 | rodolico | 80 |          $line = rtrim( $line );
 | 
        
           |  |  | 81 |          if ( $line ) {
 | 
        
           |  |  | 82 |             if ( preg_match( '/^backup\s+v?([\d.]+)$/', $line, $matches ) ) {
 | 
        
           |  |  | 83 |                $data['version'] = $matches[1];
 | 
        
           |  |  | 84 |             }
 | 
        
           |  |  | 85 |             // this line is the end of the header lines
 | 
        
           |  |  | 86 |             if ( preg_match( '/sending incremental file list/', $line, $matches ) ) {
 | 
        
           |  |  | 87 |                break;
 | 
        
           |  |  | 88 |             }
 | 
        
           |  |  | 89 |          } else {
 | 
        
           | 24 | rodolico | 90 |             break;
 | 
        
           |  |  | 91 |          }
 | 
        
           | 23 | rodolico | 92 |       }
 | 
        
           |  |  | 93 |   | 
        
           | 24 | rodolico | 94 |       // count line by line processing information. These are the actual lines
 | 
        
           |  |  | 95 |       // indicating files checked and how they were processed
 | 
        
           |  |  | 96 |       while (( $line = fgets( $log, $maxLineLength )) !== false ) {
 | 
        
           |  |  | 97 |          $line = rtrim($line);
 | 
        
           |  |  | 98 |          if ( $line ) {
 | 
        
           |  |  | 99 |             if ( ! preg_match( '~/$~', $line ) ) // count lines not ending in /, ie not directories
 | 
        
           |  |  | 100 |                $data['transferred_count']++;
 | 
        
           |  |  | 101 |             if ( preg_match( '/^deleting /', $line ) ) // count number deleted
 | 
        
           |  |  | 102 |                $data['files_deleted']++;
 | 
        
           |  |  | 103 |             if ( preg_match( '/^skipping /', $line ) )  // count number skipped
 | 
        
           |  |  | 104 |                $data['skipped']++ ;
 | 
        
           |  |  | 105 |          } else {
 | 
        
           |  |  | 106 |             break;
 | 
        
           |  |  | 107 |          }
 | 
        
           | 23 | rodolico | 108 |       }
 | 
        
           | 24 | rodolico | 109 |       $data['transferred_count'] -= $data['files_deleted'] + $data['skipped'];
 | 
        
           | 23 | rodolico | 110 |   | 
        
           | 24 | rodolico | 111 |       // we should have a stats summary at the end
 | 
        
           |  |  | 112 |       while (( $line = fgets( $log, $maxLineLength )) !== false ) {
 | 
        
           |  |  | 113 |          $data['notes'] .= $line; // everything here goes into notes
 | 
        
           |  |  | 114 |          if ( preg_match( '/Number of files:\s+(\d+)/', $line, $matches ) ) {
 | 
        
           |  |  | 115 |             $data['files_count'] = $matches[1];
 | 
        
           |  |  | 116 |          } elseif ( preg_match( '/Number of files transferred:\s+(\d+)/', $line, $matches ) ) {
 | 
        
           |  |  | 117 |             $data['transferred_count'] = $matches[1];
 | 
        
           |  |  | 118 |          } elseif ( preg_match( '/Total file size:\s+(\d+) bytes/', $line, $matches )  ) {
 | 
        
           |  |  | 119 |             $data['files_size'] = $matches[1];
 | 
        
           |  |  | 120 |          } elseif ( preg_match( '/Total transferred file size:\s+(\d+) bytes/', $line, $matches )  ) {
 | 
        
           |  |  | 121 |             $data['transferred_size'] = $matches[1];
 | 
        
           |  |  | 122 |          } elseif ( preg_match( '/Total bytes sent:\s+(\d+)/', $line, $matches )  ) {
 | 
        
           |  |  | 123 |             $data['data_sent'] = $matches[1];
 | 
        
           |  |  | 124 |          } elseif ( preg_match( '/Total bytes received:\s+(\d+)/', $line, $matches )  ) {
 | 
        
           |  |  | 125 |             $data['data_received'] = $matches[1];
 | 
        
           |  |  | 126 |          } elseif ( preg_match( '/Backup Version\s+v?([\d.]+)/', $line, $matches )  ) {
 | 
        
           |  |  | 127 |             // this is the end of the rsync stats summary, and the
 | 
        
           |  |  | 128 |             // beginning of the rsbackup summary
 | 
        
           |  |  | 129 |             $data['version'] = $matches[1];
 | 
        
           |  |  | 130 |             break;
 | 
        
           |  |  | 131 |          }
 | 
        
           | 23 | rodolico | 132 |       }
 | 
        
           |  |  | 133 |   | 
        
           | 24 | rodolico | 134 |       // process rsbackup summary
 | 
        
           |  |  | 135 |       while (( $line = fgets( $log, $maxLineLength )) !== false ) {
 | 
        
           |  |  | 136 |          $data['notes'] .= $line;
 | 
        
           |  |  | 137 |          if ( preg_match( '/^Begin\s+(\d+)/', $line, $matches )   ) {
 | 
        
           |  |  | 138 |             $data['start_time'] = $matches[1];
 | 
        
           |  |  | 139 |          } elseif ( preg_match( '/Complete\s+(\d+)/', $line, $matches )   ) {
 | 
        
           |  |  | 140 |             $data['end_time'] = $matches[1];
 | 
        
           |  |  | 141 |          } elseif ( preg_match( '/^Status:/', $line, $matches )   ) {
 | 
        
           |  |  | 142 |             break;
 | 
        
           | 23 | rodolico | 143 |          }
 | 
        
           |  |  | 144 |       }
 | 
        
           | 24 | rodolico | 145 |       // the rest of it just goes into notes
 | 
        
           |  |  | 146 |       while (( $line = fgets( $log, $maxLineLength )) !== false ) {
 | 
        
           |  |  | 147 |          $data['notes'] .= $line;
 | 
        
           | 23 | rodolico | 148 |       }
 | 
        
           | 24 | rodolico | 149 |       fclose ( $log );
 | 
        
           |  |  | 150 |    } else {
 | 
        
           |  |  | 151 |       $data['error'] = "2\tCould not open file name $filename for read";
 | 
        
           | 23 | rodolico | 152 |    }
 | 
        
           | 24 | rodolico | 153 |    return $data;
 | 
        
           | 23 | rodolico | 154 |   | 
        
           | 24 | rodolico | 155 | }
 | 
        
           | 23 | rodolico | 156 |   | 
        
           |  |  | 157 |   | 
        
           | 24 | rodolico | 158 | function recordMessage( $message ) {
 | 
        
           |  |  | 159 |    global $results;
 | 
        
           |  |  | 160 |    $results[] = $message;
 | 
        
           |  |  | 161 | }
 | 
        
           | 23 | rodolico | 162 |   | 
        
           |  |  | 163 |   | 
        
           | 24 | rodolico | 164 | /* 
 | 
        
           |  |  | 165 |  * checks for a duplicate report, ie one that has already been run.
 | 
        
           |  |  | 166 |  * if this computer has a report already for this date/time
 | 
        
           |  |  | 167 |  */ 
 | 
        
           |  |  | 168 | function checkDuplicate( $backups_id, $report_date) {
 | 
        
           |  |  | 169 |    $sql = "select count(*) from backups_run where backups_id = $backups_id and report_date = $report_date";
 | 
        
           |  |  | 170 |    $count = getOneDBValue( $sql );
 | 
        
           |  |  | 171 |    return $count ? "3\tDuplicate Report for $backups_id on $report_date" : null;
 | 
        
           |  |  | 172 | } // recordReport
 | 
        
           | 23 | rodolico | 173 |   | 
        
           |  |  | 174 |   | 
        
           | 24 | rodolico | 175 | function findServer( $name ) {
 | 
        
           |  |  | 176 |    $sql = "select distinct( device_id ) from (select device_id from device where name = '$name' union select device_id from device_alias where alias = '$name') a";
 | 
        
           |  |  | 177 |    $device_id = getOneDBValue( $sql );
 | 
        
           |  |  | 178 |    if ( ! isset( $device_id ) ) {
 | 
        
           |  |  | 179 |       recordMessage( "Could not locate device $name, not processing record" );
 | 
        
           |  |  | 180 |       return null;
 | 
        
           | 23 | rodolico | 181 |    }
 | 
        
           | 24 | rodolico | 182 |    $sql = "select backups_id from backups where device_id = $device_id";
 | 
        
           |  |  | 183 |    $backups_id = getOneDBValue( $sql );
 | 
        
           |  |  | 184 |    if ( isset( $backups_id ) ) {
 | 
        
           |  |  | 185 |       return $backups_id;
 | 
        
           |  |  | 186 |    } else {
 | 
        
           |  |  | 187 |       recordMessage( "Adding server $name to backups" );
 | 
        
           |  |  | 188 |       $sql = "insert into backups ( device_id,backups_server_id,notes ) values ( $device_id, 1, 'Automatically Added' )";
 | 
        
           |  |  | 189 |       $results = queryDatabaseExtended( $sql );
 | 
        
           |  |  | 190 |       return $results['insert_id'];
 | 
        
           |  |  | 191 |    }
 | 
        
           |  |  | 192 | }
 | 
        
           | 23 | rodolico | 193 |   | 
        
           |  |  | 194 |   | 
        
           | 24 | rodolico | 195 | /* 
 | 
        
           |  |  | 196 |  * Creates an entry in the backups_run table
 | 
        
           |  |  | 197 |  */ 
 | 
        
           |  |  | 198 | function recordReport( $report ) {
 | 
        
           |  |  | 199 |   | 
        
           |  |  | 200 |    $keys = array();
 | 
        
           |  |  | 201 |    $values = array();
 | 
        
           |  |  | 202 |   | 
        
           |  |  | 203 |    foreach ( $report as $keyfield => $value ) {
 | 
        
           |  |  | 204 |       if ( $keyfield == 'server_name' ) {
 | 
        
           |  |  | 205 |          $keys[] = 'backups_id';
 | 
        
           |  |  | 206 |          $device_id = findServer( $value );
 | 
        
           |  |  | 207 |          if ( isset( $device_id ) ) {
 | 
        
           |  |  | 208 |             $values[] = $device_id;
 | 
        
           |  |  | 209 |             $duplicate = checkDuplicate( $device_id, $report['report_date'] );
 | 
        
           |  |  | 210 |             if ( $duplicate )
 | 
        
           |  |  | 211 |                return $report['file_name'] . "\t$duplicate";
 | 
        
           |  |  | 212 |          } else {
 | 
        
           |  |  | 213 |             return $report['file_name'] . "\t4\tServer not found";
 | 
        
           | 23 | rodolico | 214 |          }
 | 
        
           | 24 | rodolico | 215 |       } else {
 | 
        
           |  |  | 216 |          $keys[] = $keyfield;
 | 
        
           |  |  | 217 |          $values[] = makeSafeSQLValue( $value );
 | 
        
           | 23 | rodolico | 218 |       }
 | 
        
           | 24 | rodolico | 219 |    }
 | 
        
           | 25 | rodolico | 220 |    $keys[] = 'added_date'; $values[] = 'now()';
 | 
        
           | 24 | rodolico | 221 |    $sql = 'insert into backups_run (' . implode( ',', $keys ) . ') values (' . implode( ',', $values ) . ')';
 | 
        
           |  |  | 222 |    // if we made it this far, we are ok, so just add the report id
 | 
        
           |  |  | 223 |    $result = queryDatabaseExtended( $sql );
 | 
        
           |  |  | 224 |    return $report['file_name'] . "\t0\tok";
 | 
        
           |  |  | 225 | } // recordReport
 | 
        
           | 23 | rodolico | 226 |   | 
        
           |  |  | 227 |   | 
        
           |  |  | 228 | /*
 | 
        
           |  |  | 229 |  * we don't know where the configuration file will be, so we have a list
 | 
        
           |  |  | 230 |  * below (searchPaths) to look for it. It will load the first one it 
 | 
        
           |  |  | 231 |  * finds, then exit. THUS, you could have multiple configuration files
 | 
        
           |  |  | 232 |  * but only the first one in the list will be used
 | 
        
           |  |  | 233 |  */
 | 
        
           |  |  | 234 |   | 
        
           | 24 | rodolico | 235 | $confFileName = "rsbackupRead.conf.yaml";
 | 
        
           |  |  | 236 | $searchPaths = array( '/etc/camp', '/opt/camp', '/opt/camp/rsbackup', $_SERVER['SCRIPT_FILENAME'], getcwd() );
 | 
        
           | 23 | rodolico | 237 | $configuration = array();
 | 
        
           |  |  | 238 | foreach ( $searchPaths as $path ) {
 | 
        
           |  |  | 239 |    if ( file_exists( "$path/$confFileName" ) ) {
 | 
        
           |  |  | 240 |       $configuration = yaml_parse_file( "$path/$confFileName" );
 | 
        
           |  |  | 241 |       break; // exit out of the loop; we don't try to load it more than once
 | 
        
           |  |  | 242 |    } // if
 | 
        
           |  |  | 243 | } // foreach
 | 
        
           |  |  | 244 |   | 
        
           | 24 | rodolico | 245 |   | 
        
           | 23 | rodolico | 246 | mysql_connect( $configuration['database']['databaseServer'], $configuration['database']['databaseUsername'], $configuration['database']['databasePassword'] ) or die(mysql_error());
 | 
        
           |  |  | 247 | mysql_select_db( $configuration['database']['database'] ) or die(mysql_error());
 | 
        
           |  |  | 248 |   | 
        
           |  |  | 249 |   | 
        
           |  |  | 250 |   | 
        
           | 28 | rodolico | 251 | $filename = $argv[1];
 | 
        
           |  |  | 252 | $report = parseFile( $filename, $configuration['datapath'] . '/' . $configuration['unprocessed_path'] );
 | 
        
           |  |  | 253 | // print_r( $report ); print "\n";
 | 
        
           |  |  | 254 |   | 
        
           |  |  | 255 | if ( isset( $report['error'] ) ) { // we had an error processing the report
 | 
        
           |  |  | 256 |    $result[] = "$filename\t" . $report['error']; 
 | 
        
           |  |  | 257 | } else {
 | 
        
           |  |  | 258 |    $result[] = recordReport( $report );
 | 
        
           | 23 | rodolico | 259 | }
 | 
        
           |  |  | 260 |   | 
        
           | 24 | rodolico | 261 | print implode( "\n", $result );
 | 
        
           |  |  | 262 |   | 
        
           | 23 | rodolico | 263 | ?>
 |