<commit_before><?php
require_once("classCommandLine.php");

// Get info from command line args
$args = CommandLine::parseArgs($_SERVER['argv']);
$host=( (array_key_exists("host", $args) )?($args["host"]):(false) ) ;
$vg=( (array_key_exists("vg", $args) )?($args["vg"]):(false) ) ;
$lvs=( (array_key_exists("lvs", $args) )?(explode(",", $args["lvs"]) ):(false) ) ;
$interval=( (array_key_exists("interval", $args) )?($args["interval"]):(false) ) ;
$max_intervals =( (array_key_exists("max_intervals", $args) )?($args["max_intervals"]):(5) ) ;
$debug=( (array_key_exists("debug", $args) )?($args["debug"]):(false) ) ;
$md5=( (array_key_exists("md5", $args) )?($args["md5"]):(false) ) ;
$date = date("Ymd");
if( ($host===false) || ($vg===false) || ($lvs===false) || ($interval===false) ) {print("ERROR: requires arguments for host, vg, lvs, interval, and max_interval\n"); exit(); }
$valid_intervals = array("daily", "weekly", "monthly", "yearly");
if(!in_array($interval , $valid_intervals ) ) {print("Invalid interval given.\n"); exit(); }

// Make sure that we have a dir for this host.
$lhost_dir = "/data/lv_backups/{$host}";
// Does $lhost_dir exist?  if not, create it.
if(!file_exists($lhost_dir) ) {$cmd = "mkdir {$lhost_dir}"; go($cmd, $debug);}


### Get started:
foreach($lvs as $lv) {
	// Create a lock file / verify we're not going to conflict with another backup process.
	$lock = "/tmp/{$host}_{$lv}.lock";
	$f = fopen($lock, 'x');
	if ($f === false) { print("\nCan't acquire lock: {$lock}\n"); continue; }

	$snapshot = "{$lv}_{$interval}_snapshot_{$date}";
	$ldir = "{$lhost_dir}/{$lv}";
	$lfile = "{$lv}_{$interval}_snapshot";

        #  Start by removing any old backup snapshots that are lingering on the server
        $cmd = "ssh {$host} 'find /dev -name {$lv}_* -exec lvremove -f {} \\;'"; go($cmd, $debug);

        #  Connect to the remote server an create a snapshot
	$cmd = "ssh {$host} lvcreate -L4G -s -n {$snapshot} /dev/{$vg}/{$lv}"; go($cmd, $debug);
	if($debug){$cmd = "ssh {$host} md5sum /dev/{$vg}/{$snapshot}"; go($cmd, $debug); }

	# Make a copy of the local image file that alreqady exists (BTFFS should make this instantaneous)
	rotate_files($ldir, $lfile, $max_intervals, false);
	if($debug){$cmd = "md5sum {$ldir}/{$lfile}"; go($cmd, $debug);}<commit_msg>Tag remote snapshots with the local hostname, so that multiple backup servers can connect to the same server and not stomp on each other.<commit_after><?php
require_once("classCommandLine.php");

// Use the local hostname to prevent collisions between multiple backup servers
// that might touch the same server simultaneously
$hostname = gethostname();

// Get info from command line args
$args = CommandLine::parseArgs($_SERVER['argv']);
$host=( (array_key_exists("host", $args) )?($args["host"]):(false) ) ;
$vg=( (array_key_exists("vg", $args) )?($args["vg"]):(false) ) ;
$lvs=( (array_key_exists("lvs", $args) )?(explode(",", $args["lvs"]) ):(false) ) ;
$interval=( (array_key_exists("interval", $args) )?($args["interval"]):(false) ) ;
$max_intervals =( (array_key_exists("max_intervals", $args) )?($args["max_intervals"]):(5) ) ;
$debug=( (array_key_exists("debug", $args) )?($args["debug"]):(false) ) ;
$md5=( (array_key_exists("md5", $args) )?($args["md5"]):(false) ) ;
$date = date("Ymd");
if( ($host===false) || ($vg===false) || ($lvs===false) || ($interval===false) ) {print("ERROR: requires arguments for host, vg, lvs, interval, and max_interval\n"); exit(); }
$valid_intervals = array("daily", "weekly", "monthly", "yearly");
if(!in_array($interval , $valid_intervals ) ) {print("Invalid interval given.\n"); exit(); }

// Make sure that we have a dir for this host.
$lhost_dir = "/data/lv_backups/{$host}";
// Does $lhost_dir exist?  if not, create it.
if(!file_exists($lhost_dir) ) {$cmd = "mkdir {$lhost_dir}"; go($cmd, $debug);}


### Get started:
foreach($lvs as $lv) {
	// Create a lock file / verify we're not going to conflict with another backup process.
	$lock = "/tmp/{$host}_{$lv}.lock";
	$f = fopen($lock, 'x');
	if ($f === false) { print("\nCan't acquire lock: {$lock}\n"); continue; }

	$snapshot = "{$hostname}_{$lv}_{$interval}_snapshot_{$date}";
	$ldir = "{$lhost_dir}/{$lv}";
	$lfile = "{$lv}_{$interval}_snapshot";

        #  Start by removing any old backup snapshots that are lingering on the server
        $cmd = "ssh {$host} 'find /dev -name {$hostname}_{$lv}_* -exec lvremove -f {} \\;'"; go($cmd, $debug);

        #  Connect to the remote server an create a snapshot
	$cmd = "ssh {$host} lvcreate -L4G -s -n {$snapshot} /dev/{$vg}/{$lv}"; go($cmd, $debug);
	if($debug){$cmd = "ssh {$host} md5sum /dev/{$vg}/{$snapshot}"; go($cmd, $debug); }

	# Make a copy of the local image file that alreqady exists (BTFFS should make this instantaneous)
	rotate_files($ldir, $lfile, $max_intervals, false);
	if($debug){$cmd = "md5sum {$ldir}/{$lfile}"; go($cmd, $debug);}
{'fraction_non_alphanumeric': 0.22809883602205433, 'fraction_numerical': 0.0028588931999183175, 'mean_word_length': 5.4874172185430465, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '709614', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1738, 'n_words': 460}