Project

General

Profile

Paste
Download (1.45 KB) Statistics
| Branch: | Revision:

root / bin / flush-files.sh @ eec59787

1 d52a5b2f jenselme
#!/bin/sh
2
3 15ed27d8 Julien Enselme
usage() {
4
    help="flush-files.sh -n number_of_files_to_keep [-m email_address]\n
5
usage: flush files from a folder except a number equal to argument.\n
6
It only prints a warning if scripts-utils.sh is not imported in order to be used
7
from the terminal."
8
    echo -e "${help}"
9
}
10
11
12
number_of_files_to_keep=''
13
email_address=''
14
while getopts "hn:m:" opt; do
15
    case "${opt}" in
16
	h)
17
	    usage; exit 0;;
18
	n)
19
	    number_of_files_to_keep="${OPTARG}";;
20
	m)
21
	    email_address="${OPTARG}";;
22
	:)
23
	    echo "Option -$OPTARG requires an argument." >&2
24
	    usage >&2; exit 1;;
25
	\?)
26
	    usage >&2; exit 1;;
27
    esac
28
done
29
shift $((OPTIND-1))
30
31
32
# Check that all required parameters are there
33
if [ -z "${number_of_files_to_keep}" ]; then
34
    echo "At least a required parameter is missing." >&2
35
    usage >&2
36
    exit 1
37
fi
38
39 d52a5b2f jenselme
40
# Check if scripts-utils.sh is imported.
41 3e9236bd Julien Enselme
if [ -z "${scripts_utils}" ] ; then
42 d52a5b2f jenselme
    echo "Import of scripts-utils.sh required."
43
    . scripts-utils.sh
44
fi
45
46
47 3e9236bd Julien Enselme
# Must not be quoted to avoid problem with ((…))
48 683f57f9 Julien Enselme
backups_number=$(ls | wc -l)
49 15ed27d8 Julien Enselme
number_of_backups_to_delete=$((backups_number - ${number_of_files_to_keep}))
50 d52a5b2f jenselme
51 3e9236bd Julien Enselme
if [ "${number_of_backups_to_delete}" -gt 0 ] ; then
52
    ls | head "-${number_of_backups_to_delete}" | xargs rm
53 d52a5b2f jenselme
else
54 15ed27d8 Julien Enselme
    if [ -n "${email_address}" ] ; then
55 8550e097 Julien Enselme
        dir=$(pwd)
56 15ed27d8 Julien Enselme
        echo "There are not enough files in $dir to Flush it. Check if backup script works fine." | mail -s "[db] $dir has a backup problem" "${email_address}"
57 d52a5b2f jenselme
    fi
58
fi