{"id":18947636,"url":"https://github.com/yousafkhamza/retention-based-cleanup-script","last_synced_at":"2025-04-15T23:30:23.986Z","repository":{"id":195965561,"uuid":"386822622","full_name":"yousafkhamza/retention-based-cleanup-script","owner":"yousafkhamza","description":"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.","archived":false,"fork":false,"pushed_at":"2021-07-17T10:25:35.000Z","size":37,"stargazers_count":6,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2023-09-20T19:52:43.663Z","etag":null,"topics":["backup-manager","bash","linux","retention","scripting","shell-script"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yousafkhamza.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-07-17T03:09:45.000Z","updated_at":"2023-09-20T19:52:48.607Z","dependencies_parsed_at":"2023-09-20T20:13:41.268Z","dependency_job_id":null,"html_url":"https://github.com/yousafkhamza/retention-based-cleanup-script","commit_stats":null,"previous_names":["yousafkhamza/retention-based-cleanup-script"],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2Fretention-based-cleanup-script","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2Fretention-based-cleanup-script/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2Fretention-based-cleanup-script/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2Fretention-based-cleanup-script/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yousafkhamza","download_url":"https://codeload.github.com/yousafkhamza/retention-based-cleanup-script/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223688710,"owners_count":17186298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backup-manager","bash","linux","retention","scripting","shell-script"],"created_at":"2024-11-08T13:10:49.092Z","updated_at":"2024-11-08T13:10:50.932Z","avatar_url":"https://github.com/yousafkhamza.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Retention Based File-Cleanup Script (Bash)\n[![Build](https://travis-ci.org/joemccann/dillinger.svg?branch=master)](https://travis-ci.org/joemccann/dillinger)\n\n---\n## Description\n\nIt'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. \n\n----\n## Feature\n- Easy to handle this script\n- Files removing with a retention period\n- Easy to schedule it as a cron job.\n- Easy to manage Backup/Log's on a Linux machine (Cron Job/Manual)\n\n---\n## Pre-Requested Packages Installation \n\n```sh\nsudo yum -y install git \n```\n\u003e please note that this is a bash script where I used for Linux-based distros and servers. \n\n----\n## How to get this script\n```sh\ngit clone https://github.com/yousafkhamza/retention-based-cleanup-script.git\ncd retention-based-cleanup-script\nchmod +x cleanup*.sh\n```\n\n## How to use and sample using templates with output\n#### _Method 1 - (Standard)_\n```sh\nyousaf@MSI:/mnt/c/Users/Downloads$ bash cleanup.sh\nEnter your directory path: ./wordpress                    \u003c--------- Enter your absolute path of that directory you need\nEnter retension period: 2200                              \u003c-------- Retention period\n--------------------------------------------------------------\nThe below-listed files are older than 2200 days under this directory ./wordpress\n--------------------------------------------------------------\n./wordpress/wp-admin/css/farbtastic.css\n./wordpress/wp-admin/images/browser.png\n./wordpress/wp-admin/images/comment-grey-bubble-2x.png\n\nDo you want to remove these above-listed files? (Please verify it before) [Y/N]: y                                  \u003c------------------ Ask you a confirmation before deleting\n\nDeleting ./wordpress/wp-admin/css/farbtastic.css\nDeleting ./wordpress/wp-admin/images/browser.png\nDeleting ./wordpress/wp-admin/images/comment-grey-bubble-2x.png\n```\n#### _Method 2 - (Command-line value passing)_\n```sh\nyousaf@MSI:/mnt/c/Users/Downloads$ bash cleanup_cmd_variable_passing.sh ./wordpresss/ 4000                          \u003c---------- First value should be absalute path and second one is the retetion period\n--------------------------------------------------------------\nThe below-listed files are older than 2200 days under this directory ./wordpress\n--------------------------------------------------------------\n./wordpress/wp-admin/css/farbtastic.css\n./wordpress/wp-admin/images/browser.png\n./wordpress/wp-admin/images/comment-grey-bubble-2x.png\n\nDo you want to remove these above-listed files? (Please verify it before) [Y/N]: y                                  \u003c------------------ Ask you a confirmation before deleting\n\nDeleting ./wordpress/wp-admin/css/farbtastic.css\nDeleting ./wordpress/wp-admin/images/browser.png\nDeleting ./wordpress/wp-admin/images/comment-grey-bubble-2x.png\n```\n\n#### _Method 3 - (For Cronjob and Commandline script (without asking any confirmation before deleting))_\n```sh\nyousaf@MSI:/mnt/c/Users/Downloads$ ./cleanup_for_cron.sh ./wordpress/ 2000\n--------------------------------------------------------------\nThe below-listed files are older than 2000 days under this directory ./wordpress/\n--------------------------------------------------------------\n./wordpress/wp-admin/css/farbtastic.css\n./wordpress/wp-admin/images/browser.png\n./wordpress/wp-admin/images/comment-grey-bubble-2x.png\n\n!...........................WARNING..............................!\nWe are going to start deleting these above files within 10sec....\nif you do not need to execute this purging task, please quite this script on here using with [ctrl + c]\n!...........................WARNING..............................!\n\nDeleting ./wordpress/wp-admin/css/farbtastic.css\nDeleting ./wordpress/wp-admin/images/browser.png\nDeleting ./wordpress/wp-admin/images/comment-grey-bubble-2x.png\n```\n#### CronJob Example\n```sh\ncrontab -e\n0 3 * * * /path/to/cleanup_for_cron.sh /path/to/location/ 30                    \u003c---------------------- Run cleanup_for_cron.sh at 3 am every day\n```\n\u003e Who you guys need to set up an automation backup/log removing as per the retention period please use the script with [cronjob](https://www.tecmint.com/create-and-manage-cron-jobs-on-linux/).\n\n----\n## Architecture\n![alt text](https://i.ibb.co/VBV9dQJ/arch.jpg)\n\n----\n## Behind the Code\n_vim cleanup.sh_\n```sh\n#!/bin/bash\n\nread -p \"Enter your directory: \" directory\nread -p \"Enter retension period: \" rp\n\nrm -f ./delcleanup.txt\nif [[ -z \"$directory\" || -z  \"$rp\" ]]\nthen\n    echo \"Please specify a valid absalute direcotry path and retionsion period!\"\n    exit 1\nelse\n    if ! [[ \"$directory\" =~ ^[[:alnum:]]*$ || \"$rp\" =~ [^0-9] ]];\n    then\n        if [[ -d \"$directory\" ]];\n        then\n            echo \"--------------------------------------------------------------\"\n            echo \"The below-listed files are older than $rp days under this directory $directory\"\n            echo \"--------------------------------------------------------------\"\n            find \"$directory\" -type f -iname \"*\" -mtime +\"$rp\" -exec ls {} \\; | tee -a ./delcleanup.txt\n            echo \"\"\n            if [ -s \"./delcleanup.txt\" ]\n            then\n                read -p \"Do you want to remove these above-listed files? (Please verify it before) [Y/N]: \" con\n                echo \"\"\n                    if [[ \"$con\" =~ ^([yY][eE][sS]|[yY])+$ ]]\n                    then\n                        for delete in $(cat ./delcleanup.txt); do echo \"Deleting $delete\"; rm -f $delete ; done;\n                        rm -f ./delcleanup.txt\n                    else\n                        rm -f ./delcleanup.txt\n                    fi\n            else\n                echo \"There is no files found with this $rp days retention period!\"\n                rm -f ./delcleanup.txt\n            fi\n        else\n            echo \"No such direcotry found!\"\n        fi\n    else\n        echo \"Please enter a valid path and period!\"\n        exit 1\n    fi\nfi\n```\n\n----\n## Conclusion\n\nIt'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.  \n\n### ⚙️ Connect with Me \n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"mailto:yousaf.k.hamza@gmail.com\"\u003e\u003cimg src=\"https://img.shields.io/badge/Gmail-D14836?style=for-the-badge\u0026logo=gmail\u0026logoColor=white\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://www.linkedin.com/in/yousafkhamza\"\u003e\u003cimg src=\"https://img.shields.io/badge/LinkedIn-0077B5?style=for-the-badge\u0026logo=linkedin\u0026logoColor=white\"/\u003e\u003c/a\u003e \n\u003ca href=\"https://www.instagram.com/yousafkhamza\"\u003e\u003cimg src=\"https://img.shields.io/badge/Instagram-E4405F?style=for-the-badge\u0026logo=instagram\u0026logoColor=white\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://wa.me/%2B917736720639?text=This%20message%20from%20GitHub.\"\u003e\u003cimg src=\"https://img.shields.io/badge/WhatsApp-25D366?style=for-the-badge\u0026logo=whatsapp\u0026logoColor=white\"/\u003e\u003c/a\u003e\u003cbr /\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyousafkhamza%2Fretention-based-cleanup-script","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyousafkhamza%2Fretention-based-cleanup-script","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyousafkhamza%2Fretention-based-cleanup-script/lists"}