Current File : //scripts/comet_protected_item_maintenance
#!/usr/local/cpanel/3rdparty/bin/perl
# Copyright 2025 WebPros International, LLC
# All rights reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited.
=encoding utf-8
=head1 NAME
comet_protected_item_maintenance - Maintenance and retention operations for Comet Protected Items and deleted users
=head1 SYNOPSIS
comet_protected_item_maintenance [--status]
comet_protected_item_maintenance --list-users
comet_protected_item_maintenance --prune-days=90 [--user=USERNAME] [--users=USER1,USER2,...] [--dry-run]
comet_protected_item_maintenance --list-deleted-user-snapshots
comet_protected_item_maintenance --prune-user=USERNAME [--dry-run]
comet_protected_item_maintenance --prune-all-deleted-users [--dry-run]
comet_protected_item_maintenance --restore-deleted-user=USERNAME [--use-snapshot=SNAPSHOT_ID] [--dry-run]
=head1 DESCRIPTION
Performs maintenance on the consolidated metadata database for Comet Protected Items
and deleted cPanel users. Supports listing users, pruning terminated Protected Item
records, pruning deleted user snapshot records from backup run logs, enumerating
snapshots for deleted users, marking deleted users as pruned (stub), and simulating
a restore (stub implementation).
The metadata database tracks both active Protected Item mappings and retention
information for deleted users. Snapshot data is stored in the backup run logs database.
Actual snapshot and restore logic are pending integration with the backend Comet
service; current commands provide placeholders to exercise CLI flows.
=cut
package scripts::comet_protected_item_maintenance;
use cPstrict;
use Getopt::Long ();
use Pod::Usage ();
use FindBin;
use lib "$FindBin::Bin/../plugins/whm/comet/perl/usr/local/cpanel";
use Whostmgr::CometBackup::ProtectedItemMap ();
use Whostmgr::CometBackup::DeletedUsers ();
# Option variables
my $prune_days = 0;
my $list_users = 0;
my $help = 0;
my $status = 0; # explicit status request
my $prune_user = '';
my $prune_all_deleted_users = 0;
my $list_deleted_snapshots = 0;
my $restore_deleted_user = '';
my $use_snapshot = '';
my $dry_run = 0;
my $user = ''; # single user for --prune-days
my $users = ''; # comma-separated users for --prune-days
sub main {
# Plugin policy: nothing in this process should be world-readable.
umask 0027;
Getopt::Long::GetOptions(
'prune-days=i' => \$prune_days,
'list-users' => \$list_users,
'help|h' => \$help,
'status' => \$status,
'prune-user=s' => \$prune_user,
'prune-all-deleted-users' => \$prune_all_deleted_users,
'list-deleted-user-snapshots' => \$list_deleted_snapshots,
'restore-deleted-user=s' => \$restore_deleted_user,
'use-snapshot=s' => \$use_snapshot,
'dry-run' => \$dry_run,
'user=s' => \$user,
'users=s' => \$users,
) or Pod::Usage::pod2usage(2);
if ($help) {
Pod::Usage::pod2usage(1);
return;
}
my $map = Whostmgr::CometBackup::ProtectedItemMap->new();
my $deleted = Whostmgr::CometBackup::DeletedUsers->new();
# Dispatch precedence: prune specific user, prune all, restore, list snapshots, prune-days, list-users, status(default)
if ($prune_user) {
prune_single_deleted_user( $deleted, $prune_user, $dry_run );
return;
}
if ($prune_all_deleted_users) {
prune_all_deleted_users( $deleted, $dry_run );
return;
}
if ($restore_deleted_user) {
restore_deleted_user( $deleted, $restore_deleted_user, $use_snapshot, $dry_run );
return;
}
if ($list_deleted_snapshots) {
list_deleted_user_snapshots($deleted);
return;
}
if ($prune_days) {
prune_old_records( $map, $prune_days, $dry_run, $user, $users );
return;
}
if ($list_users) {
list_users_with_protected_items($map);
return;
}
# Default status view
show_status( $map, $deleted );
return;
}
sub show_status ( $map, $deleted ) {
print "Comet Protected Item / Deleted User Status\n";
print "========================================\n\n";
my @all_users = $map->get_all_users_with_protected_items();
my @active_users = $map->get_all_users_with_protected_items( active_only => 1 );
my @deleted_users = $deleted->list_deleted_users();
print "Total users with protected items: " . scalar(@all_users) . "\n";
print "Users with active protected items: " . scalar(@active_users) . "\n";
print "Users with terminated items only: " . ( scalar(@all_users) - scalar(@active_users) ) . "\n";
# XXX: Delete this "Users with protected items" part?
if (@all_users) {
print "\nUsers with protected items:\n";
for my $user (@all_users) {
my @items = $map->get_protected_items_for_user( system_user => $user );
my @active_items = grep { !$_->{user_terminated} } @items;
my @terminated_items = grep { $_->{user_terminated} } @items;
printf " %-20s: %d total (%d active, %d terminated)\n",
$user, scalar(@items), scalar(@active_items), scalar(@terminated_items);
}
}
print "Deleted users tracked: " . scalar(@deleted_users) . "\n";
print "\nUse --list-users for detailed information\n";
print "Use --prune-days=N to remove terminated Protected Item records\n";
print "Use --list-deleted-user-snapshots to view stub snapshots for deleted users\n";
print "Use --prune-user=USER or --prune-all-deleted-users to mark retained backups pruned (stub)\n";
print "Use --restore-deleted-user=USER [--use-snapshot=ID] to simulate restore (stub)\n";
return;
}
sub list_users_with_protected_items ($map) {
print "Users with Protected Items\n";
print "==========================\n\n";
my @all_users = $map->get_all_users_with_protected_items( active_only => 1 );
if ( !@all_users ) {
print "No users found with protected items.\n";
return;
}
for my $user (@all_users) {
print "User: $user\n";
print "-" x 40 . "\n";
my @items = $map->get_protected_items_for_user( system_user => $user );
for my $item (@items) {
my $user_status = $item->{user_terminated} ? 'Terminated' : 'Active';
my $item_status = $item->{status} || 'unknown';
my $terminated = $item->{terminated_timestamp} ? localtime( $item->{terminated_timestamp} ) : 'N/A';
print " Protected Item: $item->{protected_item_uid}\n";
print " User Status: $user_status\n";
print " Item Status: $item_status\n";
print " User UUID: $item->{cpanel_user_uuid}\n";
print " Terminated: $terminated\n";
print "\n";
}
}
return;
}
sub prune_old_records ( $map, $days, $dry_run = 0, $single_user = '', $user_list = '' ) {
my $cutoff_time = time() - ( $days * 24 * 60 * 60 );
# Build list of users to target
my @target_users;
if ($single_user) {
push @target_users, $single_user;
}
if ($user_list) {
push @target_users, split( /,/, $user_list );
}
if ($dry_run) {
if (@target_users) {
print "[DRY RUN] Would prune terminated records older than $days days for users: " . join( ', ', @target_users ) . "...\n";
}
else {
print "[DRY RUN] Would prune terminated records older than $days days...\n";
}
# Query what would be pruned from Protected Item map
my %prune_args = ( older_than_days => $days, dry_run => 1 );
$prune_args{users} = \@target_users if @target_users;
my $map_count = $map->prune_inactive_users(%prune_args);
print "[DRY RUN] Would prune $map_count old terminated Protected Item records.\n";
# Query what would be pruned from backup run logs (deleted user snapshots)
my $deleted = Whostmgr::CometBackup::DeletedUsers->new();
my %snapshot_args = ( cutoff_epoch => $cutoff_time, dry_run => 1 );
$snapshot_args{users} = \@target_users if @target_users;
my $result = $deleted->prune_deleted_user_snapshots_before(%snapshot_args);
if ( $result->{error} ) {
print "[DRY RUN] Error checking deleted user snapshots: $result->{error}\n";
}
else {
my $snapshot_count = $result->{count};
my @affected_users = @{ $result->{users_affected} || [] };
print "[DRY RUN] Would prune $snapshot_count deleted user snapshot records";
if (@affected_users) {
print " for users: " . join( ', ', @affected_users );
}
print ".\n";
}
my $total = $map_count + ( $result->{count} || 0 );
print "[DRY RUN] Total records that would be pruned: $total\n";
}
else {
if (@target_users) {
print "Pruning terminated records older than $days days for users: " . join( ', ', @target_users ) . "...\n";
}
else {
print "Pruning terminated records older than $days days...\n";
}
# Prune terminated Protected Item records
my %prune_args = ( older_than_days => $days );
$prune_args{users} = \@target_users if @target_users;
my $map_count = $map->prune_inactive_users(%prune_args);
print "Pruned $map_count old terminated Protected Item records.\n";
# Prune deleted user snapshots from backup run logs
my $deleted = Whostmgr::CometBackup::DeletedUsers->new();
my %snapshot_args = ( cutoff_epoch => $cutoff_time, dry_run => 0 );
$snapshot_args{users} = \@target_users if @target_users;
my $result = $deleted->prune_deleted_user_snapshots_before(%snapshot_args);
if ( $result->{error} ) {
print "Error pruning deleted user snapshots: $result->{error}\n";
}
else {
my $snapshot_count = $result->{count};
my @affected_users = @{ $result->{users_affected} || [] };
print "Pruned $snapshot_count deleted user snapshot records";
if (@affected_users) {
print " for users: " . join( ', ', @affected_users );
}
print ".\n";
}
my $total = $map_count + ( $result->{count} || 0 );
print "Total records pruned: $total\n";
}
}
sub list_deleted_user_snapshots ($deleted) {
my @users = $deleted->list_deleted_users();
if ( !@users ) {
print "No deleted users to list snapshots for.\n";
return;
}
# Filter to only include users that don't exist locally anymore
my @truly_deleted_users = grep {
my $username = $_->{system_user};
!getpwnam($username); # Only include if user doesn't exist
} @users;
if ( !@truly_deleted_users ) {
print "No deleted users to list snapshots for.\n";
print "(All tracked deleted users still exist in the system)\n";
return;
}
print "Deleted User Snapshots\n";
print "======================\n\n";
for my $user_info (@truly_deleted_users) {
my $username = $user_info->{system_user};
my $result = $deleted->list_deleted_user_snapshots($username);
if ( $result->{error} ) {
print "Error for user $username: $result->{error}\n\n";
next;
}
print "User: $username\n";
print "-" x ( 6 + length($username) ) . "\n";
if ( $result->{count} == 0 ) {
print " No snapshots available\n\n";
next;
}
for my $snap ( @{ $result->{snapshots} } ) {
my $created = localtime( $snap->{created_at} // time() );
my $size = $snap->{size} // 'Unknown';
my $type = $snap->{type} // 'Unknown';
my $status = $snap->{status} // 'Unknown';
my $source_id = $snap->{source_id} // $snap->{protected_item_id} // 'Unknown';
my $destination_id = $snap->{destination_id} // 'Unknown';
my $snapshot_id = $snap->{snapshot_uuid} // $snap->{snapshot_id} // 'Unknown';
print " Snapshot:\n";
print " Source ID: $source_id\n";
print " Destination ID: $destination_id\n";
print " Snapshot ID: $snapshot_id\n";
print " Type: $type\n";
print " Created: $created\n";
print " Size: $size bytes\n";
print " Status: $status\n";
print "\n";
}
print "\n";
}
return;
}
sub prune_single_deleted_user ( $deleted, $user, $dry_run = 0 ) {
if ($dry_run) {
print "[DRY RUN] Would prune deleted user '$user'...\n";
my $result = $deleted->prune_single_deleted_user( $user, dry_run => 1 );
if ( $result->{error} ) {
print "[DRY RUN] Error: $result->{error}\n";
return;
}
print "[DRY RUN] Would succeed: $result->{message}\n";
}
else {
print "Pruning deleted user '$user'...\n";
my $result = $deleted->prune_single_deleted_user($user);
if ( $result->{error} ) {
print "Error: $result->{error}\n";
return;
}
print "Success: $result->{message}\n";
}
return;
}
sub prune_all_deleted_users ( $deleted, $dry_run = 0 ) {
my @users = $deleted->list_deleted_users();
if ( !@users ) {
print "No deleted users to prune.\n";
return;
}
if ($dry_run) {
print "[DRY RUN] Would prune all " . scalar(@users) . " deleted users...\n";
}
else {
print "Pruning all " . scalar(@users) . " deleted users...\n";
}
my $success_count = 0;
my $error_count = 0;
for my $user_info (@users) {
my $username = $user_info->{system_user};
if ($dry_run) {
print "[DRY RUN] Would prune user '$username'... ";
my $result = $deleted->prune_single_deleted_user( $username, dry_run => 1 );
if ( $result->{error} ) {
print "Error: $result->{error}\n";
$error_count++;
}
else {
print "Success\n";
$success_count++;
}
}
else {
print "Pruning user '$username'... ";
my $result = $deleted->prune_single_deleted_user($username);
if ( $result->{error} ) {
print "Error: $result->{error}\n";
$error_count++;
}
else {
print "Success\n";
$success_count++;
}
}
}
if ($dry_run) {
print "\n[DRY RUN] Pruning complete: $success_count would succeed, $error_count errors\n";
}
else {
print "\nPruning complete: $success_count successful, $error_count errors\n";
}
return;
}
sub restore_deleted_user ( $deleted, $user, $snapshot, $dry_run = 0 ) {
if ( !$snapshot ) {
# Auto-select latest snapshot if not specified
my $snapshots_result = $deleted->list_deleted_user_snapshots($user);
if ( $snapshots_result->{error} ) {
print "Error: Failed to list snapshots for user '$user': $snapshots_result->{error}\n";
return;
}
if ( !$snapshots_result->{snapshots} || @{ $snapshots_result->{snapshots} } == 0 ) {
print "Error: No snapshots available for user '$user'\n";
return;
}
# Sort snapshots by created_at descending to get the latest one
my @sorted_snapshots = sort { ( $b->{created_at} // 0 ) <=> ( $a->{created_at} // 0 ) } @{ $snapshots_result->{snapshots} };
# Use the latest snapshot (first after sorting)
$snapshot = $sorted_snapshots[0]->{snapshot_uuid} // $sorted_snapshots[0]->{snapshot_id};
if ( !$snapshot ) {
print "Error: Could not determine snapshot identifier for user '$user'\n";
return;
}
print "Using latest snapshot: $snapshot\n";
}
if ($dry_run) {
print "[DRY RUN] Would restore deleted user '$user' from snapshot '$snapshot'...\n";
my $result = $deleted->restore_deleted_user( $user, $snapshot, dry_run => 1 );
if ( $result->{error} ) {
print "[DRY RUN] Error: $result->{error}\n";
return;
}
print "[DRY RUN] Would succeed: $result->{message}\n";
if ( $result->{job_uuid} ) {
print "[DRY RUN] Would create restore job UUID: $result->{job_uuid}\n";
}
}
else {
print "Restoring deleted user '$user' from snapshot '$snapshot'...\n";
my $result = $deleted->restore_deleted_user( $user, $snapshot );
if ( $result->{error} ) {
print "Error: $result->{error}\n";
return;
}
print "Success: $result->{message}\n";
if ( $result->{job_uuid} ) {
print "Restore job UUID: $result->{job_uuid}\n";
}
}
return;
}
# Run as script if not required as module
main() if !caller();
1;
__END__
=head1 OPTIONS
=over 4
=item B<--status>
Show aggregate status (default when no other option is provided).
=item B<--list-users>
List all users with their Protected Items and activity status.
=item B<--prune-days=N>
Remove terminated Protected Item records and deleted user snapshot records older
than N days. This prunes both the Protected Item map entries for terminated users
and the actual snapshot records from the backup run logs database.
=item B<--user=USERNAME>
Limit --prune-days to a specific user. Can be used multiple times or combined with
--users to target multiple users.
=item B<--users=USER1,USER2,...>
Limit --prune-days to a comma-separated list of users. Can be combined with --user
to target additional users.
=item B<--list-deleted-user-snapshots>
List stub snapshot identifiers for all deleted users.
=item B<--prune-user=USERNAME>
Mark retained backups for a specific deleted user as pruned (stub implementation).
=item B<--prune-all-deleted-users>
Mark retained backups for all deleted users as pruned (stub implementation).
=item B<--restore-deleted-user=USERNAME>
Simulate a restore of a deleted user (stub). If --use-snapshot is not provided,
the most recent snapshot will be automatically selected based on creation timestamp.
=item B<--use-snapshot=SNAPSHOT_ID>
Specify snapshot identifier for simulated restore (used with --restore-deleted-user).
If not specified, the most recent snapshot will be used automatically.
=item B<--dry-run>
Perform a dry run without making any actual changes. Shows what would be done
without modifying the database or performing actual operations. Can be used with
--prune-days, --prune-user, --prune-all-deleted-users, and --restore-deleted-user.
=item B<--help>
Show help message.
=back
=head1 EXAMPLES
Show current status:
comet_protected_item_maintenance --status
List all users and their protected items:
comet_protected_item_maintenance --list-users
Remove terminated records older than 90 days:
comet_protected_item_maintenance --prune-days=90
Remove terminated records for a specific user:
comet_protected_item_maintenance --prune-days=90 --user=alice
Remove terminated records for multiple users:
comet_protected_item_maintenance --prune-days=90 --users=alice,bob,charlie
List deleted user snapshots (stub):
comet_protected_item_maintenance --list-deleted-user-snapshots
Prune a single deleted user:
comet_protected_item_maintenance --prune-user=alice
Restore a deleted user using the most recent snapshot (stub):
comet_protected_item_maintenance --restore-deleted-user=alice
Restore a deleted user using a specific snapshot (stub):
comet_protected_item_maintenance --restore-deleted-user=alice --use-snapshot=alice-snapA
Perform a dry run to see what would be pruned without making changes:
comet_protected_item_maintenance --prune-days=90 --dry-run
comet_protected_item_maintenance --prune-user=alice --dry-run
comet_protected_item_maintenance --prune-all-deleted-users --dry-run
comet_protected_item_maintenance --restore-deleted-user=alice --dry-run
=head1 FILES
=over 4
=item F</var/cpanel/comet/usermap.db>
SQLite database containing Protected Item mappings and deleted user retention metadata.
=item F</var/cpanel/comet/backup_run_logs.db>
SQLite database containing backup run logs and snapshot records for all users,
including deleted users. Pruned by the --prune-days option.
=back
=cut
Rosenblum TV: Video training, virtual workshops, classes, tutorials
RE-INVENTING THE TELEVISION NEWS BUSINESS*
A revolution in video storytelling
Creating entirely new & cost-effective production methods
From the world leaders in video production training and the creators of Character Driven Storytelling™
*and every other business that uses video
WHAT WE DO
Over the past 35 years, we have designed, built or restructured some of the most powerful news and journalism companies in the world.
We replace the traditional TV news ‘crew’ with one highly trained journalist, working alone with nothing but an iPhone.
No more TV news ‘crews’, no editors and no field producers.
This is television news done the way newspaper journalism is done – one reporter with their electronic pad and pencil.
In doing this, we can cut the cost of production by as much as 75% while increasing ratings and audience engagement.
In the place of conventional TV news ‘packages’ – ie, reporter stand up, interview, b-roll, man on the street, we marry great journalism with Netflix and Hollywood storytelling.
It’s a combination that works.
We have taken most of our clients to #1 in their respective markets.
And it’s not just for news. Any company, any profit, any NGO and anyone else who is online needs to tell their story in compelling yet cost-effective video. We can teach you to do that. Either in person or virtually.
EXAMPLES OF WHAT WE CAN TEACH YOUR STAFF TO PRODUCE
ITAY HOD
Itay Hod, MMJ with KPIX/CBS in San Francisco, took the 5-Day Intensive Video Storytelling Bootcamp in 2018.
Because he works alone, with only an iPhone, he was able to embed himself with a homeless family.
Here’s the story he produced in a one-day turn.
KIET DO
Kiet Do, an MMJ with KPIX/CBS in San Francisco, took the 5-Day Intensive Video Storytelling Bootcamp in 2021.
Here is a story he produced, all on his own, with only an iPhone and in a one-day turn.
TAYLOR SCHAUB
Taylor Schaub, an MMJ with Spectrum News 1 in LA, took the 5-Day Intensive Video Storytelling Bootcamp in 2023.
Here is a story he turned in only one day, using only an iPhone. It was the first video story he ever did and it was nominated for an Emmy.
THE BOOTCAMP
How do we convert stations and whole networks to working in this way?
Since 1988, we have run intensive 5-Day Video Storytelling Bootcamps
We have done these all over the world.
These are hands-on bootcamps, and participants learn an entirely new way of creating TV news stories.
-We shoot at a 3:1 ratio or lower, so turnaround times are fast.
-We go directly from camera to timelilne and edit – no written scripts. We work in the medium of pictures and sound.
-We are entirely character-driven.
-We are driven by pictures and real events.
-We are focused almost entirely on ’the viewer experience’.
Since 1988, more than 70,000 journalists around the world have taken our bootcamps, either in person on virtualy.
We have started to work with CBS News, bringing our ideas of character-driven storytelling to one of the most successful and biggest networks in the United States. Since beginning to work with them ratings have climbed and more importantly, audience engagement is through the ceiling.
We started New York Times Television in 1990 and it was the first paper to be brought into the world of TV. It quickly became one of the most successful non-fiction production companies in the United States. The series and documentaries we produced won many awards including multiple Emmys.
We have been working with the BBC since the year 2000 helping to convert their national news network to our visual storytelling technique. Most recently we have trained teams from their sports, documentaries, and comedy divisions to make character-driven stories using only smartphones.
For the past five years we have worked with Spectrum News to introduce and train their journalists on visual, character driven storytelling using smartphones helping to create a different kind of local news for their network of 24-Hour News Stations across the United States.
In 2006, we were approached by the United Nations. Rather than rely on news outlets, it would be much easier to train the field operatives to produce their own stories. We spent two years working with the UN, training more than 100 of their staff in bootcamps in Geneva and Nairobi.
We trained 50 print reporters at the paper to shoot and tell their own stories, in conjunction with their print work. We built a TV newsroom in their existing print newsroom – you could not ask for a better set and they began to live stream their stories in conjunction with their print work.
We spent two years with McGraw Hill, training more than 150 of their staffers, making them completely video literate. McGraw/Hill media properties we transit included Business Week, Aviation Week, (what was the name of the architecture magazine), and JD Power and Associates.
In 1990, we were approached by The Voice of America, the official broadcasting agency for the United States Government. When we met with VOA, they were only a short wave radio broadcaster, but working with them, we took them into television, launching VOA-TV.
British based Oyster Yachts makes some of the finest yachts in the world. Like every other company, they had to find a way to feed the never-ending video demands of social media – sites like Instagram and TikTok. We trained the Oyster staff to tell their own stories, using only iPhones.
We were approached by SEPA, the Scottish Environmental Protection Agency because they had to continually find a way to ‘feed the media beast’. The result was that SEPA was able to tell their own stories, whenever they wanted, and at almost no additional cost.
Michael Rosenblum has been writing about the media since 1988. His work and ideas have appeared in The Guardian, The Huffington Post, Ilkeston Life and many other publications.
He has been blogging regularly for the past 35 years on this subject. Having taught media studies at Columbia University, NYU and now the University of Oxford, he is considered an expert on this subject.
Continue reading this post or look back at previous posts.