Projet

Général

Profil

Paste
Télécharger (1,46 ko) Statistiques
| Branche: | Révision:

root / bin / flush-files.sh @ f8e16685

1
#!/bin/sh
2

    
3
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

    
40
# Check if scripts-utils.sh is imported.
41
if [ -z "${scripts_utils}" ] ; then
42
    echo "Import of scripts-utils.sh required."
43
    . /home/assos/bin/scripts-utils.sh
44
fi
45

    
46

    
47
# Must not be quoted to avoid problem with ((…))
48
backups_number=$(ls | wc -l)
49
number_of_backups_to_delete=$((backups_number - ${number_of_files_to_keep}))
50

    
51
if [ "${number_of_backups_to_delete}" -gt 0 ] ; then
52
    ls | head "-${number_of_backups_to_delete}" | xargs rm
53
else
54
    if [ -n "${email_address}" ] ; then
55
        dir=$(pwd)
56
        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
    fi
58
fi