Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Add Comment
Please, Sign In to add comment