Created
August 24, 2024 08:29
-
-
Save NorbertFeria/e2728ce4e16565e1d152608672b38258 to your computer and use it in GitHub Desktop.
Restore the backup files to a destination folder
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 | |
| # Default values | |
| DEFAULT_COMPRESSED_FILE="/backups/file.tar.gz" | |
| DEFAULT_DEST_DIR="/public_html" | |
| # Prompt for the compressed file | |
| read -p "Enter the path to the compressed file [${DEFAULT_COMPRESSED_FILE}]: " COMPRESSED_FILE | |
| COMPRESSED_FILE=${COMPRESSED_FILE:-$DEFAULT_COMPRESSED_FILE} | |
| # Check if the compressed file exists | |
| if [ ! -f "$COMPRESSED_FILE" ]; then | |
| echo "Error: File '$COMPRESSED_FILE' not found." | |
| exit 1 | |
| fi | |
| # Check if the file has a .tar.gz extension | |
| if [[ "$COMPRESSED_FILE" != *.tar.gz ]]; then | |
| echo "Error: The file '$COMPRESSED_FILE' is not a .tar.gz file." | |
| exit 1 | |
| fi | |
| # Prompt for the destination directory | |
| read -p "Enter the destination directory [${DEFAULT_DEST_DIR}]: " DEST_DIR | |
| DEST_DIR=${DEST_DIR:-$DEFAULT_DEST_DIR} | |
| # Check if the destination directory is writable or can be created | |
| if [ -d "$DEST_DIR" ]; then | |
| if [ ! -w "$DEST_DIR" ]; then | |
| echo "Error: The destination directory '$DEST_DIR' is not writable." | |
| exit 1 | |
| fi | |
| else | |
| echo "The destination directory '$DEST_DIR' does not exist. Creating it now..." | |
| mkdir -p "$DEST_DIR" | |
| if [ $? -ne 0 ]; then | |
| echo "Error: Could not create destination directory '$DEST_DIR'." | |
| exit 1 | |
| fi | |
| fi | |
| # Check if there is enough space to uncompress the file | |
| AVAILABLE_SPACE=$(df --output=avail "$DEST_DIR" | tail -1) | |
| REQUIRED_SPACE=$(du -s "$COMPRESSED_FILE" | cut -f1) | |
| if [ "$REQUIRED_SPACE" -gt "$AVAILABLE_SPACE" ]; then | |
| echo "Error: Not enough space to uncompress the file in '$DEST_DIR'." | |
| exit 1 | |
| fi | |
| # Uncompress the file to the destination directory without including the directory structure | |
| tar -xzvf "$COMPRESSED_FILE" -C "$DEST_DIR" | |
| # Check if the tar command was successful | |
| if [ $? -eq 0 ]; then | |
| echo "Files uncompressed successfully to ${DEST_DIR}" | |
| else | |
| echo "Error: Failed to uncompress the file." | |
| exit 1 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment