TERMINAL EXPLOIT V2.1

[LOCATION]: /proc/2620982/root/scripts/

Folder Link Grabber

PREFIX: SUFFIX:

Mass File Creator

FILENAME: CONTENT:

Quick Actions

FILE:
NEW_ITEM:
#!/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.

package scripts::backup_jobs_helper;

use cPstrict;

use Getopt::Long qw(GetOptions);
use Pod::Usage   qw(pod2usage);
use Time::Piece  ();
use Cpanel::Logger;
use Cpanel::Debug ();

use Whostmgr::CometBackup::BackupJobs ();
use Whostmgr::CometBackup::Scheduling ();
use Whostmgr::CometBackup::Constants  ();
use Data::UUID                        ();
use POSIX                             ();

my $logger = Cpanel::Logger->new();

=encoding utf-8

=head1 NAME

backup_jobs_helper - Process scheduled backup jobs for Comet Backup integration

=head1 SYNOPSIS

backup_jobs_helper [options]

=head1 DESCRIPTION

This script processes scheduled backup jobs by checking which jobs are due to run
and executing them. It is designed to be called periodically by cron.

The script performs the following operations:

=over

=item * Retrieves all active backup jobs from the database

=item * Checks which jobs are scheduled to run at the current time

=item * Executes any due backup jobs

=item * Updates job status and last run timestamps

=back

=head1 OPTIONS

=over

=item --help

Show this help message and exit.

=item --verbose

Enable verbose logging output.

=item --debug

Enable debug logging output (shows detailed scheduling calculations).

=item --dry-run

Show what would be done without actually executing backup jobs.

=back

=head1 EXAMPLES

    # Process backup jobs normally
    /usr/local/cpanel/scripts/backup_jobs_helper

    # Show what would be done without executing
    /usr/local/cpanel/scripts/backup_jobs_helper --dry-run

    # Enable verbose output
    /usr/local/cpanel/scripts/backup_jobs_helper --verbose

    # Enable debug output
    /usr/local/cpanel/scripts/backup_jobs_helper --debug

=cut

sub main {
    my ( $help, $verbose, $dry_run, $debug );

    GetOptions(
        'help|h'    => \$help,
        'verbose|v' => \$verbose,
        'dry-run'   => \$dry_run,
        'debug|d'   => \$debug,
    ) or pod2usage(2);

    pod2usage(1) if $help;

    # Enable debug logging if --debug flag is set
    if ($debug) {
        $Cpanel::Debug::level = 1;
    }

    my $current_time = time();
    my $current_dt   = Time::Piece->new($current_time);

    if ($verbose) {
        $logger->info( "Starting backup jobs helper at " . $current_dt->strftime('%Y-%m-%d %H:%M:%S') );
    }

    eval {
        my $jobs_manager = Whostmgr::CometBackup::BackupJobs->new();
        my $scheduler    = Whostmgr::CometBackup::Scheduling->new();

        # Get all active scheduled backup jobs
        my @active_jobs = $jobs_manager->get_backup_jobs();

        if ($verbose) {
            $logger->info( "Found " . scalar(@active_jobs) . " active backup jobs" );
        }

        my $jobs_processed = 0;

        for my $job (@active_jobs) {

            $logger->debug( "LOOKING AT JOB: " . $job->{job_id} . " ( " . $job->{description} . " | $job->{schedule_type} )" );

            # Skip manual jobs - they are not scheduled
            next if $job->{schedule_type} eq 'manual';

            # Skip if job is currently running
            next if $job->{status} && $job->{status} eq 'running';

            # Check if this job should run now
            my $should_run = _should_job_run_now( $job, $scheduler, $current_time );

            if ($should_run) {
                if ($verbose) {
                    $logger->info( "Processing backup job: " . $job->{name} . " (job_id: " . $job->{job_id} . ")" );
                }

                if ($dry_run) {
                    print "Would execute backup job: " . $job->{name} . " (job_id: " . $job->{job_id} . ")\n";
                }
                else {
                    _execute_backup_job( $jobs_manager, $job );
                }

                $jobs_processed++;
            }
        }

        if ($verbose) {
            $logger->info("Processed $jobs_processed backup jobs");
        }

        if ( ( $verbose || $debug ) && $dry_run && $jobs_processed == 0 ) {
            $logger->info("No backup jobs are currently due to run");
        }
    };

    if ($@) {
        $logger->error("Error in backup jobs helper: $@");
        exit 1;
    }

    if ($verbose) {
        $logger->info("Backup jobs helper completed successfully");
    }
}

sub _should_job_run_now {
    my ( $job, $scheduler, $current_time ) = @_;

    # Parse job configuration
    my $schedule_info = $job->{schedule_info} || {};
    my $last_run      = $job->{last_run};

    # Calculate next run time based on schedule
    my $next_run_time;

    eval { $next_run_time = $scheduler->calculate_next_run( $schedule_info, $last_run ); };

    if ($@) {
        $logger->warn( "Could not calculate next run time for job " . $job->{job_id} . ": $@" );
        return 0;
    }

    $logger->debug( "Next run time for job " . $job->{job_id} . " ( $job->{description} ) : " . scalar localtime($next_run_time) );

    # Check if current time is past the next run time
    return defined $next_run_time && $current_time >= $next_run_time;
}

sub _execute_backup_job {
    my ( $jobs_manager, $job ) = @_;

    my $job_id = $job->{job_id};

    eval {
        # Generate a unique run_uuid for this backup run instance
        require Data::UUID;
        my $ug       = Data::UUID->new();
        my $run_uuid = lc( $ug->create_str() );

        $logger->info( "Starting backup job: " . $job->{name} . " (job_id: $job_id, run_uuid: $run_uuid)" );

        # Store the current_run_uuid in the backup_jobs table for tracking
        $jobs_manager->set_current_run_uuid( $job_id, $run_uuid );

        # Update job status to running and set last run timestamp
        $jobs_manager->update_backup_job(
            $job_id,
            status             => 'running',
            last_run_timestamp => time(),
        );

        # Fork the backup runner script in the background
        my $backup_runner_path = $Whostmgr::CometBackup::Constants::BACKUP_RUNNER_PATH;

        unless ( -x $backup_runner_path ) {
            die "Backup runner script not found or not executable: $backup_runner_path";
        }

        my $pid = fork();

        if ( !defined $pid ) {

            # Fork failed
            die "Failed to fork backup runner process: $!";
        }

        if ( $pid == 0 ) {

            # Child process - exec the backup runner
            # Redirect STDOUT and STDERR to the log file
            open STDOUT, '>>', '/var/log/comet_backup_jobs_helper.log' or die "Cannot redirect STDOUT: $!";
            open STDERR, '>>', '/var/log/comet_backup_jobs_helper.log' or die "Cannot redirect STDERR: $!";

            # Become session leader to fully detach
            require POSIX;
            POSIX::setsid() or die "Cannot start a new session: $!";

            # Execute the backup runner with run_uuid
            exec( $backup_runner_path, '--force', '--job_uuid', $job_id, '--run_uuid', $run_uuid );

            # If exec fails, we die (child process only)
            die "Failed to exec backup runner: $!";
        }

        # Parent process - just log that we started the runner
        $logger->info( "Forked backup runner process (PID: $pid) for job: " . $job->{name} );
    };

    if ($@) {
        $logger->error( "Failed to execute backup job " . ( $job_id // 'job_id UNSET' ) . ": $@" );

        # Update job status to failed
        eval {
            $jobs_manager->update_backup_job(
                $job_id,
                status => 'failed',
            );
            $jobs_manager->clear_current_run_uuid($job_id);
        };
    }
}

# Run the main function if this script is executed directly
main() if !caller;

1;

__END__

=head1 SEE ALSO

L<Whostmgr::CometBackup::BackupJobs>, L<Whostmgr::CometBackup::Scheduling>

=head1 AUTHOR

WebPros International, LLC

=head1 COPYRIGHT

Copyright 2025 WebPros International, LLC. All rights reserved.

=cut
[ CLOSE ]