Skip to content

Instantly share code, notes, and snippets.

@NullArray
Last active July 21, 2019 20:17
Show Gist options
  • Save NullArray/a4bb0c513df0bf826268cfe154d985ed to your computer and use it in GitHub Desktop.
Save NullArray/a4bb0c513df0bf826268cfe154d985ed to your computer and use it in GitHub Desktop.
Shell script that finds all files and directories that have been modified in the last hour, copies them to a temporary directory and stored them as an encrypted archive.
#!/bin/bash
#____ ____ __
#\ \ / /____ _____/ |_ ___________
# \ Y // __ \_/ ___\ __\/ _ \_ __ \
# \ /\ ___/\ \___| | ( <_> ) | \/
# \___/ \___ >\___ >__| \____/|__|
# \/ \/
#--Author : Vector/NullArray
#----Twitter: @Real__Vector
#--------Licensed under GNU GPL 3
##################################################
# Coloring scheme for notfications
ESC="\x1b["
RESET=$ESC"39;49;00m"
RED=$ESC"31;01m"
GREEN=$ESC"32;01m"
# Warning
function warning()
{ echo -e "\n$RED [!] $1 $RESET\n"
}
# Green notification
function notification()
{ echo -e "\n$GREEN [+] $1 $RESET\n"
}
function file_ops()
{ printf "Please be patient while we collect relevant files..."
cwd=$(pwd)
cd $output
mkdir Archive
# Set up array to copy relevant files
while IFS= read -d $'\0' -r file ; do
file_list=("${file_list[@]}" "$file")
# Uncomment line 43 and comment line 44 in order to force the script to look for log files instead
# done < <( sudo find / -name "*.log" -print0)
done < <( sudo find / -mmin -60 -print0)
notification "All relevant data has been collected, processing..."
# Copy files to the specified Dir + temporary Archive directory
for file in "${file_list[@]}"
do
sudo cp -p -f $file -t Archive
done
notification "Archiving data with password..."
cd Archive
7z a results.7z * -p
mv results.7z ..
read -p "Secure delete 'Archive' files and dir? [Y/n]: " choice
if [[ $choice == 'y' || $choice == 'Y' ]]; then
# Shred files and delete Archive dir
cd ..
find Archive -depth -type f -exec shred -v -n 1 -z -u {} \; && rm -rf Archive
sleep 1 && clear
cd $cwd
notification "All operations completed."
exit 0
else
cd $cwd
notification "All operations completed."
exit 0
fi
}
# Funtion to handle operations related to a provided directory that does not exist
function dir_ops()
{ read -p 'Create directory? [Y/n]: ' choice
if [[ $choice == 'y' || $choice == 'Y' ]]; then
mkdir $output
stat $output || warning "Could not create directory. Exiting" && exit 0
file_ops
else
warning "Aborted..."
exit 0
fi
}
# Starting function
function main()
{ printf "%b\nWelcome.
This script will copy all files and dirs that were
altered in the last hour to a directory of your
choosing and store them in an encrypted archive.\n\n\n"
read -p 'Enter full path to output location : ' output
printf "%b\n\n"
notification "Checking output location..."
stat $output || dirstat=0
if [[ $dirstat == 0 ]]; then
dir_ops
fi
notification "Directory checked, proceeding with file operations..."
sleep 2
# Call file operations function
file_ops
}
# Check for root
if [[ "$EUID" -ne 0 ]]; then
warning "It is recommeded the script is run as root"
read -p 'Continue without root? [Y/n]: ' choice
if [[ $choice == 'y' || $choice == 'Y' ]]; then
main
else
exit 0
fi
else
main
fi
@Ekultek
Copy link

Ekultek commented May 3, 2018

Very well written

@NullArray
Copy link
Author

NullArray commented May 3, 2018

Thank you. 👍

@Ekultek
Copy link

Ekultek commented May 18, 2018

Just so you know I'm forking this for further use

@NullArray
Copy link
Author

That's alright man, if i don't specify any licensing you can assume that it is licensed under GNU GPL 3. While that may be of no consequence for personal use, if you want to use it in a business setting, your company or employer can be content in the knowledge that from a legal stand point i got you and by extension your company covered as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment