-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-osx.sh
executable file
·150 lines (118 loc) · 3.42 KB
/
backup-osx.sh
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
#!/bin/bash
# Colorize terminal
red='\e[0;31m'
no_color='\033[0m'
# Console step increment
i=1
# Get Date
NOW=$(date +'%Y-%m-%dT%H-%M-%S')
# Default
BACKUP_DIR="$(pwd)/backup_$NOW"
BACKUP_COMPRESSION="false"
BACKUP_COMPRESSION_ARGS="--no-compress"
BACKUP_FULL="false"
# Declare script helper
TEXT_HELPER="\nThis script aims to copy importants files and directory before a restauration.
Following flags are available:
-c (Optional) Use rsync compression during backup.
Default is '$BACKUP_COMPRESSION'.
-f (Optional) Perform a full backup of the entire home directory (for personnal use).
Default is '$BACKUP_FULL'.
-o (Optional) Output directory.
Default is '$(pwd)'.
-h Print script help.\n\n"
print_help() {
printf "$TEXT_HELPER"
}
# Parse options
while getopts hcfo: flag; do
case "${flag}" in
c)
BACKUP_COMPRESSION="true"
BACKUP_COMPRESSION_ARGS="--compress";;
f)
BACKUP_FULL="true";;
o)
BACKUP_DIR="${OPTARG}/backup_$NOW";;
h | *)
print_help
exit 0;;
esac
done
# Settings
printf "\nScript settings:
-> backup target dir: ${red}${BACKUP_DIR}${no_color}
-> use compression: ${red}${BACKUP_COMPRESSION}${no_color}
-> full backup: ${red}${BACKUP_FULL}${no_color}\n"
# Backup /etc files
printf "\n${red}${i}.${no_color} Backup /etc files\n\n"
i=$(($i + 1))
ETC_FILES=(
/etc/hosts
/etc/resolv.conf
)
mkdir -p "${BACKUP_DIR%/}/etc"
rsync -ahW $BACKUP_COMPRESSION_ARGS --info=progress2 ${ETC_FILES[*]} "${BACKUP_DIR%/}/etc"
# Export gpg keys
printf "\n${red}${i}.${no_color} Export gpg keys\n\n"
i=$(($i + 1))
EMAIL="this-is-tobi@proton.me"
BACKUP_DIR="$HOME/.gnupg/backup"
mkdir -p "$BACKUP_DIR"
KEY_ID=$(gpg --list-secret-keys --keyid-format=long "$EMAIL" | grep 'sec' | awk '{print $2}' | cut -d'/' -f2)
if [ -z "$KEY_ID" ]; then
echo "No GPG key found for email: $EMAIL"
else
gpg --export --armor "$KEY_ID" > "$BACKUP_DIR/public-key-$KEY_ID.asc"
echo "Public gpg key saved to $BACKUP_DIR/public-key-$KEY_ID.asc"
gpg --export-secret-keys --armor "$KEY_ID" > "$BACKUP_DIR/private-key-$KEY_ID.asc"
echo "Private gpg key saved to $BACKUP_DIR/private-key-$KEY_ID.asc"
echo "Backup completed for GPG key: $KEY_ID"
fi
# Backup dotfiles
printf "\n${red}${i}.${no_color} Backup dotfiles\n\n"
i=$(($i + 1))
DOT_FILES=(
$HOME/.aws
$HOME/.config
$HOME/.docker
$HOME/.gitconfig
$HOME/.gnupg
$HOME/.kube
$HOME/.mc
$HOME/.npmrc
$HOME/.ssh
$HOME/.zshrc
)
mkdir -p "${BACKUP_DIR%/}/dotfiles"
rsync -ahW $BACKUP_COMPRESSION_ARGS --info=progress2 ${DOT_FILES[*]} "${BACKUP_DIR%/}/dotfiles"
# Backup home directory
printf "\n${red}${i}.${no_color} Backup home directory\n\n"
i=$(($i + 1))
mkdir "${BACKUP_DIR%/}/home"
if [ "$BACKUP_FULL" = "true" ]; then
HOME_DIRS=(
$HOME/desktop
$HOME/dev
$HOME/documents
$HOME/downloads
$HOME/movies
$HOME/music
$HOME/pictures
)
else
HOME_DIRS=(
$HOME/dev
)
fi
rsync -ahW $BACKUP_COMPRESSION_ARGS --info=progress2 --exclude '**/node_modules' ${HOME_DIRS[*]} "${BACKUP_DIR%/}/home"
# Backup brave
if [ -d $HOME/Library/Application\ Support/BraveSoftware ]; then
printf "\n${red}${i}.${no_color} Backup brave files\n\n"
i=$(($i + 1))
BRAVE_FILES=(
$HOME/Library/Application\ Support/BraveSoftware/Brave-Browser/Default
)
mkdir -p "${BACKUP_DIR%/}/brave"
rsync -ahW $BACKUP_COMPRESSION_ARGS --info=progress2 ${BRAVE_FILES[*]} "${BACKUP_DIR%/}/brave"
fi