Advertisement
Grizly

osTicket backup/delete

Dec 9th, 2013
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.13 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Removes obsolete tickets, can be run as often as you choose.
  4.  * Ideally via cron:
  5. # Every 28 days?
  6. * * /28 * * nobody /usr/bin/php /path/to/remove_obsolete_tickets.php
  7.  
  8. YOU WILL NEED TO CONFIGURE THE FOLLLOWING:
  9.  */
  10.  
  11. define('BACKUPDIR','/home/backups/');
  12. define('DELETEMONTHS', 6); //Any closed ticket older than this many months will be archived/deleted.
  13. define('MY_USER_ID', 'Grizly');
  14. define('CRONFRIENDLY', true); //change to false to show more output.
  15.  
  16. /**
  17.  * Should be able to ignore from here, until you are ready to start pruning.
  18.  * Then go to bottom, un-comment the line:  // return $t->delete();
  19.  * This will start actually deleting tickets, so, make sure its backing them up first.
  20.  */
  21. if(! php_sapi_name () == 'cli')
  22.     die('Cannot be run from browser.. ');
  23. $_SERVER ['REMOTE_ADDR'] = 'localhost';
  24. require_once ('main.inc.php');
  25. $thisstaff = new Staff(MY_USER_ID);
  26.  
  27. // select old messages.. we probably don't need any of them
  28. $select = 'SELECT ticket_id FROM ' . TICKET_TABLE . " WHERE status='closed' AND closed < DATE_SUB(NOW(), INTERVAL " . DELETEMONTHS . ' MONTH)';
  29.  
  30. $res = db_query ( $select );
  31. if ($res && $num =db_num_rows ( $res )) {
  32.     print "Found $num matching deletables!\n";
  33.     $messages = db_assoc_array ( $res, true );
  34.     if ($messages && !CRONFRIENDLY)
  35.         print "Deleting old Messages\n";
  36.    
  37.     foreach ( $messages as $ticket) {
  38.         if (! delete_ticket ( $ticket['ticket_id'] ))
  39.             print 'Failed to delete ticket_id: ' .  $ticket['ticket_id']  . "\n\n";
  40.     }
  41. } elseif (!CRONFRIENDLY){
  42.     print "Didn't find anything to delete.";
  43. }
  44.  
  45. /**
  46.  * Uses osTicket functions to remove a ticket correctly.
  47.  */
  48. function delete_ticket($id) {
  49.     $t = new Ticket ( $id );
  50.     print "Deleting old ticket $id:" . $t->getSubject () . "\n";
  51.     // save backup?
  52.     // If you don't want backups, you don't need a staff-id either.. odd, but there you go.
  53.     // Simply comment out this whole part, and uncomment the $t->delete(); line..
  54.     $pdf = new Ticket2PDF ( $t, 'A4', true );
  55.     $name = 'Ticket-' . $t->getExtId () . '.pdf';
  56.     $file = BACKUPDIR. $name;
  57.     $string = $pdf->Output ( $file, 'F' );
  58.     // return $t->delete();
  59.     return true;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement