forked from colin/disk-space-report
				
			Update swap.sh
This commit is contained in:
		
							parent
							
								
									a08f30fa56
								
							
						
					
					
						commit
						115a538790
					
				| 
						 | 
					@ -1,91 +0,0 @@
 | 
				
			||||||
#!/bin/bash
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
echo "Starting disk space report... This may take a few minutes."
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Function to estimate the size of the apt cache
 | 
					 | 
				
			||||||
estimate_apt_cache_size() {
 | 
					 | 
				
			||||||
    echo "Estimating the size of the apt cache:"
 | 
					 | 
				
			||||||
    sudo du -sh /var/cache/apt
 | 
					 | 
				
			||||||
    echo
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Function to estimate the size of old installed packages
 | 
					 | 
				
			||||||
estimate_old_packages_size() {
 | 
					 | 
				
			||||||
    echo "Estimating the size of old installed packages:"
 | 
					 | 
				
			||||||
    sudo du -sh /var/lib/apt/lists /var/lib/apt/lists/partial
 | 
					 | 
				
			||||||
    echo
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Function to estimate the size of journal logs
 | 
					 | 
				
			||||||
estimate_journal_size() {
 | 
					 | 
				
			||||||
    echo "Estimating the size of journal logs:"
 | 
					 | 
				
			||||||
    sudo journalctl --disk-usage
 | 
					 | 
				
			||||||
    echo
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Function to estimate the size of temporary files
 | 
					 | 
				
			||||||
estimate_tmp_size() {
 | 
					 | 
				
			||||||
    echo "Estimating the size of temporary files:"
 | 
					 | 
				
			||||||
    sudo du -sh /tmp /var/tmp
 | 
					 | 
				
			||||||
    echo
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Function to estimate the size of unused Docker volumes
 | 
					 | 
				
			||||||
estimate_docker_volumes_size() {
 | 
					 | 
				
			||||||
    echo "Estimating the size of unused Docker volumes:"
 | 
					 | 
				
			||||||
    docker volume ls -qf dangling=true | xargs -I {} docker volume inspect --format '{{ .Mountpoint }}' {} | xargs -I {} sudo du -sh {} | awk '{ sum += $1 } END { print sum "B" }'
 | 
					 | 
				
			||||||
    echo
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Function to check and suggest logrotate for large Docker logs
 | 
					 | 
				
			||||||
check_docker_logs() {
 | 
					 | 
				
			||||||
    echo "Checking Docker logs for large files..."
 | 
					 | 
				
			||||||
    large_logs=$(docker ps -q --filter "status=exited" | xargs -I {} docker inspect --format '{{.LogPath}}' {} | xargs -I {} sudo find {} -type f -size +1G)
 | 
					 | 
				
			||||||
    if [ -n "$large_logs" ]; then
 | 
					 | 
				
			||||||
        echo "The following Docker logs are larger than 1GB:"
 | 
					 | 
				
			||||||
        echo "$large_logs"
 | 
					 | 
				
			||||||
        echo
 | 
					 | 
				
			||||||
        echo "Consider setting up logrotate to manage Docker logs."
 | 
					 | 
				
			||||||
        echo "To truncate all Docker logs, run:"
 | 
					 | 
				
			||||||
        echo 'sudo find /var/lib/docker/containers/ -type f -name "*.log" -exec truncate -s 0 {} \;'
 | 
					 | 
				
			||||||
        echo
 | 
					 | 
				
			||||||
    else
 | 
					 | 
				
			||||||
        echo "No large Docker logs found."
 | 
					 | 
				
			||||||
        echo
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Function to list directories consuming more than a specified size
 | 
					 | 
				
			||||||
list_large_directories() {
 | 
					 | 
				
			||||||
    local directory=$1
 | 
					 | 
				
			||||||
    local size_limit=$2
 | 
					 | 
				
			||||||
    echo "Directories in $directory consuming more than ${size_limit}GB:"
 | 
					 | 
				
			||||||
    sudo du -ahx $directory 2>/dev/null | awk -v limit=$size_limit '{
 | 
					 | 
				
			||||||
        size=$1; unit=substr(size, length(size));
 | 
					 | 
				
			||||||
        size_val=substr(size, 1, length(size)-1);
 | 
					 | 
				
			||||||
        if ((unit=="G" && size_val+0 > limit) || (unit=="T" && size_val*1024 > limit)) {
 | 
					 | 
				
			||||||
            print
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }'
 | 
					 | 
				
			||||||
    echo
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Estimate storage savings
 | 
					 | 
				
			||||||
echo "Estimating potential storage savings..."
 | 
					 | 
				
			||||||
estimate_apt_cache_size
 | 
					 | 
				
			||||||
estimate_old_packages_size
 | 
					 | 
				
			||||||
estimate_journal_size
 | 
					 | 
				
			||||||
estimate_tmp_size
 | 
					 | 
				
			||||||
estimate_docker_volumes_size
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Check Docker logs
 | 
					 | 
				
			||||||
check_docker_logs
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# List large directories
 | 
					 | 
				
			||||||
echo "Listing directories consuming more than 5GB and 10GB:"
 | 
					 | 
				
			||||||
list_large_directories / 5
 | 
					 | 
				
			||||||
list_large_directories /home 5
 | 
					 | 
				
			||||||
list_large_directories / 10
 | 
					 | 
				
			||||||
list_large_directories /home 10
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
echo "Storage savings estimation and large directory listing completed."
 | 
					 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,40 @@
 | 
				
			||||||
 | 
					#!/bin/bash
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Detect the main disk by finding the partition mounted on root ('/')
 | 
				
			||||||
 | 
					main_disk=$(df / | tail -1 | awk '{print $1}')
 | 
				
			||||||
 | 
					mount_point=$(df / | tail -1 | awk '{print $6}')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Get available space on the partition in GB
 | 
				
			||||||
 | 
					available_space=$(df -BG $mount_point | tail -1 | awk '{print $4}' | sed 's/G//')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Calculate 5% of the available space, and ensure it's within the limits (max 30GB)
 | 
				
			||||||
 | 
					swap_size=$((available_space * 5 / 100))
 | 
				
			||||||
 | 
					if [ $swap_size -gt 30 ]; then
 | 
				
			||||||
 | 
					    swap_size=30
 | 
				
			||||||
 | 
					elif [ $swap_size -lt 1 ]; then
 | 
				
			||||||
 | 
					    echo "Not enough disk space to create a swap file."
 | 
				
			||||||
 | 
					    exit 1
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					echo "Creating a ${swap_size}GB swap file at /swapfile..."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Create the swap file
 | 
				
			||||||
 | 
					sudo fallocate -l ${swap_size}G /swapfile
 | 
				
			||||||
 | 
					sudo chmod 600 /swapfile
 | 
				
			||||||
 | 
					sudo mkswap /swapfile
 | 
				
			||||||
 | 
					sudo swapon /swapfile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					echo "Swap file of ${swap_size}GB created and activated."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Ask if the user wants to persist the swap after reboot
 | 
				
			||||||
 | 
					read -p "Do you want to persist the swap file after reboot? (y/n): " persist_swap
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [[ "$persist_swap" == "y" || "$persist_swap" == "Y" ]]; then
 | 
				
			||||||
 | 
					    echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
 | 
				
			||||||
 | 
					    echo "Swap file added to /etc/fstab."
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
					    echo "Swap file will not persist after reboot."
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Show swap summary
 | 
				
			||||||
 | 
					sudo swapon --show
 | 
				
			||||||
		Loading…
	
		Reference in New Issue