-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-conf
executable file
·51 lines (41 loc) · 1.04 KB
/
backup-conf
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
#!/bin/bash
if [ -z "$1" ]; then
echo "no input file"
exit
fi
full_path=$(readlink -f "$1")
file_name=$(basename "${full_path}")
directory=$(dirname "${full_path}")
cd "$directory"
if [ ! -f "${file_name}" ]; then
echo "file doesn't exist!"
exit
fi
backup_file_name="${file_name}.bk"
if [[ -f "${backup_file_name}" ]]; then
# echo "Backup ${backup_file_name} already exists"
MAX_BACKUP_NUMBER=100
for ((number=1; number <= ${MAX_BACKUP_NUMBER}; number++))
{
backup_file_name="${file_name}.${number}.bk"
if [[ ! -f "${backup_file_name}" ]]; then
break;
fi
}
if [ "$number" -ge "$MAX_BACKUP_NUMBER" ]; then
echo "Version overflow; Backup cannot be created"
exit
fi
fi
command='cp "${file_name}" "${backup_file_name}"'
#check_writable
if [ -w "$directory" ]; then
eval "${command}"
else
eval "sudo ${command}"
fi
cd /
ls -l "${directory}/${file_name}"
ls -l "${directory}/${file_name}.bk"
ls -l "${directory}/${file_name}.bk".* 2>/dev/null
ls -l "${directory}/${file_name}."*".bk" 2>/dev/null