#!/bin/bash# Create incremental backups using rsync with hard links# Current impementation meant for daily backups# TODO: Implement arguments for customizability- number of backups, date interval, source and destination folderscurrent_date=$(date)to_backup=("/etc""/home""/root""/var""/srv""/opt")# number of backups to keepbackup_backlog=10
# Change to backup foldercd /backup
# rotate backups# wildcard allows for missing days due to downtime (using async scheduler)rm-rf backup.*.${backup_backlog}for((log_number=$backup_backlog; log_number >= 2; log_number-- ));do
log_date=$(ls | grep backup.*.$((${log_number}-1)) | cut-d'.'-f 2)# If the destination folder doesn't have the max number of backups (backup_backlog), filenaming gets weird# This makes sure that if any of the backup directories are missing, an empty file with the right name is added# New empty file keeps naming convention consistentif[!-d"backup.${log_date}.$((${log_number}-1))"];then
touch"backup.$(date--date="$(date) -${log_number} days"'+%F').$((${log_number}-1))"log_date=$(ls | grep backup.*.$((${log_number}-1)) | cut-d'.'-f 2)fi
mv"backup.${log_date}.$((${log_number}-1))""backup.${log_date}.${log_number}"done
cp-al"backup.$(ls | grep backup.*.0 | cut-d'.'-f 2).0""backup.$(ls | grep backup.*.0 | cut-d'.'-f 2).1"# make new backup
rsync -a--dry-run--delete--exclude="/home/*/.cache"${to_backup[@]}"backup.$(ls | grep backup.*.0 | cut-d'.'-f 2).0"# Keeps naming convention consistentmv"backup.$(ls | grep backup.*.0 | cut-d'.'-f 2).0""backup.$(date'+%F').0/"
Check if certain files or directories have changed
Generate an arbitrary number of test files in a specified folder
#!/bin/sh# Generates an arbitrary number of files in a given directory# Default content of files is "test", but user can provide a custom string or the content of a file# Make sure all vars are emptyunset-v target_dir
unset-v count
unset-v content
show_help(){echo"
Usage: ./filegen <-d Directory> <-n Count> [-c String | Path]
-d:\tTarget directory
-n:\tNumber of files to generate
-c:\tContent of generated file, can be custom string or content of given file (defaults to "test")
For Example: ./filegen.sh -d test_directory -n 100 -c "Test Content"
-h:\tDisplays this message
"}# get all flagswhile getopts":c:d:n:h" opt;do
case"${opt}"in
d)target_dir=$OPTARG;;
n)count=$OPTARG;;
c)content=$OPTARG;;
h) show_help;exit 0;;*)echo'Incorrect Usage:'>&2
show_help
exit 1
esac
done
shift"$(( OPTIND -1))"# Make sure target directory and count were specifiedif[-z"$target_dir"]||[-z"$count"];then
echo'Incorrect usage: Please specify target directory (-d) and file count (-n)'
show_help
exit 1
fi# Check if $content is a file, if so read file contents into variableif[-f"$content"];then
if![-w"$content"];then
echo'Insufficient Privileges, using content "test"'content='test'else
echo"Using file content from: '$content'"content="$(cat$content)"fi
fi# If $content wasn't a file or file was empty, make sure content variable is populatedif[-z"$content"];then
content='test'fi# check if count is an intcase$countin''|*[!0-9]*)echo"$count is not an integer!";exit 1;;*)continue
esac# If existing directory, make sure there are permissionsif[-d$target_dir];then
if![-w$target_dir];then
echo"Permission denied!"exit 1
fi
fi# Make directoryif mkdir-p$target_dir 2>/dev/null;then# If the directory is made, make sure there are no pre-existing test filescd$target_dirrm test_*.txt 2> /dev/null
continue
else# Fails, usually means bad permissionsecho'Cannot create directory!'exit 1
fi# POSIX shell compatible for loopi=1
while["$i"-le"$count"];do
echo"$content"> test_${i}.txt
i=$((i+1))done
unset-v i
echo"Done creating ${count} files!"exit 0
Windows Scripts
Powershell
Find Listening TCP Services
# Find all open Ports and report the local address, local port, remote address, remote port, and Owning Process$connections=Get-NetTCPConnection|sort-objectState,LocalAddress,OwningProcess|Where-Object{$_.LocalAddress-notlike'*:*'}$connections|Format-Table-AutoSizeLocalAddress,LocalPort,RemoteAddress,RemotePort,State,@{Label="Owning Process"Expression={(Get-Process-Id$_.OwningProcess)}}
Command Prompt
Delete files older than X days
REM Windows batch file to delete all files in a specific folder older than x daysREM Based on script from https://www.howtogeek.com/131881/how-to-delete-files-older-than-x-days-on-windows/REM Get folder to clear out and time restrictionset/p "folder=Enter folder to clear out: "REM Check if folder existsifnotexist%folder%\*gotonotexistREM Enter how old in days the file must be to be deleted then lists the files in the folderREM If the input is invalid, catch it and quitset/p "UserInput=How old should the deleted files be (in days)? "set/a days=%UserInput%if%days%EQU0(echoDeletionFailed. CheckSyntax.
pausegotoquit)else(forfiles-p %folder%-s -m *.*-d -%days%-c "cmd /c echo @file"pause)REM Ask user if they want to delete the listed filesset/p "choice=Delete these files? [Y/N]: "if/I %choice%=="Y"gotodeletegotonodelete:nodeleteechoNotdeletingfiles.
gotoquit:deleteREM Delete files in specified folderechoDeletingfilesforfiles-p %folder%-s -m *.*-d -(Numberofdays)-c "cmd /c del @path"gotoquit:notexistechoFolderdoesnotexistgotoquit:quitpause