It's a bash script and it's used to clean files with retention period. So, it might be useful to clear large-scale backup storing servers to delete these files with a particular retention period. I just built the same script with 3 ways of working.
- Easy to handle this script
- Files removing with a retention period
- Easy to schedule it as a cron job.
- Easy to manage Backup/Log's on a Linux machine (Cron Job/Manual)
sudo yum -y install git
please note that this is a bash script where I used for Linux-based distros and servers.
git clone https://github.com/yousafkhamza/retention-based-cleanup-script.git
cd retention-based-cleanup-script
chmod +x cleanup*.sh
yousaf@MSI:/mnt/c/Users/Downloads$ bash cleanup.sh
Enter your directory path: ./wordpress <--------- Enter your absolute path of that directory you need
Enter retension period: 2200 <-------- Retention period
--------------------------------------------------------------
The below-listed files are older than 2200 days under this directory ./wordpress
--------------------------------------------------------------
./wordpress/wp-admin/css/farbtastic.css
./wordpress/wp-admin/images/browser.png
./wordpress/wp-admin/images/comment-grey-bubble-2x.png
Do you want to remove these above-listed files? (Please verify it before) [Y/N]: y <------------------ Ask you a confirmation before deleting
Deleting ./wordpress/wp-admin/css/farbtastic.css
Deleting ./wordpress/wp-admin/images/browser.png
Deleting ./wordpress/wp-admin/images/comment-grey-bubble-2x.png
yousaf@MSI:/mnt/c/Users/Downloads$ bash cleanup_cmd_variable_passing.sh ./wordpresss/ 4000 <---------- First value should be absalute path and second one is the retetion period
--------------------------------------------------------------
The below-listed files are older than 2200 days under this directory ./wordpress
--------------------------------------------------------------
./wordpress/wp-admin/css/farbtastic.css
./wordpress/wp-admin/images/browser.png
./wordpress/wp-admin/images/comment-grey-bubble-2x.png
Do you want to remove these above-listed files? (Please verify it before) [Y/N]: y <------------------ Ask you a confirmation before deleting
Deleting ./wordpress/wp-admin/css/farbtastic.css
Deleting ./wordpress/wp-admin/images/browser.png
Deleting ./wordpress/wp-admin/images/comment-grey-bubble-2x.png
yousaf@MSI:/mnt/c/Users/Downloads$ ./cleanup_for_cron.sh ./wordpress/ 2000
--------------------------------------------------------------
The below-listed files are older than 2000 days under this directory ./wordpress/
--------------------------------------------------------------
./wordpress/wp-admin/css/farbtastic.css
./wordpress/wp-admin/images/browser.png
./wordpress/wp-admin/images/comment-grey-bubble-2x.png
!...........................WARNING..............................!
We are going to start deleting these above files within 10sec....
if you do not need to execute this purging task, please quite this script on here using with [ctrl + c]
!...........................WARNING..............................!
Deleting ./wordpress/wp-admin/css/farbtastic.css
Deleting ./wordpress/wp-admin/images/browser.png
Deleting ./wordpress/wp-admin/images/comment-grey-bubble-2x.png
crontab -e
0 3 * * * /path/to/cleanup_for_cron.sh /path/to/location/ 30 <---------------------- Run cleanup_for_cron.sh at 3 am every day
Who you guys need to set up an automation backup/log removing as per the retention period please use the script with cronjob.
vim cleanup.sh
#!/bin/bash
read -p "Enter your directory: " directory
read -p "Enter retension period: " rp
rm -f ./delcleanup.txt
if [[ -z "$directory" || -z "$rp" ]]
then
echo "Please specify a valid absalute direcotry path and retionsion period!"
exit 1
else
if ! [[ "$directory" =~ ^[[:alnum:]]*$ || "$rp" =~ [^0-9] ]];
then
if [[ -d "$directory" ]];
then
echo "--------------------------------------------------------------"
echo "The below-listed files are older than $rp days under this directory $directory"
echo "--------------------------------------------------------------"
find "$directory" -type f -iname "*" -mtime +"$rp" -exec ls {} \; | tee -a ./delcleanup.txt
echo ""
if [ -s "./delcleanup.txt" ]
then
read -p "Do you want to remove these above-listed files? (Please verify it before) [Y/N]: " con
echo ""
if [[ "$con" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
for delete in $(cat ./delcleanup.txt); do echo "Deleting $delete"; rm -f $delete ; done;
rm -f ./delcleanup.txt
else
rm -f ./delcleanup.txt
fi
else
echo "There is no files found with this $rp days retention period!"
rm -f ./delcleanup.txt
fi
else
echo "No such direcotry found!"
fi
else
echo "Please enter a valid path and period!"
exit 1
fi
fi
It's a simple bash script and it's used to clean files with retention period. So, maybe it's useful for those who handled backup/log management on Linux system engineers. Please let me know if you have to face any issues while using this script and please find the below contact details of who needs to connect me.