/backup-manager-purge

http://github.com/sukria/Backup-Manager · Perl · 337 lines · 160 code · 42 blank · 135 comment · 20 complexity · 0f0af2429fa7bb24e4ee3c4495fc6afd MD5 · raw file

  1. #!/usr/bin/perl
  2. # Copyright Š 2005-2016 The Backup Manager Authors
  3. #
  4. # See the AUTHORS file for details.
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. use strict;
  20. use warnings;
  21. =pod
  22. =head1 NAME
  23. backup-manager-purge - backup-manager's wrapper for outdating files
  24. =head1 SYNOPSIS
  25. backup-manager-purge [TTL] <options>
  26. =head1 DESCRIPTION
  27. B<backup-manager-purge> is the only authorized entity that can say if an archive
  28. should be purged or not. Any tasks used by backup-manager may have to know if
  29. an archive is deprecated (eg: the purging phase of an upload method). This tool
  30. is here to fulfill that need.
  31. Given a I<time to live (TTL)> and a list of archives, B<backup-manager-purge>
  32. will return another list of archives, corresponding to the ones that are
  33. outdated by the TTL.
  34. =head1 REQUIRED ARGS
  35. =over 4
  36. =item B<--ttl=>I<time-to-live>
  37. Specify the time to live (in days) for the archives. Any archive that is older
  38. than I<ttl> days will be outdated.
  39. =back
  40. =head1 OPTIONAL ARGS
  41. =over 4
  42. =item B<--files-from=>file
  43. A file containing a list of archives to parse, one archive per line.
  44. If this option is not used, STDIN will be used for catching the files to parse.
  45. =back
  46. =head1 RETURN
  47. B<backup-manager-purge> will return the list of outdated files on STDOUT, one
  48. file per line.
  49. =head1 ERROR CODES
  50. If an error occurs, it will print the error message on stderr and will exit with
  51. an error code greater than 0.
  52. Here are the possible error codes:
  53. =over 4
  54. =item bad command line (wrong arguments) : 10
  55. =item internal error (should be reported as a bug) : 20
  56. =back
  57. =head1 SEE ALSO
  58. backup-manager(8) backup-manager-upload(8)
  59. =head1 AUTHORS
  60. Concept and design by Alexis Sukrieh and Jan Metzger.
  61. =cut
  62. ##############################################################
  63. # Uses
  64. ##############################################################
  65. use BackupManager::Config;
  66. use BackupManager::Logger;
  67. use BackupManager::Dialog;
  68. use POSIX qw(strftime);
  69. use File::Basename;
  70. use Data::Dumper;
  71. ##############################################################
  72. # Constants
  73. ##############################################################
  74. use constant E_SUCCESS => 0;
  75. use constant E_INVALID => 10;
  76. use constant E_INTERNAL => 20;
  77. use constant TRUE => 1;
  78. use constant FALSE => 0;
  79. use constant DIALOG_VERBOSE => 0;
  80. use constant MSG_INTERNAL => "Internal system error, please report the bug.";
  81. ##############################################################
  82. # Global variables
  83. ##############################################################
  84. my $g_ttl = undef;
  85. my $g_filelist = undef;
  86. my @g_archives = ();
  87. my @g_outdated = ();
  88. my $g_fh = *STDIN;
  89. my $g_rh_archives = {};
  90. ##############################################################
  91. # Command line parsing
  92. ##############################################################
  93. BackupManager::Config::getopt("$0 -ttl=<TTL> --files-from=<FILE>\n
  94. --ttl|-t: the time to live for outdating files
  95. --files-from|-f: a file that contains the list of archives to process",
  96. 'ttl|t=s' => \$g_ttl,
  97. 'files-from|f=s' => \$g_filelist,
  98. );
  99. ##############################################################
  100. # Subs
  101. ##############################################################
  102. # Takes an archive an returns all meta-data contained in its name
  103. sub parse_archive ($)
  104. {
  105. my ($archive) = @_;
  106. unless (defined $archive) {
  107. print_error MSG_INTERNAL;
  108. exit E_INTERNAL;
  109. }
  110. my ($prefix, $name, $date, $master, $filetype);
  111. $archive = basename ($archive);
  112. if ($archive =~ m/^\s*($ENV{BM_ARCHIVE_PREFIX})-?(\S+)?\.?(\d{8})\.(master\.)?(\S+)\s*$/) {
  113. ($prefix, $name, $date, $master, $filetype) = ($1, $2, $3, $4, $5);
  114. $master = $master ? 1 : 0;
  115. $name = "$prefix-md5" if $filetype eq 'md5' and not $name;
  116. }
  117. # The archive pattern
  118. elsif ($archive =~ /^\s*([^-]+)-(\S+)\.(\d{8})\.(\S+)\s*$/) {
  119. $prefix = $1;
  120. $name = $2;
  121. $date = $3;
  122. my $suffix = $4;
  123. if ($suffix =~ /master\.(\S+)/) {
  124. $master = 1;
  125. $filetype = $1;
  126. }
  127. elsif ($suffix =~ /\.?(.+)/) {
  128. $master = 0;
  129. $filetype = $1;
  130. }
  131. }
  132. # The md5 file pattern
  133. elsif ($archive =~ /^\s*([^-]+)-(\d{8})\.md5\s*$/) {
  134. $prefix = $1;
  135. $name = "$prefix-md5";
  136. $date = $2;
  137. $filetype = "md5";
  138. $master = 0;
  139. }
  140. # Unknown pattern
  141. else {
  142. return undef;
  143. }
  144. return { prefix => $prefix,
  145. name => $name,
  146. date => $date,
  147. master => $master,
  148. filetype => $filetype};
  149. }
  150. # Takes a file handle and an array ref, parse the file's content
  151. # and store in the array exiting filenames.
  152. sub read_archives($$)
  153. {
  154. my ($ra_archives, $fh) = @_;
  155. my $archive = "";
  156. while (<$fh>) {
  157. chomp();
  158. if (/^\s*(\S+)\s*$/) {
  159. $archive = $1;
  160. }
  161. my $rh_data = parse_archive ($archive);
  162. next unless defined $rh_data;
  163. next unless defined $rh_data->{date};
  164. if ($rh_data->{master}) {
  165. $g_rh_archives->{$rh_data->{name}}{pathByDateMasters}{$rh_data->{date}} = $archive;
  166. }
  167. $g_rh_archives->{$rh_data->{name}}{pathByDate}{$rh_data->{date}} = $archive;
  168. $g_rh_archives->{dataByPath}{$archive} = $rh_data;
  169. push @{$ra_archives}, $archive;
  170. }
  171. }
  172. # Takes two array refs. Reads from the first one the list of archives
  173. # to process, and push in the second one the outdated archives.
  174. sub outdate_archives($$)
  175. {
  176. my ($ra_archives, $ra_outdated) = @_;
  177. unless (defined $ra_archives and
  178. defined $ra_outdated) {
  179. exit E_INTERNAL;
  180. }
  181. my $purge_date = strftime ('%Y%m%d',
  182. localtime(time() - $g_ttl * 24 * 3600));
  183. print_info "Outdating archives made before $purge_date";
  184. my %outdated = (); # set of outdated archives. Will be converted to a list
  185. # at the end of this function
  186. my %seen = ();
  187. my $outdate_master_notmaster = sub
  188. {
  189. my $do_master = shift;
  190. ARCHIVE_LOOP:
  191. foreach my $archive (sort @{$ra_archives}) {
  192. my $data = $g_rh_archives->{dataByPath}{$archive};
  193. next unless defined $data;
  194. next unless defined $data->{date};
  195. # if the date of the archive is older than $purge_date, we may have to outdate it
  196. # unless, nothing to do for that archive.
  197. next if ($data->{date} > $purge_date);
  198. # We can outdate a master only if a younger master exists
  199. if ($data->{master} && $do_master) {
  200. next if $seen{$archive};
  201. $seen{$archive} = 1;
  202. my $pathByDateMasters = $g_rh_archives->{$data->{name}}{pathByDateMasters};
  203. foreach my $master_date ( keys %$pathByDateMasters) {
  204. if ($master_date > $data->{date}) {
  205. $outdated{$archive} = 1;
  206. last;
  207. }
  208. }
  209. }
  210. # here the archive is deprecated, its date is < to $purge_date
  211. if (!$data->{master} && !$do_master) {
  212. next if $seen{$archive};
  213. $seen{$archive} = 1;
  214. # An incremental archive should not be deleted if its master is
  215. # still around. At this point I looked through all the masters
  216. # and I know which ones I'm keeping. Any archive younger then
  217. # the oldest master is kept
  218. my $pathByDateMasters = $g_rh_archives->{$data->{name}}{pathByDateMasters};
  219. foreach my $master_date ( keys %$pathByDateMasters) {
  220. if ($master_date < $data->{date} && !$outdated{$pathByDateMasters->{$master_date}}) {
  221. # I found an older master that I decided to keep. This
  222. # archive should thus be kept as well
  223. next ARCHIVE_LOOP;
  224. }
  225. }
  226. # if BM_ARCHIVE_STRICTPURGE is true, we can only purge
  227. # an archive prefixed with BM_ARCHIVE_PREFIX
  228. next if (($ENV{BM_ARCHIVE_STRICTPURGE} eq "true") and
  229. ($data->{prefix} ne $ENV{BM_ARCHIVE_PREFIX}));
  230. # now, we're sure we can outdate the archive
  231. $outdated{$archive} = 1;
  232. }
  233. }
  234. };
  235. $outdate_master_notmaster->(1); # masters first
  236. $outdate_master_notmaster->(0); # then the others
  237. push @{$ra_outdated}, sort keys %outdated;
  238. }
  239. ##############################################################
  240. # Main
  241. ##############################################################
  242. # Init
  243. init_dialog (DIALOG_VERBOSE);
  244. # Args check
  245. unless (defined $g_ttl) {
  246. print_error "No TTL given";
  247. exit E_INVALID;
  248. }
  249. # In
  250. if (defined $g_filelist and -f $g_filelist) {
  251. print_info "Reading archives from $g_filelist";
  252. open $g_fh, $g_filelist or die "Unable to open $g_filelist";
  253. }
  254. else {
  255. print_info "Reading archives from STDIN";
  256. }
  257. read_archives (\@g_archives, $g_fh);
  258. # Process
  259. outdate_archives (\@g_archives, \@g_outdated);
  260. # Out
  261. foreach my $archive (@g_outdated) {
  262. print "$archive\n";
  263. }
  264. exit E_SUCCESS;