Created
November 23, 2017 08:02
-
-
Save vermauv/7cd0dd83c9593dc2d32f2ae443b203db to your computer and use it in GitHub Desktop.
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 | |
# | |
# Delete files older than some time | |
# | |
usage() | |
{ | |
cat << EOF | |
usage $0 options | |
OPTIONS | |
-p path | |
-n name regex | |
-u unit of time | |
Example: ./clean-backups.sh -p /Users/foo/websites/backup/files -n '*.sql.gz' -u 30d | |
-n : True if the last component of the pathname being examined matches pattern | |
-u : Possible units of time: | |
s second | |
m minute (60 seconds) | |
h hour (60 minutes) | |
d day (24 hours) | |
w week (7 days) | |
Any number of units may be combined in one argument, for example, "1h30m". | |
EOF | |
} | |
path='' | |
unitoftime='' | |
regex='*.sql.gz' | |
if [ $# -eq 0 ]; | |
then | |
usage | |
exit 1 | |
fi | |
while getopts "hp:n:u:" opt | |
do | |
case $opt in | |
h) | |
usage | |
exit 1 | |
;; | |
p) | |
echo "Path : $OPTARG" | |
path=$OPTARG | |
;; | |
n) | |
echo "Name Regex : $OPTARG" | |
regex=$OPTARG | |
;; | |
u) | |
echo "Unit of Time : $OPTARG" | |
unitoftime=$OPTARG | |
;; | |
\?) | |
echo "Invalid option: $OPTARG" >&2 | |
usage | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
usage | |
exit 1 | |
;; | |
esac | |
done | |
if [ ! -z $path ] && [ ! -z $regex ] && [ ! -z $unitoftime ]; | |
then | |
if [ ! -d $path ] | |
then | |
echo "Directory Not Found!!!" | |
exit 1 | |
fi | |
log=$(date +"%d-%m-%Y-%H-%M-%S")_backup_files_deleted.log; | |
echo "find ${path} -type f -name ${regex} -Btime +${unitoftime}" | |
find ${path} -type f -name ${regex} -Btime +${unitoftime} > $path/$log | |
find ${path} -type f -name ${regex} -Btime +${unitoftime} -exec rm {} \; | |
echo "Cleaned the backups..!" | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment