/
usr
/
local
/
stat_watch
/
includes
/
File Upload :
llllll
Current File: //usr/local/stat_watch/includes/create.shf
#! /bin/bash ### Create a Stat Watch job v_ACCOUNT= v_NAME= function fn_name { echo -n "What name do you want to use for this job" if [[ -n "$v_ACCOUNT" ]]; then echo -n " (leave blank for \"$v_ACCOUNT\")" fi read -ep "? " v_NAME echo if [[ -z "$v_NAME" && -n "$v_ACCOUNT" ]]; then v_NAME="$v_ACCOUNT" elif [[ -z "$v_NAME" ]]; then echo "A name must be provided" fn_name fi if [[ $( echo "$v_NAME" | egrep -c "^[0-9a-zA-Z]+$" ) -lt 1 ]]; then echo "The name should only contain letters and numbers" fn_name fi if [[ -f "$f_JOBS" ]]; then if [[ $( egrep -c "^$v_NAME:" "$f_JOBS" ) -gt 0 ]]; then echo "That name has already been used" fn_name fi fi } function fn_create { local v_HOMEDIR local v_MONITOR local v_YN ### Prompt for basic details about the job that we're going to create read -ep "What is the name of the account that you're creating a Stat Watch job for? " v_ACCOUNT echo if [[ $( grep -E "^""$v_ACCOUNT"":" -c /etc/passwd ) -gt 0 ]]; then echo "Account \"$v_ACCOUNT\" Does not exist" v_ACCOUNT= else v_HOMEDIR="$( grep -E "^""$v_ACCOUNT"":" /etc/passwd | cut -d ":" -f6 )" if [[ -d "$v_HOMEDIR""/public_html" ]]; then read -ep "Monitor directory $( echo -n "$v_HOMEDIR/public_html" | "$d_PROGRAM"/scripts/escape.pl ) (y/N)? " v_YN if [[ $( echo "$v_YN" | grep -E -c "^[yY]" ) -gt 0 ]]; then v_MONITOR="$v_HOMEDIR""/public_html" fi fi fi if [[ -z "$v_MONITOR" ]]; then read -ep "Name one directory we should monitor (you can add more later): " v_MONITOR if [[ ! -e $v_MONITOR ]]; then echo "\"$v_MONITOR\" is not a directory" exit elif [[ ${v_MONITOR:0:1} != "/" ]]; then echo "Please provide the full path to the directory" exit fi fi ### Get a name for the job fn_name ### And figure out where we're going to store this job local v_DIR echo "Next we need a directory to designate as the working directory for this job. It's recommended that this directory not contain any other files and directories so that its contents can be completely dedicated to this job. Be sure that the directory exists on a partition with enough extra space to store the files that we will be backing up. Also, if this Stat Watch job is being created out of concern that malicious users might have access to the files that are being monitored, it is best to select a directory outside of that threat." | "$d_PROGRAM"/scripts/fold_out.pl echo local v_PATH="$v_DEFAULT_JOB_DIRECTORY" if [[ -z "$v_DEFAULT_JOB_DIRECTORY" ]]; then v_PATH="/home/stat_watch" if [[ -d "/home/stat_watch" ]]; then v_PATH="/home/stat_watch" elif [[ -d "/backup/stat_watch" ]]; then v_PATH="/backup/stat_watch" elif [[ -n "$( cd /*/stat_watch2 2> /dev/null && pwd )" ]]; then v_PATH="$( cd /*/stat_watch2 2> /dev/null && pwd )" fi fi read -ep "What directory do you want to use as the working directory for this job (Default: $( echo -n "$v_PATH/$v_NAME" | "$d_PROGRAM"/scripts/escape.pl ))? " v_DIR echo if [[ -z "$v_DIR" ]]; then v_DIR="$v_PATH/$v_NAME" if [[ ! -d "$v_PATH" ]]; then mkdir -p "$v_PATH" fi fi fn_file_path "$v_DIR" if [[ -n "$s_DIR" && ! -d "$s_DIR" ]]; then echo "Directory '$s_DIR' does not exist" exit elif [[ ! -d "$v_DIR" ]]; then read -ep "Directory $( echo -n "$v_DIR" | "$d_PROGRAM"/scripts/escape.pl ) does not exist. Do you want me to create it for you (Y/n)? " v_YN echo if [[ $( echo "$v_YN" | grep -E -c "^[nN]" ) -gt 0 ]]; then echo "exiting" exit fi mkdir -p "$v_DIR" fi if [[ -f "$v_DIR"/"$v_NAME".job ]]; then echo "There already appears to be a Stat Watch job at $( echo -n "$v_DIR/$v_NAME.job" | "$d_PROGRAM"/scripts/escape.pl ). You should probably delete or modify that rather than create a new job." | "$d_PROGRAM"/scripts/fold_out.pl exit fi d_BACKUP="$v_DIR"/backup_"$v_NAME" if [[ -n "$v_BACKUP_DIRECTORY" ]]; then echo -n "The default backup directory is " echo -n "$v_BACKUP_DIRECTORY" | "$d_PROGRAM"/scripts/escape.pl -n read -ep "Do you want to use the default backup directory for backup storage? (Y/n)" v_YN echo if [[ $( echo "$v_YN" | grep -E -c "^[nN]" ) -eq 0 ]]; then d_BACKUP="$v_BACKUP_DIRECTORY" fi fi ##### This probably shouldn't be a default question # ### Prompt if the job should stop running after 45 days # local b_EXPIRE=false # read -ep "Do you want this job to stop running after 45 days, and for all backed up files to be removed after 60 days (Y/n)? " v_YN # echo # if [[ $( echo "$v_YN" | grep -E -c "^[nN]" ) -eq 0 ]]; then # b_EXPIRE=true # fi ### Prompt for an email address to send reports to local v_EMAIL read -ep "What email address should reports be sent to (leave blank for none)? " v_EMAIL echo if [[ -n "$v_EMAIL" ]]; then echo -e "\e[91m" echo "If $v_EMAIL is a customer-facing address, MAKE SURE that appropriate expectations have been set for how these emails will be followed up on." | "$d_PROGRAM"/scripts/fold_out.pl echo "Here is an example of setting such expectations: https://raw.githubusercontent.com/sporks5000/stat_watch/master/texts/expectations.txt" | "$d_PROGRAM"/scripts/fold_out.pl echo echo "Note: If you create a file at $( echo -n "$v_DIR/$v_NAME""_message_head.txt" | "$d_PROGRAM"/scripts/escape.pl ) or $( echo -n "$v_DIR/$v_NAME""_message_foot.txt" | "$d_PROGRAM"/scripts/escape.pl ), the contents of that file will be sent at the top or bottom, respectively, of each email message. It might be wise to set expectations there as well" | "$d_PROGRAM"/scripts/fold_out.pl echo -e "\e[0m" ### Pause here, because I SUPER want people to read this sleep 10 fi ### Create the relevant files mkdir -p "$d_BACKUP" touch $v_DIR/$v_NAME.log local f_JOB="$v_DIR/$v_NAME".job ### Changing indentation for the heredoc cat << EOF > "$f_JOB" ### This file created by Stat Watch I $v_MONITOR BackupD $d_BACKUP ### back up the following extensions: .php, .php4, .php5, .php7, .pl, .pm, .py, .js, .json, .css, .cgi .htm, .html, .htaccess, .htpasswd, .sh, .rb BackupR \.(p(hp(4|5|7)?|[lym])|js(on)?|c(ss|gi)|ht(ml?|access|passwd)|sh|rb)$ BackupMD $v_BACKUP_MIN_DAYS BackupMC $v_BACKUP_MAX_COPIES Log $v_DIR/$v_NAME.log ### These lines specific to Stat Watch job functionality Name $v_NAME Email-retain $v_EMAIL_RETAIN EOF ### Returning to previous indentation if [[ -n "$v_EMAIL" ]]; then echo "Email $v_EMAIL" >> "$f_JOB" fi if [[ $b_EXPIRE == true ]]; then echo "Expire $( date --date="now + 45 days" +%s )" >> "$f_JOB" fi ### Log that the job was created by this script echo "$( date +%Y-%m-%d" "%T" "%z ) - Job \"$f_JOB\" created by $( echo -n "$f_PROGRAM" | "$d_PROGRAM"/scripts/escape.pl )" >> "$v_DIR"/"$v_NAME".log ### Create a working directory and a file to document jobs created mkdir -p "$d_WORKING" local f_TEMP=$( mktemp ) ### Copy all of the other jobs into a temp file grep -E -v "^$f_JOB - Created " "$f_JOBS" > "$f_TEMP" 2> /dev/null ### Add this job to the temp file echo "$v_NAME"":""$f_JOB - Created $( date +%Y-%m-%d" "%T" "%z )" >> "$f_TEMP" ### Replace the temp file mv -f "$f_TEMP" "$f_JOBS" ### Output text telling the user what next steps they need to take echo echo -e "A job file has been created at \e[92m$( echo -n "$f_JOB" | "$d_PROGRAM"/scripts/escape.pl )\e[0m" echo "Run \"stat_watch --help job\" for further information on how it can be edited to suit your needs" echo "Once you have that file organized as you need it, run the following command:" echo echo "$( echo -n "$d_PROGRAM/$f_PROGRAM" | "$d_PROGRAM"/scripts/escape.pl ) --run $( echo -n "$f_JOB" | "$d_PROGRAM"/scripts/escape.pl )" echo echo "Then add the following line to root's crontab (adjusting times as desired):" echo ### set a random minute from 1 to 59 for the cron job to run echo "$(( 1 + RANDOM % 58 )) */2 * * * "$( echo -n "$d_PROGRAM/$f_PROGRAM" | "$d_PROGRAM"/scripts/escape.pl ) --run $( echo -n "$f_JOB" | "$d_PROGRAM"/scripts/escape.pl )" > /dev/null 2>&1" echo }
Copyright ©2k19 -
Hexid
|
Tex7ure