Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello, I just finished my first perl script. I am having a hard time understanding a lot of the code, but it works so that's great. I am making a btrfs snapshot manager, this script is going to be just one component of it. Anyways I just want to improve my skills and would like some feedback.

#!/usr/bin/env perl use strict; use warnings; use 5.010; =pod =head1 DESCRIPTION This script is used for taking and deleting a single snapshot. Snapshot names look something like this: 'day=01_07_2021,time=15:30'. Exactly six arguments must be given: 1: subvolume to be snapshotted 2: root snapshot directory (typically /.snapshots) 3: the yabsm name of the subvolume being snapshotted (ex: 'root' for ' +/') 4: the timeframe for the snapshot (hourly, daily, etc). 5: the desired date format. Either 'mm/dd/yyyy' or 'dd/mm/yyyy' 6: the number of snapshots the user wants to keep for this subvolume/t +imeframe. This script should not be used by the end user. =cut #################################### # GRAB INPUT PARAMETERS # #################################### my $subvol_to_snapshot_arg = $ARGV[0]; my $snap_root_dir_arg = $ARGV[1]; my $yabsm_subvol_name_arg = $ARGV[2]; my $timeframe_arg = $ARGV[3]; my $date_format_arg = $ARGV[4]; my $snaps_to_keep_arg = $ARGV[5]; #################################### # MAIN # #################################### delete_earliest_snapshot(); take_new_snapshot();
#################################### # SNAPSHOT CREATION # #################################### sub take_new_snapshot { my $snap_name = create_snapshot_name(); system('btrfs subvolume snapshot ' . $subvol_to_snapshot_arg . ' ' . $snap_root_dir_arg . '/' . $yabsm_subvol_name_arg . '/' . $timeframe_arg . '/' . $snap_name); } sub create_snapshot_name { my ($min, $hr, $day, $mon, $yr) = (localtime())[1..5]; $mon++; # month count starts at zero. $yr += 1900; # year represents years since 1900. if ($date_format_arg eq 'mm/dd/yyyy') { return 'day='.pad($mon).'_'.pad($day).'_'.$yr.',time='.pad($hr).':'.pad +($min); } elsif ($date_format_arg eq 'dd/mm/yyyy') { return 'day='.pad($day).'_'.pad($mon).'_'.$yr.',time='.pad($hr).':'.pad +($min); } else { die "$date_format_arg is not a valid date format"; } } sub pad { if ($_[0] < 10) { return '0' . $_[0]; } else { return $_[0]; } } #################################### # SNAPSHOT DELETION # #################################### sub delete_earliest_snapshot { opendir(DIR,"${snap_root_dir_arg}/${yabsm_subvol_name_arg}/$timefram +e_arg"); my @snaps = grep(/^[^\.]/, readdir DIR); # exclude dot files closedir DIR; return if scalar(@snaps) < $snaps_to_keep_arg; my $earliest_snap = earliest_snapshot(\@snaps); system('btrfs subvolume delete ' . $snap_root_dir_arg . '/' . $yabsm_subvol_name_arg . '/' . $timeframe_arg . '/' . $earliest_snap); } sub earliest_snapshot { my @snaps = @{$_[0]}; my $earliest_snap = $snaps[0]; foreach my $snap (@snaps) { $earliest_snap = $snap if snapshot_lt($snap,$earliest_snap); } return $earliest_snap; } sub snapshot_lt { return lexically_lt(snap_name_to_lex_ord_nums($_[0]), snap_name_to_lex_ord_nums($_[1])); } sub lexically_lt { my ($head1, @tail1) = @{$_[0]}; my ($head2, @tail2) = @{$_[1]}; if ($head1 > $head2) { return 0; } elsif ($head1 < $head2) { return 1; } elsif (@tail1 == 0) { return 1; } elsif (@tail2 == 0) { return 0; } else { return lexically_lt(\@tail1,\@tail2); } } sub snap_name_to_lex_ord_nums { my ($yr, $mon, $day, $hr, $min); if ($date_format_arg eq 'mm/dd/yyyy') { ($mon,$day,$yr,$hr,$min) = snap_name_nums($_[0]); return [$yr,$mon,$day,$hr,$min]; } elsif ($date_format_arg eq 'dd/mm/yyyy') { ($day,$mon,$yr,$hr,$min) = snap_name_nums($_[0]); return [$yr,$mon,$day,$hr,$min]; } else { die "$date_format_arg is not a valid date format"; } } sub snap_name_nums { return $_[0] =~ m/([0-9]+)/g; }

In reply to looking for feedback on my first script by thirtySeven

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2021-10-18 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (72 votes). Check out past polls.

    Notices?