Révision 15ed27d8
Ajouté par Julien Enselme il y a plus de 9 ans
bin/flush-files.sh | ||
---|---|---|
1 | 1 |
#!/bin/sh |
2 | 2 |
|
3 |
help="# ARGS: number of files to keep, [email address]\n |
|
4 |
# usage: flush files from a folder except a number equal to argument.\n |
|
5 |
# It only prints a warning if scripts-utils.sh is not imported in order to be used |
|
6 |
# from the terminal." |
|
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 |
|
|
7 | 39 |
|
8 | 40 |
# Check if scripts-utils.sh is imported. |
9 | 41 |
if [ -z "${scripts_utils}" ] ; then |
... | ... | |
15 | 47 |
|
16 | 48 |
# Must not be quoted to avoid problem with ((…)) |
17 | 49 |
backups_number=$(ls | wc -l) |
18 |
number_of_backups_to_delete=$((backups_number - $1))
|
|
50 |
number_of_backups_to_delete=$((backups_number - ${number_of_files_to_keep}))
|
|
19 | 51 |
|
20 | 52 |
if [ "${number_of_backups_to_delete}" -gt 0 ] ; then |
21 | 53 |
ls | head "-${number_of_backups_to_delete}" | xargs rm |
22 | 54 |
else |
23 |
if [ -n "$2" ] ; then
|
|
55 |
if [ -n "${email_address}" ] ; then
|
|
24 | 56 |
dir=$(pwd) |
25 |
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" "$2"
|
|
57 |
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}"
|
|
26 | 58 |
fi |
27 | 59 |
fi |
Formats disponibles : Unified diff
Use getopts to print help and to parse script arguments.