<commit_before>#!/usr/bin/perl

use strict;
use warnings;
use English qw( -no_match_vars );
use Carp;
use Getopt::Std; # for commandline args
use IO::CaptureOutput 'capture_exec'; # for reliable shelling out
use JSON; # imports encode_json, decode_json, to_json and from_json
use String::ShellQuote; # for sanitising strings when shelling out

my $description = <<'ENDDESC';
#==============================================================================#
# Server Backup Script
#==============================================================================#
# 
# A simple backup script for backing up data on one Linux server from another
# via SSH.
#
# This script is run on the server being backed up to, and the script pulls the
# data off the server over SSH.
#
# The script is designed to be run nightly via cron, and retains 7 coppies, one
# for every day of the week.
# 
# Flags:
# ------
# -c - REQUIRED - the path to the JSON file contianing the config information
#      describing the backup to be performed.
# -v - enter verbose mode
# -d - enter debug mode  (implies -v)
#
# Returns Codes:
# --------------
# 0 - success
# 1 - error
#
#==============================================================================#
ENDDESC

#
# Define 'global' variables
#
my $NUM_ERRORS = 0; # counter for errors while shelling out

#
# Process arguments
#
my %flags;
unless(getopts('c:vd', \%flags)){
	print "FATAL ERROR - invalid arguments - see documentation below:\n\n$description\n";
    exit 1;
}
my $json_path = $flags{c};
unless($json_path && -f $json_path){
	print "FATAL ERROR - no valid coniguration file path provided - see documentation below:\n\n$description\n";
    exit 1;
}
my $verbose = 0;
my $debug = 0;
if($flags{d}){
    $verbose = 1;
    $debug = 1;
    print "DEBUG - entering debug mode (verbose implied)\n";
}elsif($flags{v}){
    $verbose = 1;
    print "INFO - entering verbose mode\n";
}

#
# Parse the config file
#
my $config = load_config($json_path);
my $SSH = $config->{localPaths}->{ssh};
unless(defined $SSH && -f $SSH){
	print "FATAL ERROR - invalid SSH path '$SSH'\n";
	exit 1;
}
my $SCP = $config->{localPaths}->{scp};
unless(defined $SCP && -f $SCP){
	print "FATAL ERROR - invalid SCP path '$SCP'\n";
	exit 1;
}
my $MKDIR = $config->{localPaths}->{mkdir};
unless(defined $MKDIR && -f $MKDIR){
	print "FATAL ERROR - invalid mkdir path '$MKDIR'\n";
	exit 1;
}
unless(defined $config->{localPaths}->{backupBaseDir} && -d $config->{localPaths}->{backupBaseDir}){
	print "FATAL ERROR - backup base $config->{localPaths}->{backupBaseDir} does not exist\n";
	exit 1;
}

#
# Calculate which of the rolling backups to target
#
my @days = qw{Sun Mon Tue Wed Thur Fri Sat};
my @datetime = localtime(time);
my $daily_backup_dir = $config->{localPaths}->{backupBaseDir}.$days[$datetime[6]].q{/};
print "INFO - daily backup dir calculated: $daily_backup_dir\n" if $verbose;
unless(-d $daily_backup_dir){
	print "FATAL ERROR - daily backup dir $daily_backup_dir does not exist\n";
	exit 1;
}

#
# Loop through the defined servers
#<commit_msg>Added automatic creation of daily folders<commit_after>#!/usr/bin/perl

use strict;
use warnings;
use English qw( -no_match_vars );
use Carp;
use Getopt::Std; # for commandline args
use IO::CaptureOutput 'capture_exec'; # for reliable shelling out
use JSON; # imports encode_json, decode_json, to_json and from_json
use String::ShellQuote; # for sanitising strings when shelling out

my $description = <<'ENDDESC';
#==============================================================================#
# Server Backup Script
#==============================================================================#
# 
# A simple backup script for backing up data on one Linux server from another
# via SSH.
#
# This script is run on the server being backed up to, and the script pulls the
# data off the server over SSH.
#
# The script is designed to be run nightly via cron, and retains 7 coppies, one
# for every day of the week.
# 
# Flags:
# ------
# -c - REQUIRED - the path to the JSON file contianing the config information
#      describing the backup to be performed.
# -v - enter verbose mode
# -d - enter debug mode  (implies -v)
#
# Returns Codes:
# --------------
# 0 - success
# 1 - error
#
#==============================================================================#
ENDDESC

#
# Define 'global' variables
#
my $NUM_ERRORS = 0; # counter for errors while shelling out

#
# Process arguments
#
my %flags;
unless(getopts('c:vd', \%flags)){
	print "FATAL ERROR - invalid arguments - see documentation below:\n\n$description\n";
    exit 1;
}
my $json_path = $flags{c};
unless($json_path && -f $json_path){
	print "FATAL ERROR - no valid coniguration file path provided - see documentation below:\n\n$description\n";
    exit 1;
}
my $verbose = 0;
my $debug = 0;
if($flags{d}){
    $verbose = 1;
    $debug = 1;
    print "DEBUG - entering debug mode (verbose implied)\n";
}elsif($flags{v}){
    $verbose = 1;
    print "INFO - entering verbose mode\n";
}

#
# Parse the config file
#
my $config = load_config($json_path);
my $SSH = $config->{localPaths}->{ssh};
unless(defined $SSH && -f $SSH){
	print "FATAL ERROR - invalid SSH path '$SSH'\n";
	exit 1;
}
my $SCP = $config->{localPaths}->{scp};
unless(defined $SCP && -f $SCP){
	print "FATAL ERROR - invalid SCP path '$SCP'\n";
	exit 1;
}
my $MKDIR = $config->{localPaths}->{mkdir};
unless(defined $MKDIR && -f $MKDIR){
	print "FATAL ERROR - invalid mkdir path '$MKDIR'\n";
	exit 1;
}
unless(defined $config->{localPaths}->{backupBaseDir} && -d $config->{localPaths}->{backupBaseDir}){
	print "FATAL ERROR - backup base $config->{localPaths}->{backupBaseDir} does not exist\n";
	exit 1;
}

#
# Calculate which of the rolling backups to target
#
my @days = qw{Sun Mon Tue Wed Thur Fri Sat};
my @datetime = localtime(time);
my $daily_backup_dir = $config->{localPaths}->{backupBaseDir}.$days[$datetime[6]].q{/};
print "INFO - daily backup dir calculated: $daily_backup_dir\n" if $verbose;
# make sure the daily directory exists, if not, try create it
unless(-d $daily_backup_dir){
	unless(-d $daily_backup_dir){
		print "Creating daily backup dir ...\n";
		my $cout = exec_command(qq{$MKDIR }.shell_quote($daily_backup_dir));
		unless($cout->{success}){
			print "\nFATAL ERROR - failed to create daily backup dir $daily_backup_dir\n";
			exit 1;
		}
	}
}

#
# Loop through the defined servers
#
{'fraction_non_alphanumeric': 0.20773549594510293, 'fraction_numerical': 0.005302557704304429, 'mean_word_length': 4.344166666666666, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 32, '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': '5744373', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2025, 'n_words': 699}