forked from pedroetb/rsync-incremental-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsync-incremental-backup-system
127 lines (102 loc) · 3.24 KB
/
rsync-incremental-backup-system
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
#!/bin/bash
# Configuration variables (change as you wish)
src="/"
dst="/mnt/path/to/target"
backupDepth=7
timeout=1800
pathBak0="data"
rotationLockFileName=".rsync-rotation-lock"
pathBakN="backup"
nameBakN="backup"
logName="rsync-incremental-backup_$(date -Id)_$(date +%H-%M-%S).log"
tempLogFolderName=".rsync-incremental-backup"
logFolderName="log"
# Combinate previously defined variables for use (don't touch this)
tempLogBasePath="${HOME}/${tempLogFolderName}"
tempLogPath="${tempLogBasePath}/system_${dst//[\/]/\\}"
bak0="${dst}/${pathBak0}"
rotationLockFilePath="${dst}/${rotationLockFileName}"
logPath="${dst}/${pathBakN}/${logFolderName}"
logFile="${tempLogPath}/${logName}"
# Prepare log file
mkdir -p ${tempLogPath}
touch ${logFile}
writeToLog() {
echo -e "$1" | tee -a ${logFile}
}
writeToLog "********************************"
writeToLog "* *"
writeToLog "* rsync-incremental-backup *"
writeToLog "* *"
writeToLog "********************************"
# Prepare backup paths
i=1
while [ $i -le $backupDepth ]
do
export bak$i="${dst}/${pathBakN}/${nameBakN}.$i"
true $((i = i + 1))
done
# Prepare main rsync configuration
rsyncFlags="-aAXhv --info=progress2 --timeout=${timeout} --delete -W \
--link-dest=${bak1}/ --log-file=${logFile} --exclude=${tempLogBasePath}"
# Prepare log rsync configuration
logRsyncFlags="-rhv --remove-source-files --exclude=${logName} --log-file=${logFile}"
writeToLog "\n[$(date -Is)] You are going to backup"
writeToLog "\tfrom: ${src}"
writeToLog "\tto: ${bak0}"
writeToLog "\tflags: ${rsyncFlags}"
# Prepare paths at destination
mkdir -p ${dst} ${logPath}
writeToLog "\n[$(date -Is)] Old logs sending begins\n"
# Send old pending logs to destination
rsync ${logRsyncFlags} ${tempLogPath}/ ${logPath}/
writeToLog "\n[$(date -Is)] Old logs sending finished"
# Rotate backups if last rsync succeeded ..
if ([ ! -e ${rotationLockFilePath} ])
then
# .. and there is previous data
if [ -d ${bak0} ]
then
writeToLog "\n[$(date -Is)] Backups rotation begins"
true $((i = i - 1))
# Remove the oldest backup if exists
bak="bak$i"
rm -rf ${!bak}
# Rotate the previous backups
while [ $i -gt 0 ]
do
bakNewPath="bak$i"
true $((i = i - 1))
bakOldPath="bak$i"
if [ -d ${!bakOldPath} ]
then
mv ${!bakOldPath} ${!bakNewPath}
fi
done
writeToLog "[$(date -Is)] Backups rotation finished\n"
else
writeToLog "\n[$(date -Is)] No previous data found, there is no backups to be rotated\n"
fi
else
writeToLog "\n[$(date -Is)] Last backup failed, backups will not be rotated\n"
fi
# Set rotation lock file to detect in next run when backup fails
touch ${rotationLockFilePath}
writeToLog "[$(date -Is)] Backup begins\n"
# Do the backup (with mandatory exclusions)
rsync ${rsyncFlags} --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} \
${src}/ ${bak0}/
# Check rsync success
if [ "$?" -eq "0" ]
then
writeToLog "\n[$(date -Is)] Backup completed successfully\n"
# Clear unneeded partials and lock file
rm -rf ${rotationLockFilePath}
rsyncFail=0
else
writeToLog "\n[$(date -Is)] Backup failed, try again later\n"
rsyncFail=1
fi
# Send the complete log file to destination
mv ${logFile} ${logPath}
exit ${rsyncFail}