From: Joe Z. <jz...@us...> - 2004-05-23 21:25:47
|
Update of /cvsroot/bobs/bobs/inc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21371/inc Modified Files: class_db.php class_server.php Added Files: class_cleanup.php Log Message: Remove old incremental files and directories during daily backup. --- NEW FILE: class_cleanup.php --- <?php /* description: cleanup functions Remove incremental files as specified in server configuration. See doc/fileformat for the format of the databases. */ ///////////////////////////////////////////////////////////////////// // class cleanup variables ///////////////////////////////////////////////////////////////////// class cleanup extends database { var $old_days = ''; // Days to retain incremental files var $old_date = ''; // Retention date: yyyy.ddd //////////////////////////////////////////////////////////////////// // class cleanup constructor //////////////////////////////////////////////////////////////////// function cleanup ($backup_config, $old_days) { parent::database($backup_config); // Calculate retention date $this->old_days = $old_days; $day_sec = 60 * 60 * 24; // Seconds in day $old_seconds = $old_days * $day_sec; // Seconds to retain files $today_stamp = date("U"); // Today's time stamp $old_stamp = $today_stamp - $old_seconds; // Timestamp of retention date $this->old_date = date("Y", $old_stamp) . '.' . date("z", $old_stamp); } //////////////////////////////////////////////////////////////////// // Delete old incremental files //////////////////////////////////////////////////////////////////// function delete_files() { // Zero retention days means to not delete old incremental files if ($this->old_days <= 0) return(0); print "Removing incremental files older than (YYYY.DDD): $this->old_date\n"; // CRUD! Keys are NOT in order in db cause they are treated like strings // and they're not padded with zeroes. // Create array to hold keys of files to delete so I can delete them in // alphabetical order so they will show in order in the log. $keys = array(); // Get db file name for current incremental $dbfile = $this->db_location("incremental", "file"); // Open file db if (! $handle = dba_open($dbfile, "w", $this->db_type)){ print("Error: Unable to open database file $dbfile\n"); return(1); } // Read through the entire database and save into an array // the database keys of files to be deleted. // if (($key = dba_firstkey($handle)) == FALSE) return(0); // Can't test first key because first key in db is actually 0 (false) $key = dba_firstkey($handle); do { // Record type is determined by last digit of key // Get file name (key ends in '1') if(substr($key, -1 , 1) == '1'){ // This is a file name $file = dba_fetch($key, $handle); if(substr($file, -8) < $this->old_date){ // File is old $keys[] = intval($key) - 1; // Save '0' key } } } while(($key = dba_nextkey($handle)) != FALSE); // Sort and itterate through the array, // deleting files along the way. asort($keys, SORT_NUMERIC); reset($keys); // rewind array while (list($key, $val) = each($keys)) { $path = dba_fetch($val, $handle); // Get '0' key (path) $file = dba_fetch($val + 1, $handle); // Get '1' key (filename) $fullpath = $this->incrementdir . '/' . $this->server . '/' . $this->share . '/' . $path . '/' . $file; // combine base dir + path + filename if(unlink($fullpath) == TRUE){ // unlink file verbosely print("Removed $fullpath\n"); for($i = 0; $i<6; $i++) // Delete keys '0' - '5' dba_delete($val + $i, $handle); } else { print("Unable to remove $fullpath\n"); } } dba_close($handle); } //////////////////////////////////////////////////////////////////// // Delete empty incremental directories //////////////////////////////////////////////////////////////////// function delete_dirs() { // Zero retention days means to not delete old incremental dirs if ($this->old_days <=0) return(0); // Open dir db $dbfile = $this->db_location("incremental", "dir"); if (! $handle = dba_open($dbfile, "w", $this->db_type)){ print("Error: Unable to open database file $dbfile\n"); return(1); } // If dir is empty, unlink it verbosely and remove the dir db record if (($key = dba_firstkey($handle)) == FALSE) return(0); do { $fullpath = $this->incrementdir . '/' . $this->server . '/' . $this->share . '/' . $key; if ($this->dir_is_empty($fullpath) == TRUE){ if(rmdir($fullpath) == TRUE){ // Delete dir print("Removed empty directory $fullpath\n"); dba_delete($key, $handle); } else { print("Unable to remove directory $fullpath\n"); } } } while(($key = dba_nextkey($handle)) != FALSE); // Close dir db dba_close($handle); } //////////////////////////////////////////////////////////////////// // Private function dir_is_empty // Returns TRUE if directory is empty //////////////////////////////////////////////////////////////////// function dir_is_empty($dir){ if (is_dir($dir)){ if (($dirhandle = opendir($dir)) == FALSE) return(FALSE); // Error while(($file = readdir($dirhandle)) !== FALSE){ if (($file != ".") && ($file != "..")){ closedir($dirhandle); return(FALSE); // Directory not empty } } } closedir($dirhandle); return(TRUE); // Directory is empty } } // end class ?> Index: class_db.php =================================================================== RCS file: /cvsroot/bobs/bobs/inc/class_db.php,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- class_db.php 26 Mar 2004 21:33:50 -0000 1.5 +++ class_db.php 23 May 2004 21:25:38 -0000 1.6 @@ -42,9 +42,9 @@ // check what dba type we should use $tmpfile = tempnam("/tmp", "BOBS"); - $dbcheck = @dba_open($tmpfile, "c", "db3"); + $dbcheck = @dba_open($tmpfile, "n", "db3"); if ( $dbcheck === FALSE ) { - $dbcheck = @dba_open($tmpfile, "c", "db4"); + $dbcheck = @dba_open($tmpfile, "n", "db4"); if ( $dbcheck === FALSE ) { echo "Could not create a database of type db3 or db4 (tried both)\n"; } else { @@ -56,6 +56,7 @@ $this->db_type = "db3"; } + unlink($tmpfile); return; } @@ -79,7 +80,7 @@ $this->db[$where][$type] = dba_open($db, "r", $this->db_type); } } else { - $this->db[$where][$type] = dba_open($db, "c", $this->db_type); + $this->db[$where][$type] = dba_open($db, "n", $this->db_type); } } return; Index: class_server.php =================================================================== RCS file: /cvsroot/bobs/bobs/inc/class_server.php,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- class_server.php 7 Apr 2004 22:38:25 -0000 1.9 +++ class_server.php 23 May 2004 21:25:38 -0000 1.10 @@ -287,6 +287,25 @@ } // --------------------------------------------------------- +// get_incrementals - Get number of days to keep incremental backups +// Used by: backup.php +// Parms: none +// Returns: Number of days to keep incremental backups. 0 = forever. +// Note: set_config() should have been previously called. +// --------------------------------------------------------- +function get_incrementals(){ + + $days = intval($this->config['incrementals']); + $days_per_week = strlen($this->config['run_days']); + if (($days > 0) && ($days_per_week > 0)){ + $retention_days = intval(round($days / $days_per_week * 7)); + } else { + $retention_days = 0; + } + return $retention_days; +} + +// --------------------------------------------------------- } // Class end bracket ?> |