Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
use v6;
use fatal;
sub scan-drive-ids(--> Set) {
my Set $ret;
for '/dev/disk/by-id/'.IO.dir.grep(!*.IO.basename.match(/'part' \d+ $/)) {
$ret = .basename.Str;
CATCH { default { warn .message } }
}
$ret
}
sub reset-usb-hub(--> True) {
state $reset-attempt = 0;
if $reset-attempt++ {
say already reset, doing nothing;
$reset-attempt = 0;
} else {
say Resetting usb hub.;
my $usb_modeswitch = run <usb_modeswitch -v 0x2109 -p 0x0813 --reset-usb>;
fail resetting usb hub failed unless $usb_modeswitch;
}
}
sub backup-root-and-home-to-disk(|) { ... }
sub backup-home-to-usb(|) { ... }
my %last-seen := scan-drive-ids;
my %usb-backup-set = Set(<usb-SanDisk_Ultra_USB_3.0_4C530001160708110455-0:0 usb-SanDisk_Ultra_USB_3.0_4C530001190708111070-0:0 usb-SanDisk_Ultra_USB_3.0_4C530001220708110370-0:0 usb-SanDisk_Ultra_USB_3.0_4C530001280708111064-0:0>);
my %root-backup-disk = Set(<ata-TOSHIBA_DT01ACA200_8443D04GS>);
my $delayed-check := Channel.new;
my Promise $timeout-promise;
react {
whenever IO::Notification.watch-path('/dev/disk/by-id/') {
my %just-seen := scan-drive-ids;
my %new-drives := %just-seen %last-seen;
my %old-drives := %last-seen %just-seen;
my %removed-drives := %last-seen %just-seen;
%last-seen := %just-seen;
# say ‚old drives: ‘, %old-drives.keys.sort;
say new drives: , %new-drives.keys.sort || '';
say removed drives: , %removed-drives.keys.sort || '';
if %usb-backup-set %new-drives {
$timeout-promise = Promise.in(5).then: {
$delayed-check.send: True;
$timeout-promise = Nil;
} without $timeout-promise;
}
if %root-backup-disk %new-drives {
sleep 2;
backup-root-and-home-to-disk(%root-backup-disk);
}
say '';
}
whenever $delayed-check {
my %just-seen := scan-drive-ids;
if %usb-backup-set %just-seen {
backup-home-to-usb(%usb-backup-set);
} elsif %usb-backup-set %just-seen {
warn 'drive missing in usb set: ' ~ (%usb-backup-set (%usb-backup-set %just-seen)).keys;
reset-usb-hub;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment