-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackup
executable file
·133 lines (117 loc) · 2.42 KB
/
Backup
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
#!/bin/bash
#Backs up all non-hidden folders and files in the current directory
#Accepts additional files and folters to compress as arguments
ITEMS="*";
FOLDER=".";
WORKING_DIRECTORY="$PWD"
EXCLUDE=false
INCLUDE=false
CHECK=false
until [ -z "$1" ]
do
case "$1" in
"-f")
if [ -z "$2" ]; then
echo "Please enter a folder location"
else
FOLDER="$2"
shift
fi
;;
"-I")
INCLUDE=true
EXCLUDE=false
shift
continue
;;
"-E")
EXCLUDE=true
INCLUDE=false
shift
continue
;;
"-C")
CHECK=true
shift
continue
;;
*)
if $INCLUDE; then
ITEMS="$ITEMS $1"
elif $EXCLUDE; then
echo "Removing $1"
ITEMS="$( echo $ITEMS | sed "s/$1//g" )"
fi
shift
;;
esac
done
echo $ITEMS
#Makes directory if it doesnt exist
if [[ "$FOLDER" && ! -d "$FOLDER" ]]; then
mkdir $FOLDER
fi
cd $FOLDER
FILE_NAME="BACKUP_FILE"
#Tests if that file exist already
if [ -f "$FILE_NAME" ]; then
echo "File already exists.\nOveride (Y/N)"
read -t 10 -n 1 options
case "$options" in
"N")
echo "New file name:"
read FILE_NAME
;;
*)
;;
esac
fi
cd $WORKING_DIRECTORY
echo $PWD
#Checks if the user has permission for all the files he
#is trying to back up
if $CHECK && [ "$(id -u)" != "0" ]; then
echo "Checking"
FILE_USERS="$( ls -lR | awk '{print $3}' )"
ALL_USERS="$( cat /etc/passwd | awk -F: '{print $1}' )"
for usr in $FILE_USERS
do
if [ usr != "$USER" ]; then
for x in $ALL_USERS
do
if [ x = usr ]; then
print "You may not have permissions for all the files/folders you included"
print "Would you like to continue (Y/N)"
read -t 10 -n 1 options
case "$options" in
"N")
exit 2
;;
"Y")
break 2
;;
*)
exit 2
;;
esac
fi
done
fi
done
fi
echo "Creating tar"
tar -cf $FOLDER/backup.tar $ITEMS
if [ "$?" -eq 2 ]; then
echo "Tar creatinon failed"
else
echo "$( ls -lh | awk '{ print $9, " is ", $5 }' )"
tar -tvf "$FOLDER/backup.tar" > "$FOLDER/manifest"
7z a "$FOLDER/$FILE_NAME" "$FOLDER/backup.tar" "$FOLDER/manifest"
fi
rm -f $FOLDER/backup.tar
if [ "$?" -eq 0 ]; then
exit $?
else
echo "May not have sucessfully compressed"
exit $?
fi