-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-del-log.sh
executable file
·86 lines (78 loc) · 1.99 KB
/
docker-del-log.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
###############################################################################
#
# Author: Akim Sissaoui
# Website: https://akim.sissaoui.com/en/informatique
#
# Description:
# This script uses jq to parse docker Id out of docker container inspect
# and delete the log file of a specific container
#
# Requirement:
# The scripts needs jq (https://stedolan.github.io/jq/)
# User must be allowed sudoer
#
# Usage:
# ./docker-del-log.sh [container name]
#
# Example:
#
# Below example will delete the log for a container called hass
#
# sudo ./docker-del-log.sh hass
#
#
###############################################################################
# Check if value has been provided
# Parse parameters
dockername=$1
noconfirm=$2
# Check if docker name has been provided
if [ -z $dockername ]
then
echo "Container name is required. Exiting"
exit 1
fi
#Parse container ID
container_id=`echo $(docker container inspect hass | jq -r '.[]."Id"';printf '%s\n')`
# Check if docker exists
exists=$(echo $container_id | wc -l)
if [ $exists -eq "0" ]
then
echo The container $dockername does has not been found. Exiting...
exit 1
elif [ $exists -gt "1" ]
then
echo Something weird happened. More than one container with the name $dockername have been found... Exiting...
exit 1
fi
# Set log path variable
logpath="/var/lib/docker/containers/$container_id/$container_id-json.log"
# Check if log exists
if [ -f $logpath ]
then
echo "Log file found."
echo
else
echo "Log file not found. Please check container name and configuration. Exiting."
exit 1
fi
# Ask for user confirmation
read -p "Container $dockername has ID #$container_id. Delete the log ?" -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
rm -f $logpath
success=$?
if [[ $success -eq "0" ]]
then
echo "File successfully deleted"
exit 0
else
echo "Something went wrong. Check if file $logpath exists."
exit 1
fi
else
echo Action canceled by user
exit 1
fi
exit 0