Skip to content

Instantly share code, notes, and snippets.

@samarpanda
Created December 21, 2024 17:00

Revisions

  1. samarpanda revised this gist Dec 21, 2024. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion redis-persisted-data-start.sh
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,9 @@

    # Configuration
    ENV=local
    PROJECT=demo
    IMAGE=redis:7.4.0
    CNAME=redis-$ENV-ic
    CNAME=redis-$ENV-$PROJECT
    PORT=6379
    DATA_DIR=$PWD/data

  2. samarpanda created this gist Dec 21, 2024.
    69 changes: 69 additions & 0 deletions redis-persisted-data-start.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    #!/bin/bash

    # Configuration
    ENV=local
    IMAGE=redis:7.4.0
    CNAME=redis-$ENV-ic
    PORT=6379
    DATA_DIR=$PWD/data

    # Helper function to display usage
    usage() {
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    }

    # Start Redis container
    start_redis() {
    echo "Starting Redis container: $CNAME"
    docker pull $IMAGE
    docker stop --time=20 $CNAME 2>/dev/null || true
    docker rm -f $CNAME 2>/dev/null || true
    if [ ! -d "$DATA_DIR" ]; then
    mkdir -p $DATA_DIR
    echo "Created data directory: $DATA_DIR"
    fi
    docker run -d \
    --name $CNAME \
    -p $PORT:6379 \
    -v $DATA_DIR:/data \
    $IMAGE redis-server --appendonly yes
    echo "Redis container started on port $PORT"
    }

    # Stop Redis container
    stop_redis() {
    echo "Stopping Redis container: $CNAME"
    docker stop --time=20 $CNAME
    docker rm -f $CNAME
    echo "Redis container stopped and removed"
    }

    # Restart Redis container
    restart_redis() {
    echo "Restarting Redis container: $CNAME"
    stop_redis
    start_redis
    }

    # Main script logic
    if [ "$#" -ne 1 ]; then
    usage
    fi

    case "$1" in
    start)
    start_redis
    ;;
    stop)
    stop_redis
    ;;
    restart)
    restart_redis
    ;;
    *)
    usage
    ;;
    esac

    exit 0