@@ -6,18 +6,33 @@ CONTAINER_NAME="influxdb"
66NETWORK_NAME=" influx_network"
77DATA_DIR=" ./influxdb_data"
88INFLUXDB_USER=" admin" # Change this to your desired username
9- INFLUXDB_PASSWORD=" admin_password" # Change this to your desired password
10- INFLUXDB_ORG=" systemguard" # Change this to your desired organization name
9+ INFLUXDB_PASSWORD=" admin_password" # Change this to your desired password
10+ INFLUXDB_ORG=" systemguard" # Change this to your desired organization name
1111INFLUXDB_BUCKET=" system_metrics" # Change this to your desired initial bucket (database) name
12- # demo influxdb token | change this to your own token
13- INFLUXDB_TOKEN=" "
12+ INFLUXDB_TOKEN=" " # Demo InfluxDB token | Change this to your own token
1413
1514# Export the INFLUXDB_TOKEN to be used later in your app
16- export " INFLUXDB_TOKEN" =$INFLUXDB_TOKEN
15+ export INFLUXDB_TOKEN=" $INFLUXDB_TOKEN "
16+
17+ # Function to get the appropriate username, even when running as root
18+ get_user_name () {
19+ if [ " $( whoami) " = " root" ]; then
20+ LOGNAME_USER=$( logname 2> /dev/null)
21+ if [ $? -ne 0 ]; then
22+ USER_NAME=$( cat /etc/passwd | grep ' /home' | cut -d: -f1 | tail -n 1)
23+ else
24+ USER_NAME=$LOGNAME_USER
25+ fi
26+ else
27+ USER_NAME=$( whoami)
28+ fi
29+ echo " $USER_NAME "
30+ }
31+
32+ USER_NAME=$( get_user_name)
1733
1834# Create a data directory for InfluxDB
19- rm -rf $DATA_DIR
20- mkdir -p $DATA_DIR
35+ sudo -u " $USER_NAME " mkdir -p " $DATA_DIR "
2136
2237# Stop and remove existing InfluxDB container if it exists
2338if [ " $( docker ps -aq -f name=$CONTAINER_NAME ) " ]; then
@@ -33,7 +48,7 @@ if sudo lsof -i :8086; then
3348 sudo fuser -k 8086/tcp
3449fi
3550
36- # Create a Docker network
51+ # Create a Docker network if it doesn't exist
3752echo " Creating Docker network..."
3853docker network create $NETWORK_NAME || true # Avoid error if the network already exists
3954
@@ -48,7 +63,7 @@ docker run -d \
4863 --network $NETWORK_NAME \
4964 --restart=always \
5065 -p 8086:8086 \
51- -v $PWD /$DATA_DIR :/var/lib/influxdb2 \
66+ -v " $PWD /$DATA_DIR :/var/lib/influxdb2" \
5267 -e DOCKER_INFLUXDB_INIT_MODE=setup \
5368 -e DOCKER_INFLUXDB_INIT_USERNAME=$INFLUXDB_USER \
5469 -e DOCKER_INFLUXDB_INIT_PASSWORD=$INFLUXDB_PASSWORD \
@@ -57,6 +72,5 @@ docker run -d \
5772 -e DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=$INFLUXDB_TOKEN \
5873 influxdb:$INFLUXDB_VERSION
5974
60-
6175# Output completion message
6276echo " InfluxDB setup completed! Access it at http://localhost:8086 with your credentials."
0 commit comments