-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.sh
More file actions
executable file
·211 lines (181 loc) · 5.63 KB
/
utils.sh
File metadata and controls
executable file
·211 lines (181 loc) · 5.63 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env bash
# Colors for better output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Print a colored message
function print_message() {
echo -e "${GREEN}[+] $1${NC}"
}
# Print a warning message
function print_warning() {
echo -e "${YELLOW}[!] $1${NC}"
}
# Print an error message
function print_error() {
echo -e "${RED}[-] $1${NC}"
}
# Check if Docker Compose is available (either V1 or V2)
DOCKER_COMPOSE=""
if command -v docker-compose &> /dev/null; then
DOCKER_COMPOSE="docker-compose"
print_message "Docker Compose V1 detected."
elif docker compose version &> /dev/null; then
DOCKER_COMPOSE="docker compose"
print_message "Docker Compose V2 detected."
else
print_error "Docker Compose is not installed. Please install Docker Compose first."
exit 1
fi
# Show help message
function show_help() {
echo "Utility Script for Docker Environment"
echo ""
echo "Usage: $0 [command]"
echo ""
echo "Commands:"
echo " status - Show status of all containers"
echo " start - Start all containers"
echo " stop - Stop all containers"
echo " restart - Restart all containers"
echo " logs [name] - Show logs for a specific container (or all if no name provided)"
echo " shell [name] - Open a shell in a specific container"
echo " update - Update all containers"
echo " backup - Create a backup of all volumes"
echo " restore - Restore backup of all volumes"
echo " help - Show this help message"
}
# Show status of all containers
function show_status() {
print_message "Showing status of all containers..."
$DOCKER_COMPOSE ps
}
# Start all containers
function start_containers() {
print_message "Starting all containers..."
$DOCKER_COMPOSE up -d
}
# Stop all containers
function stop_containers() {
print_message "Stopping all containers..."
$DOCKER_COMPOSE down
}
# Restart all containers
function restart_containers() {
print_message "Restarting all containers..."
$DOCKER_COMPOSE restart
}
# Show logs for a specific container
function show_logs() {
if [[ -z "$1" ]]; then
print_message "Showing logs for all containers..."
$DOCKER_COMPOSE logs
else
print_message "Showing logs for container $1..."
$DOCKER_COMPOSE logs "$1"
fi
}
# Open a shell in a specific container
function open_shell() {
if [[ -z "$1" ]]; then
print_error "Please specify a container name."
return 1
else
print_message "Opening shell in container $1..."
docker exec -it "$1" bash || docker exec -it "$1" sh
fi
}
# Update all containers
function update_containers() {
print_message "Updating all containers..."
$DOCKER_COMPOSE pull
$DOCKER_COMPOSE up -d
}
# Create a backup of all volumes
function backup_volumes() {
local backup_dir="backup_$(date +%Y%m%d_%H%M%S)"
print_message "Creating backup of all volumes in directory $backup_dir..."
mkdir -p "$backup_dir"
# Stop containers
$DOCKER_COMPOSE down
# Backup each volume
for volume in kasm_db_1.15.0 portainer_data evilginx2_workspace_data gophish_workspace_data evilginx2_data gophish_data axiom_data; do
print_message "Backing up volume $volume..."
docker run --rm -v "$volume:/source" -v "$(pwd)/$backup_dir:/backup" alpine tar -czf "/backup/$volume.tar.gz" -C /source .
done
# Restart containers
$DOCKER_COMPOSE up -d
print_message "Backup completed successfully in directory $backup_dir."
}
# Restore backup of all volumes
function restore_volumes() {
print_message "Available backups:"
local backups=(backup_*)
if [[ ${#backups[@]} -eq 0 ]]; then
print_error "No backups found."
return 1
fi
for i in "${!backups[@]}"; do
echo "$((i+1)). ${backups[$i]}"
done
echo "Enter the number of the backup to restore:"
read -r backup_num
if [[ ! "$backup_num" =~ ^[0-9]+$ ]] || [[ "$backup_num" -lt 1 ]] || [[ "$backup_num" -gt ${#backups[@]} ]]; then
print_error "Invalid selection."
return 1
fi
local backup_dir="${backups[$((backup_num-1))]}"
print_message "Restoring backup from directory $backup_dir..."
# Stop containers
$DOCKER_COMPOSE down
# Restore each volume
for volume in kasm_db_1.15.0 portainer_data evilginx2_workspace_data gophish_workspace_data evilginx2_data gophish_data axiom_data; do
if [[ -f "$backup_dir/$volume.tar.gz" ]]; then
print_message "Restoring volume $volume..."
docker run --rm -v "$volume:/destination" -v "$(pwd)/$backup_dir:/backup" alpine sh -c "rm -rf /destination/* && tar -xzf /backup/$volume.tar.gz -C /destination"
else
print_warning "Backup for volume $volume not found. Skipping..."
fi
done
# Restart containers
$DOCKER_COMPOSE up -d
print_message "Restore completed successfully."
}
# Main function
function main() {
case "$1" in
status)
show_status
;;
start)
start_containers
;;
stop)
stop_containers
;;
restart)
restart_containers
;;
logs)
show_logs "$2"
;;
shell)
open_shell "$2"
;;
update)
update_containers
;;
backup)
backup_volumes
;;
restore)
restore_volumes
;;
help|*)
show_help
;;
esac
}
# Execute main function
main "$@"