-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem
More file actions
112 lines (98 loc) · 3.69 KB
/
Copy pathsystem
File metadata and controls
112 lines (98 loc) · 3.69 KB
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
#!/usr/bin/env bash
## LOCATION: $HOME/bin/
# Insert file paths of files for backup
declare -a SOURCEPATH=($HOME/.bashrc $HOME/.xinitrc $HOME/.Xresources $HOME/.vimrc $HOME/bin/colors2 $HOME/bin/notify $HOME/bin/system $HOME/bin/xmenu.sh $HOME/bin/gen $HOME/bin/install.sh $HOME/bin/rpg $HOME/bin/luminance.py $HOME/.conkyrc $HOME/bin/bar.sh)
BACKUPDIR=$HOME/dotfiles/
backup () {
[[ -d $BACKUPDIR ]] || { mkdir $BACKUPDIR; notify "$BACKUPDIR created"; }
# Compare the local file associated with each file found in the backup directory. If there is a difference, back it up. If no file is found in the backup directory, perform inital backup
for FILE in ${SOURCEPATH[@]}; do
if [ -f "$(find $BACKUPDIR -name $(basename $FILE))" ]; then
if [[ $(cmp $FILE $(find $BACKUPDIR -name $(basename $FILE))) ]]; then
cp $FILE $BACKUPDIR/$(basename $FILE) || { notify "Error backing up file"; err_log "Error backing up $FILE"; continue; }
newversions="$newversions$FILE "
fi
else
cp $FILE $BACKUPDIR$(basename $FILE)
initialbackups="$initialbackups$FILE "
fi
done
# Checking strings to make sure they are full before calling notify function
if [ ! -z "$newversions" ]; then
notify "New versions of: $newversions"
if [ ! -z "$initialbackups" ]; then
notify "Initial backups of: $initialbackups"
fi
else
notify "Nothing to be backed up"
fi
}
restore () {
[[ -d $BACKUPDIR ]] || { notify "$BACKUPDIR not found. Not really sure what happened, this was downloaded along with the dotfiles"; exit 1; }
for FILE in ${SOURCEPATH[@]}; do
if [ ! -d $(dirname $FILE) ]; then
mkdir -p $(dirname $FILE)
dirscreated="$dirscreated$FILE "
fi
if [[ $(cmp $(find $BACKUPDIR -name $(basename $FILE)) $FILE) ]]; then
cp $(find $BACKUPDIR -name $(basename $FILE)) $FILE || { notify "Error restoring $FILE"; err_log "Error restoring $FILE"; continue; }
restoredfiles="$restoredfiles$FILE "
fi
done
# Checking strings to make sure they are full before calling notify function
if [ ! -z "$dirscreated" ]; then
notify "Directories created for $dirscreated"
if [ ! -z "$restoredfiles" ]; then
notify "Files restored: $restoredfiles"
fi
else
notify "Nothing has been restored"
fi
}
init () {
# .git directory causes issues with find function when restoring, after repo is cloned from github
sudo rm -r .git
[[ -d $BACKUPDIR ]] || { notify "$BACKUPDIR not found. Please git clone repository"; exit 1; }
for FILE in ${SOURCEPATH[@]}; do
if [ ! -d $(dirname $FILE) ]; then
mkdir -p $(dirname $FILE)
dirscreated="$dirscreated$FILE "
fi
cp $(find $BACKUPDIR -name $(basename $FILE)) $FILE || { notify "Error restoring $FILE"; err_log "Error restoring $FILE"; continue; }
restoredfiles="$restoredfiles$FILE "
done
# Checking strings to make sure they are full before calling notify function
if [ ! -z "$dirscreated" ]; then
notify "Directories created for $dirscreated"
if [ ! -z "$restoredfiles" ]; then
notify "Files restored: $restoredfiles"
fi
else
notify "Nothing has been restored"
fi
}
# Preferred method of notification
notify() {
# notify-send "$@" || { echo "$@"; }
echo "$@"
}
err_log() {
[[ -e err.log ]] || touch err.log
echo "$@" >> err.log
}
usage() {
echo "Usage:"
echo "backup backup files"
echo "restore restore files"
echo "init initial file restore on new system"
echo ""
echo "Make sure to add files that need to be backed up to the SOURCEPATH array"
echo "To check for a list of errors, look for a file named err.log"
echo "WARNING: restoring files will overwrite files with the same name in the destination directory"
}
case $1 in
backup) backup "$@" ;;
restore) restore "$@" ;;
init) init "$@" ;;
*) usage ;;
esac