Last active
August 6, 2022 22:22
-
-
Save Farid-NL/4975e2918d1e10c65844b428b59b18ad to your computer and use it in GitHub Desktop.
Script for restoring and backing up dot files
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 | |
| ## ______ _ _ | |
| ## | ____| (_) | | | |
| ## | |__ __ _ _ __ _ __| | | |
| ## | __|/ _` || '__|| | / _` | | |
| ## | | | (_| || | | || (_| | | |
| ## |_| \__,_||_| |_| \__,_| | |
| ## @author Carlos Farid Nogales López | |
| ## @since 2020-09-07 | |
| ## @brief Script that backup old dot files (if any) and place the current dot files | |
| BOLD=$(tput bold) | |
| NORMAL=$(tput sgr0) | |
| DIR=$(readlink -f "$1") | |
| DEFAULT=$HOME | |
| # Files and dirs to be backed up. Check the dot files repo, it must be the same. | |
| FILES_AND_DIRS=( | |
| ".zshrc" | |
| ".zshenv" | |
| ".gitconfig" | |
| ".p10k.zsh" | |
| "README.md" | |
| ".scripts" | |
| ".config/nvim" | |
| ".config/zsh" | |
| ".config/yakuakerc" | |
| ".icons/Dracula-cursors" | |
| ".local/share/konsole" | |
| ) | |
| # Choose where the dot files will be placed (Default: ~) | |
| if [ $# -eq 0 ]; then | |
| echo "No argument provided." | |
| echo -n "Do you want to use the default directory '$DEFAULT'? [Y/n]: " | |
| read -r CHOOSE | |
| if [ "$CHOOSE" = Y ] || [ "$CHOOSE" = y ] || [ "$CHOOSE" = "" ]; then | |
| DIR=$DEFAULT | |
| else | |
| echo -e "\nPut the target directory where the ${BOLD}dot files ${NORMAL}will be placed " | |
| echo "Normally is ${BOLD}\$HOME ${NORMAL}or ${BOLD}~" | |
| exit 1 | |
| fi | |
| fi | |
| if [ ! -d "$1" ]; then | |
| echo "Invalid argument: Not a directory" | |
| exit 1 | |
| fi | |
| # Handlement of .gitignore | |
| [ ! -f "$DIR"/.gitignore ] && touch "$DIR"/.gitignore | |
| if grep -Fxq ".dot-config" "$DIR/.gitignore"; then | |
| echo -e ".gitignore file ${BOLD}already set\n${NORMAL}" | |
| else | |
| echo ".dot-config" >> "$DIR"/.gitignore | |
| echo -e ".gitignore file ${BOLD}set\n${NORMAL}" | |
| fi | |
| ## Backup files if needed | |
| ## @arg $1 Relative path of the file(s) | |
| backup() { | |
| TARGET_DIR=$DIR/dot-files-bak | |
| if [ -f "$DIR/$1" ]; then | |
| [ ! -d "$TARGET_DIR" ] && mkdir -p "$TARGET_DIR" | |
| mv "$DIR/$1" "$TARGET_DIR/$1" | |
| elif [ -d "$DIR/$1" ]; then | |
| [[ "$1" == *"/"* ]] && TARGET_DIR="$TARGET_DIR/${1%/*}" | |
| [ ! -d "$TARGET_DIR" ] && mkdir -p "$TARGET_DIR" | |
| mv "$DIR/$1" "$TARGET_DIR" | |
| fi | |
| } | |
| # Backing up | |
| for i in "${FILES_AND_DIRS[@]}"; do | |
| backup "$i" | |
| done | |
| [ -d "$DIR"/dot-files-bak ] && echo "Previous files backed up in ${BOLD}$DIR/dot-files-bak" | |
| # Cloning and checking out [Change the repo url] | |
| if [ ! -d "$DIR"/.dot-config/ ]; then | |
| git clone --quiet --bare [email protected]:FaruNL/dotfiles.git "$DIR"/.dot-config/ | |
| git --git-dir="$DIR"/.dot-config/ --work-tree="$DIR" checkout | |
| else | |
| git --git-dir="$DIR"/.dot-config/ --work-tree="$DIR" restore "${FILES_AND_DIRS[@]/#/$DIR\/}" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment