<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
# backup.pl
# Perform a backup and send the results to someone

$no_acl_check++;
require './fsdump-lib.pl';
$dump = &amp;get_dump($ARGV[0]);
$dump-&gt;{'id'} || die "Dump $ARGV[0] does not exist!";

# Check if this backup is already running
&amp;foreign_require("proc", "proc-lib.pl");
@procs = &amp;proc::list_processes();
@running = &amp;running_dumps(\@procs);
($running) = grep { $_-&gt;{'id'} eq $dump-&gt;{'id'} &amp;&amp;
		    $_-&gt;{'pid'} != $$ } @running;

$sfile = "$module_config_directory/$dump-&gt;{'id'}.$$.status";
if ($running) {
	# Already running! Do nothing ..
	$ok = 0;
	$out = &amp;text('email_already', $running-&gt;{'pid'})."\n";
	}
else {
	# Update status file
	%status = ( 'status' =&gt; 'running',
		    'pid' =&gt; $$,
		    'start' =&gt; time() );
	&amp;write_file($sfile, \%status);

	if ($dump-&gt;{'email'}) {
		# Save output for mailing
		$temp = &amp;transname();
		open(OUT, "&gt;$temp");
		}
	else {
		# Throw output away
		open(OUT, "&gt;/dev/null");
		}

	# Create tape change wrapper
	&amp;create_wrappers();

	$bok = &amp;execute_before($dump, OUT, 0);
	if (!$bok &amp;&amp; !$dump-&gt;{'beforefok'}) {
		# Before command failed!
		print OUT "\n$text{'email_ebefore'}\n";
		$status{'status'} = 'failed';
		}
	else {
		# Do the backup
		$now = time();
		$ok = &amp;execute_dump($dump, OUT, 0, 1, $now);

		# Re-update the status file
		if ($ok) {
			# Worked .. but verify if asked
			if ($dump-&gt;{'reverify'}) {
				print OUT "\n$text{'email_verify'}\n";
				$ok = &amp;verify_dump($dump, OUT, 0, 1, $now);
				}
			if ($ok) {
				$status{'status'} = 'complete';
				}
			else {
				$status{'status'} = 'verifyfailed';
				}
			}
		else {
			$status{'status'} = 'failed';
			}
		}
	$status{'end'} = time();
	&amp;write_file($sfile, \%status);

	if ($status{'status'} eq 'complete' || $dump-&gt;{'afteraok'}) {
		# Execute the post-backup script
		$bok = &amp;execute_after($dump, OUT, 0);
		if (!$bok &amp;&amp; !$dump-&gt;{'afterfok'}) {
			print OUT "\n$text{'email_eafter'}\n";
			$status{'status'} = 'failed';
			$ok = 0;
			}
		}
	close(OUT);

	if ($temp) {
		# Read output
		open(OUT, $temp);
		while(&lt;OUT&gt;) {
			s/\r//g;
			$out .= $_;
			}
		close(OUT);
		unlink($temp);
		}
	}

if ($out &amp;&amp; $dump-&gt;{'email'} &amp;&amp; &amp;foreign_check("mailboxes")) {
	# Construct the email
	&amp;foreign_require("mailboxes", "mailboxes-lib.pl");
	$host = &amp;get_system_hostname();
	@dirs = &amp;dump_directories($dump);
	$dirs = join(", ", @dirs);
	%hash = ( %$dirs, 'dirs' =&gt; $dirs );
	local $subject = &amp;substitute_template($dump-&gt;{'subject'}, \%hash) ||
			 &amp;text('email_subject', $dirs, $host);
	local $data = &amp;text('email_subject', $dirs, $host)."\n\n";
	$data .= $out;
	$data .= "\n";
	if ($ok) {
		$data .= $text{'email_ok'}."\n";
		}
	else {
		$data .= $text{'email_failed'}."\n";
		}

	# Send the email
	if (!$ok || !$config{'error_email'}) {
		# Only send email upon failure, or it requested always
		&amp;mailboxes::send_text_mail(&amp;mailboxes::get_from_address(),
					   $dump-&gt;{'email'},
					   undef,
					   $subject,
					   $data,
					   $config{'smtp_server'});
		}
	}

# Check for any dumps scheduled to run after this one
foreach $follow (&amp;list_dumps()) {
	if ($follow-&gt;{'follow'} eq $dump-&gt;{'id'} &amp;&amp; $follow-&gt;{'enabled'} == 2) {
		system("$cron_cmd $follow-&gt;{'id'}");
		}
	}

</pre></body></html>