Created
May 9, 2025 18:27
-
-
Save pr0way/0684ec837eb2f6e3cab83ad1181f14f3 to your computer and use it in GitHub Desktop.
Stupid Simple Backup Solution - one-time bash script to backup all your user data folders at once.
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
| #!/usr/bin/env bash | |
| # Double compression doesn't make sense, leave as it is | |
| final_backup_file="backup.tar" | |
| home_dir="/home/$(whoami)" | |
| # Enter to the user directory | |
| cd "$home_dir" | |
| # Create temporary directory to group all archives | |
| temp_dir=$(mktemp -d) | |
| if [ ! -d "$temp_dir" ]; then | |
| echo "I cannot create a temporary directory" | |
| exit 1 | |
| fi | |
| # Go through all directories, exclude hiddens and current dir | |
| find . -maxdepth 1 -type d -not -path '*/.*' -not -path '.' -print0 | while IFS= read -r -d '' current_dir; do | |
| # Get the directory name | |
| base_name=$(basename "$current_dir") | |
| echo "Found a new folder: [$base_name]" | |
| # Check if the directory contains any files | |
| if find "$current_dir" -mindepth 1 -print -quit | grep -q .; then | |
| # Go inside | |
| cd "$current_dir" || { echo "I cannot go to $current_dir"; exit 1; } | |
| # Create archive | |
| tar -czf "${temp_dir}/${base_name}.tar.gz" * | |
| # Get back | |
| cd "$home_dir" || { echo "I cannot go to home directory"; exit 1; } | |
| else | |
| echo "Directory $current_dir is empty, omit." | |
| fi | |
| done | |
| # Go to temp | |
| cd "$temp_dir" || { echo "I cannot go to $temp_dir"; exit 1; } | |
| # Create final archive | |
| tar -cf "$home_dir/$final_backup_file" *.tar.gz && echo "New archive was created: $home_dir/$final_backup_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment