aboutsummaryrefslogtreecommitdiffstats
path: root/dot-backup.sh
blob: aef6fee18b15e0ac7b727fe41bf06289a19627dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#! /bin/bash

#####################################################################
#	     ___        __   ___                   __                 
#	  __| _/ ____ _/  |_ \_ |__ _____    ____ |  | ____ ________  
#	 / __ | / __ \\   __\ | __ \\__  \ _/ ___\|  |/ /  |  \____ \ 
#	/ /_/ |(  \_\ )|  |   | \_\ \/ __ \_  \___|    \|  |  /  |_\ \
#	\____ | \____/ |__|   |___  /____  /\___  /__|_ \____/|   ___/
#	     \/                   \/     \/     \/     \/     |__|    
#
#	by danix
#
#####################################################################

set -euo pipefail

# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

### DIRECTORIES
WORKDIR=$(pwd)
# Output directory
#DEFAULT_OUTPUT_DIR="${HOME}/Programming/GIT/my-dotfiles"
DEFAULT_OUTPUT_DIR="/tmp/my-dotfiles"

# The list of dotfiles
DOTFILES=(
    # Shell configurations
    ".bashrc"
    ".bash_profile"
    ".bash_aliases"
    ".bash_functions"
    ".dir_colors"
    ".lessfilter"
    ".profile.d"

    # Shell history (optional - comment out if you don't want history)
    # ".bash_history"

    # Git configuration
    ".gitconfig"
    ".gitignore_global"
    ".gitmessage"

    # Desktop
    ".fonts"
    ".icons"
    ".local/share/applications"
    ".local/share/icons"
    ".user-icon.jpg"

    # Editors
    ".config/nvim"
    ".config/sublime-text"

    # hyprland
    ".config/hypr"
    ".config/wal"
    ".config/waybar"
    ".config/qarma.conf"

    # firefox
    # ".mozilla"

    # thunderbird
    # ".thunderbird"

    # music players
    ".config/mpd"
    ".config/rmpc"
    ".config/sayonara"
    ".config/haruna"
    ".config/harunarc"

    # other programs
    ".config/bash-notes.rc"
    ".config/cherrytree"
    ".config/conky"
    ".config/draw.io"
    ".config/featherpad"
    ".config/htop"
    ".config/joplin-desktop"
    ".config/kitty"
    ".config/lximage-qt"
    ".config/lxqt"
    ".config/mimeapps.list"
    ".config/pcmanfm-qt"
    ".config/powerline"
    ".config/qpdfview"
    ".config/rofi"

    # pandoc
    ".local/share/pandoc/"

    # my executables
    "bin"

    ### SYSTEM FILES
    # bash completion
    "/etc/bash_completion.d"
)

# helper function to check if a string ($2) begins with another string ($1)
beginswith() { 
    case $2 in 
        "$1"*)
            true
            ;; 
        *)
            false
            ;;
    esac;
}

# clear the screen before we output anything
clear

echo -e "${YELLOW}Local Git Repository: ${NC}"
if [[ -d $DEFAULT_OUTPUT_DIR ]]; then
    echo -e "\t${BLUE}$DEFAULT_OUTPUT_DIR ${GREEN}${NC}"
    # it exists, so we check for subdirectories
    #   - home
    #   - system
    [ -d $DEFAULT_OUTPUT_DIR/home ] || mkdir -p $DEFAULT_OUTPUT_DIR/home
    [ -d $DEFAULT_OUTPUT_DIR/system ] || mkdir -p $DEFAULT_OUTPUT_DIR/system
    # check if we are in a git repository
    cd $DEFAULT_OUTPUT_DIR
    if [[ $(git rev-parse --is-inside-work-tree) == "true" ]]; then
        # we are inside a git repo
        echo -e "${GREEN}Git Repository already initialized.${NC}"
    else
        echo -e "${YELLOW}Initializing Git Repository. Don't forget to add your remotes.${NC}"
        git init
        git add .
    fi
else
    echo -e "\t${BLUE}$DEFAULT_OUTPUT_DIR ${RED}${NC}"
    # our backup directory doesn't exists, so we make it
    echo -e "${YELLOW}creating our backup directories${NC}"
    mkdir -p $DEFAULT_OUTPUT_DIR/{home,system}
    echo -e "${YELLOW}Initializing Git Repository. Don't forget to add your remotes.${NC}"
    cd $DEFAULT_OUTPUT_DIR
    git init
    git add .
fi

# reset colors
echo -e $NC

cd $WORKDIR
# we iterate all dotfiles in the list
for i in ${DOTFILES[@]}; do
    # if it's a file in my home
    if [[ -f ${HOME}/$i ]]; then
        echo -e "${GREEN}${HOME}/$i\t  ${NC}"
        # it exists, we copy it to our output directory
        rsync -a ${HOME}/$i ${DEFAULT_OUTPUT_DIR}/home/ && echo -e "\t ${GREEN}Copied\t  ${NC}"
    # if it's a directory in my home
    elif [[ -d ${HOME}/$i ]]; then
        echo -e "${BLUE}${HOME}/$i\t  ${NC}"
        # it exists, we copy it to our output directory
        rsync -a ${HOME}/$i ${DEFAULT_OUTPUT_DIR}/home/ && echo -e "\t ${GREEN}Copied\t  ${NC}"
    # if it begins with a / it's a system file/directory 
    elif beginswith "/" $i; then
        # if it's a file
        if [[ -f $i ]]; then
            echo -e "${GREEN}$i\t  ${NC}"
            # it exists, we copy it to our output directory
            rsync -a $i ${DEFAULT_OUTPUT_DIR}/system/ && echo -e "\t ${GREEN}Copied\t  ${NC}"
        # if it's a directory
        elif [[ -d $i ]]; then
            echo -e "${BLUE}$i\t  ${NC}"
            # it exists, we copy it to our output directory
            rsync -a $i ${DEFAULT_OUTPUT_DIR}/system/ && echo -e "\t ${GREEN}Copied\t  ${NC}"
        fi
    # if it doesn't exists
    else
        echo -e "\n${RED}NOT FOUND: ${i}\t  ${NC}\n"
    fi
done

echo -e "\n${GREEN}Adding all copied files to git.${NC}"
cd $DEFAULT_OUTPUT_DIR
git add .
# reset colors before exiting
echo -e $NC
cd $WORKDIR
exit 0