Advertisement
Guest User

Удаление ежедневных бэкапов

a guest
Nov 12th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.99 KB | None | 0 0
  1. $week_offset = date("Y-m-d 12:00:00", time() - 60 * 60 * 24 * 7);
  2. $month_offset = date("Y-m-d 12:00:00", time() - 60 * 60 * 24 * 30);
  3. $three_month_offset = date("Y-m-d 12:00:00", time() - 60 * 60 * 24 * 90);
  4.  
  5. $table = get_table(1);
  6. $del_count = 0;
  7. $three_month_del_count = array();
  8. $backup_time_last = array();
  9.  
  10. $backup_count_all = array();
  11. $result_count_all = sql_query("SELECT * FROM " . DATA_TABLE . "1 WHERE f61 IN ('ежедневный', 'апргрейд') AND f321 NOT LIKE '%free%' Order by add_time desc");
  12. while ($row_count_all = sql_fetch_assoc($result_count_all)) {
  13.     if (!$backup_count_all[$row_count_all['f41']]) {
  14.         $backup_count_all[$row_count_all['f41']] = 1;
  15.     } else {
  16.         $backup_count_all[$row_count_all['f41']]++;
  17.     }
  18. }
  19.  
  20. $result = sql_query("SELECT * FROM " . DATA_TABLE . "1 WHERE f61 IN ('ежедневный', 'апргрейд') and add_time<'" . form_sql($week_offset) . "' AND f321 NOT LIKE '%free%' Order by add_time desc");
  21. while ($line = sql_fetch_assoc($result)) {
  22.     $delete = 'удалять';
  23.  
  24.     $backup_time = strtotime($line['add_time']);
  25.    
  26.     if (!$three_month_del_count[$line['f41']]) {
  27.         $three_month_del_count[$line['f41']] = 0;
  28.     }
  29.    
  30.     if (!$backups_last_month_count[$line['f41']]) {
  31.         $backups_last_month_count[$line['f41']] = 0;
  32.     }
  33.  
  34.     if ($backup_count_all[$line['f41']] <= 14) {
  35.         $delete = 'оставляем';
  36.     }
  37.  
  38.     if (date("w", $backup_time) == 1) {
  39.         // Оставляем бэкапы Последний месяц, раз в неделю 3 штуки
  40.         if ($backup_time > strtotime($month_offset) && $backups_last_month_count[$line['f41']]<3) {
  41.             $delete = 'оставляем';
  42.             $backups_last_month_count[$line['f41']]++;
  43.         } elseif (($backup_time > strtotime($three_month_offset)) && ($three_month_del_count[$line['f41']] < 8)) {
  44.             // Проверяем 8 так как в 2-х месяцах оставшихся 8 недель
  45.             // Оставляем бэкапы Последние 3 месяца, каждые 2 недели 4 штуки
  46.             $three_month_del_count[$line['f41']]++;
  47.             if ($backup_time_last[$line['f41']]) {
  48.                 if (($backup_time_last[$line['f41']] - $backup_time) > 864000) {
  49.                     $delete = 'оставляем';
  50.                 }
  51.             } // оставляем если промежуток между предыдущим больше 10 дней
  52.             if (($three_month_del_count[$line['f41']] % 2) == 0) {
  53.                 $delete = 'оставляем';
  54.             } // оставляем каждый второй
  55.         }
  56.     }
  57.  
  58.     $backup_time_last[$line['f41']] = $backup_time;
  59.  
  60.     // удалить
  61.     if ($delete == 'удалять') {
  62.         drop_files_by_line($table, $line);
  63.         sql_query("DELETE FROM " . DATA_TABLE . "1 WHERE id=" . intval($line['id']));
  64.         $del_count++;
  65.     }
  66. }
  67. echo "Deleted $del_count backups";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement