Last active
December 18, 2023 08:58
-
-
Save ajanderson1/674eb61095d55bb6c71fba8512f00623 to your computer and use it in GitHub Desktop.
Useful Scripts for working with Docker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script to create Docker volumes from .tar files | |
# It will list all .tar files in the current directory and allow the user to select the ones to import | |
# The script will create a Docker volume for each selected .tar file and populate it with the contents of the .tar file | |
# The volume will be named after the .tar file, with the .tar extension removed and any "_backup," "_tar," "_export," and "_volume" suffixes removed. | |
# ANSI color codes | |
RED='\033[1;31m' | |
YELLOW='\033[0;33m' | |
NC='\033[0m' # No Color | |
# Function to check if Docker daemon is running | |
check_docker_daemon() { | |
if ! docker info > /dev/null 2>&1; then | |
echo "Cannot connect to the Docker daemon. Is the docker daemon running?" | |
exit 1 | |
fi | |
} | |
# Function to create Docker volume and populate it from a selected .tar file | |
create_docker_volume() { | |
local tar_file="$1" | |
local volume_name="$2" | |
# Create a Docker volume with the specified name | |
docker volume create "$volume_name" | |
# Populate the Docker volume with the contents of the .tar file | |
docker run --rm -v "$volume_name":/target_volume -v "$(pwd)":/backup busybox tar xvf "/backup/$tar_file" -C /target_volume | |
} | |
# Check Docker daemon status | |
check_docker_daemon | |
echo "Available .tar Files:" | |
# Prepare a list of .tar files with indices | |
tar_files=(*.tar) | |
options=() | |
# Use fzf for file selection and store the selected items in an array | |
for i in "${!tar_files[@]}"; do | |
options+=("${tar_files[i]}") | |
done | |
IFS=$'\n' read -r -d '' -a selected_files <<< "$(printf "%s\n" "${options[@]}" | fzf --height 20% --ansi --multi --header="Select .tar Files:")" | |
# Output the selected .tar files | |
echo -e "${YELLOW}Selected .tar Files:${NC}" | |
for tar_file in "${selected_files[@]}"; do | |
volume_name="${tar_file%.tar}" # Remove the .tar extension | |
# Loop through possible suffixes and remove them if found | |
suffixes=("backup" "tar" "export" "volume") | |
for suffix in "${suffixes[@]}"; do | |
volume_name="${volume_name%%_$suffix}" | |
done | |
echo -e "${YELLOW}$tar_file${NC} -> Creating Docker volume: ${YELLOW}$volume_name${NC}" | |
create_docker_volume "$tar_file" "$volume_name" | |
done | |
# Output the names of the new volumes that have been created | |
echo -e "${RED}New Docker Volumes Created:${NC}" | |
for tar_file in "${selected_files[@]}"; do | |
volume_name="${tar_file%.tar}" # Remove the .tar extension | |
# Loop through possible suffixes and remove them if found | |
suffixes=("backup" "tar" "export" "volume") | |
for suffix in "${suffixes[@]}"; do | |
volume_name="${volume_name%%_$suffix}" | |
done | |
echo -e " - ${RED}$volume_name${NC}" | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Version 1.02 | |
# Script to export Docker volumes | |
# ANSI color codes | |
RED='\033[1;31m' | |
YELLOW='\033[0;33m' | |
NC='\033[0m' # No Color | |
# Variable to control the use of fzf | |
use_fzf_if_available=true | |
# Function to check if Docker daemon is running | |
check_docker_daemon() { | |
if ! docker info > /dev/null 2>&1; then | |
echo "Cannot connect to the Docker daemon. Is the docker daemon running?" | |
exit 1 | |
fi | |
} | |
# Check Docker daemon status | |
check_docker_daemon | |
echo "Available Docker Volumes:" | |
# Prepare a list of volumes with indices | |
volume_names=($(docker volume ls -q)) | |
options=() | |
# Use fzf if available and allowed | |
# Use fzf if available and allowed | |
if [[ $use_fzf_if_available = true ]] && command -v fzf &> /dev/null; then | |
for i in ${!volume_names[@]}; do | |
options+=("${volume_names[i]}") | |
done | |
# Use fzf for volume selection and store the selected items in an array | |
IFS=$'\n' read -r -d '' -a selected_volumes <<< "$(printf "%s\n" "${options[@]}" | fzf --height 20% --ansi --multi --header="Select Docker Volumes:")" | |
else | |
# fzf is not available or not allowed, use simple table-like format | |
echo "Index | Volume Name" | |
echo "------|------------" | |
for i in ${!volume_names[@]}; do | |
printf "%-6s| %s\n" "$((i+1))" "${volume_names[i]}" | |
done | |
# Prompt user to select volumes by index | |
read -p "Enter the index numbers of the volumes to export, separated by commas: " selected_indices | |
IFS=',' read -r -a indices <<< $selected_indices | |
selected_volumes=() | |
for index in ${indices[@]}; do | |
selected_volumes+=("${volume_names[$((index-1))]}") | |
done | |
fi | |
# Output the selected volumes | |
echo -e "${YELLOW}Selected Volumes:${NC}" | |
for volume in "${selected_volumes[@]}"; do | |
echo -e "${YELLOW}$volume${NC}" | |
done | |
# set a default destination directory | |
destination="$HOME" | |
# Prompt user for destination directory | |
read -p "Enter the destination directory (default: $destination):" | |
destination=${destination:-$HOME} | |
# Ensure the destination directory exists | |
mkdir -p $destination | |
# Function to export a volume | |
export_volume() { | |
local volume_name=$1 | |
local backup_filename="$destination/${volume_name}_backup.tar" | |
echo "Exporting volume $volume_name to $backup_filename..." | |
docker run --rm \ | |
-v "$volume_name:/volume" \ | |
-v "$destination:/backup" \ | |
busybox tar -C /volume -cf "/backup/${volume_name}_backup.tar" . | |
} | |
# Export each selected volume | |
for volume in "${selected_volumes[@]}"; do | |
export_volume "$volume" | |
done | |
# At the end of the script, confirm where the files have been saved | |
echo -e "${RED}Export process completed.${NC}" | |
echo -e "${RED}The backup tar files are saved in the following path${NC}" | |
for volume in "${selected_volumes[@]}"; do | |
echo -e " - ${RED}$destination/${volume}_backup.tar${NC}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment