Last active
August 11, 2018 17:21
-
-
Save marco-ostaska/00c2a4df0c5f4f99a1f5afdef94a975f to your computer and use it in GitHub Desktop.
This script was tested with centos 7 (still have some bugs in ubuntu). This is the script I use before packing my vagrant boxes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Check if it is ubuntu or centos | |
if [ -f /etc/os-release ] | |
then | |
. /etc/os-release | |
OS_NAME=`echo $NAME | awk '{print $1}' | tr '[:upper:]' '[:lower:]'` | |
else | |
echo "Unable to get os" | |
exit 1 | |
fi | |
clean_hitory() | |
{ | |
unset HISTFILE | |
[ -f /root/.bash_history ] && rm /root/.bash_history | |
[ -f /home/vagrant/.bash_history ] && rm /home/vagrant/.bash_history | |
history -c | |
su - vagrant -c "history -c" | |
} | |
zero_vm_compression() | |
{ | |
dd if=/dev/zero of=/dummy.bin bs=1M | |
rm -rf /dummy.bin | |
} | |
centos_purge() | |
{ | |
#clean yum | |
echo "$OS_NAME: Step 1/5 - Cleaning yum" | |
yum clean all | |
rm -rf /var/cache/yum | |
echo "$OS_NAME: Step 2/5 - Cleaning /tmp" | |
rm -rf /tmp/* # clean /tmp | |
echo "$OS_NAME: Step 3/5 - Zero free space fro VM compression" | |
zero_vm_compression; #Zero free space to helps VM compression | |
echo "$OS_NAME: Step 4/5 - Cleaning last logged in users logs " | |
rm -f /var/log/wtmp /var/log/btmp #clean up the last logged in users logs | |
echo "$OS_NAME: Step 5/5 - cleaning history" | |
clean_hitory; #clean up history | |
echo "$OS_NAME: ALL DONE" | |
} | |
ubuntu_purge() | |
{ | |
# Remove APT cache | |
echo "$OS_NAME: Step 1/7 - Cleaning apt" | |
apt-get clean -y && apt-get autoclean -y | |
find /var/lib/apt -type f | xargs rm -f | |
echo "$OS_NAME: Step 2/7 - cleaning documentation" | |
find /var/lib/doc -type f | xargs rm -f # remove documentation | |
echo "$OS_NAME: Step 3/7 - cleaning headers" | |
rm -rf /usr/src/linux-headers* #remove headers | |
echo "$OS_NAME: Step 4/7 - Cleaning /tmp" | |
rm -rf /tmp/* # clean /tmp | |
echo "$OS_NAME: Step 5/7 - Zero free space fro VM compression" | |
zero_vm_compression; #Zero free space to helps VM compression | |
echo "$OS_NAME: Step 6/7 - remove logs" | |
find /var/log -type f | while read f; do echo -ne '' > $f; done; | |
echo "$OS_NAME: Step 7/7 - cleaning history" | |
clean_hitory; | |
echo "$OS_NAME: ALL DONE" | |
} | |
if [ $OS_NAME == 'centos' ] | |
then | |
centos_purge; | |
elif [ $OS_NAME == 'ubuntu' ] | |
then | |
ubuntu_purge; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment