This repository was archived by the owner on Aug 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.sh
More file actions
executable file
·81 lines (60 loc) · 1.95 KB
/
backup.sh
File metadata and controls
executable file
·81 lines (60 loc) · 1.95 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
#!/bin/bash
# backup script
# check if backup config file exists
if [[ -s "backup.config" ]]; then
source "backup.config"
else
echo "fatal: no config file found"
exit 1
fi
# check if targets file exists
if [[ ! -s "targets.config" ]]; then
echo "fatal: no targets file found"
exit 1
fi
# check if backup directory exists
if [[ ! -d "${backupDirectory}" ]]; then
echo "fatal: backup directory does not exists"
exit 1
fi
# make dir for shadow copies
mkdir /tmp/backup
# progress info
echo "info: backing up..."
cat "targets.config" | while read line; do
# remove double quotes
tempTargetDirectory=$(sed -e 's/^"//' -e 's/"$//' <<<"$line")
# remove single quotes
targetDirectory=$(sed -e "s/^'//" -e "s/'$//" <<<"$tempTargetDirectory")
# get name of target directory
targetDirectoryName=$(echo "${targetDirectory}" | sed 's:.*/::')
# check if target directory is empty
if [[ -z "$(ls -A ${targetDirectory})" ]]; then
echo "error: refuse to backup directory which is empty"
exit 1
fi
echo "info: backing up directory: ${targetDirectory} ${targetDirectoryName}"
# make shadow copy of target directory in temp/backup folder
nice -n 19 cp -r "${targetDirectory}" "/tmp/backup/"
# check if targets info file exists in temp/backup folder
if [[ ! -s "/tmp/backup/targets.info" ]]; then
echo "info: no targets info file found, creating a new one"
touch "/tmp/backup/targets.info"
fi
# add name of directory with path to temp/backup
echo "${targetDirectoryName}=${targetDirectory}" >>"/tmp/backup/targets.info"
done
# remember current directory
pwd=$(pwd)
cd /tmp/
# compress
echo "info: compressing..."
nice -n 19 tar -czf "backup.tar.gz" "backup"
echo "info: done compressing"
nice -n 19 rm -r "backup"
nice -n 19 mv "backup.tar.gz" "${timestampFormat}-backup.tar.gz"
nice -n 19 mv "${timestampFormat}-backup.tar.gz" "${backupDirectory}"
# finish info
echo "info: backup successful"
# get back to were we were
cd "${pwd}"