forked from pedroetb/rsync-incremental-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsync-incremental-backup-remote
executable file
·144 lines (118 loc) · 3.82 KB
/
rsync-incremental-backup-remote
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
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
# Configuration variables (change as you wish)
src="/path/to/source"
dst="/path/to/target"
remote="ssh_remote"
backupDepth=7
timeout=1800
pathBak0="data"
partialFolderName=".rsync-partial"
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}/${remote}_${dst//[\/]/\\}"
remoteDst="${remote}:${dst}"
bak0="${dst}/${pathBak0}"
remoteBak0="${remoteDst}/${pathBak0}"
partialFolderPath="${dst}/${partialFolderName}"
rotationLockFilePath="${dst}/${rotationLockFileName}"
logPath="${dst}/${pathBakN}/${logFolderName}"
remoteLogPath="${remote}:${logPath}"
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="-achvz --info=progress2 --timeout=${timeout} --delete --no-W --partial-dir=${partialFolderName} \
--link-dest=${bak1}/ --log-file=${logFile} --exclude=${tempLogBasePath} --chmod=+r"
# Prepare log rsync configuration
logRsyncFlags="-rhvz --remove-source-files --exclude=${logName} --log-file=${logFile}"
writeToLog "\n[$(date -Is)] You are going to backup"
writeToLog "\tfrom: ${src}"
writeToLog "\tto: ${remoteBak0}"
writeToLog "\tflags: ${rsyncFlags}"
# Check remote connection
ssh -q -o BatchMode=yes -o ConnectTimeout=10 ${remote} exit
if [ "$?" -ne "0" ]
then
writeToLog "\n[$(date -Is)] Remote destination is not reachable"
exit 1
fi
# Prepare paths at destination
ssh ${remote} "mkdir -p ${dst} ${logPath}"
writeToLog "\n[$(date -Is)] Old logs sending begins\n"
# Send old pending logs to destination
rsync ${logRsyncFlags} ${tempLogPath}/ ${remoteLogPath}/
writeToLog "\n[$(date -Is)] Old logs sending finished"
# Rotate backups if last rsync succeeded ..
if (ssh ${remote} "[ ! -d ${partialFolderPath} ] && [ ! -e ${rotationLockFilePath} ]")
then
# .. and there is previous data
if (ssh ${remote} "[ -d ${bak0} ]")
then
writeToLog "\n[$(date -Is)] Backups rotation begins"
true $((i = i - 1))
# Remove the oldest backup if exists
bak="bak$i"
ssh ${remote} "rm -rf ${!bak}"
# Rotate the previous backups
while [ $i -gt 0 ]
do
bakNewPath="bak$i"
true $((i = i - 1))
bakOldPath="bak$i"
if (ssh ${remote} "[ -d ${!bakOldPath} ]")
then
ssh ${remote} "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
ssh ${remote} "touch ${rotationLockFilePath}"
writeToLog "[$(date -Is)] Backup begins\n"
# Do the backup
rsync ${rsyncFlags} ${src}/ ${remoteBak0}/
# Check rsync success
if [ "$?" -eq "0" ]
then
writeToLog "\n[$(date -Is)] Backup completed successfully\n"
# Clear unneeded partials and lock file
ssh ${remote} "rm -rf ${partialFolderPath} ${rotationLockFilePath}"
rsyncFail=0
else
writeToLog "\n[$(date -Is)] Backup failed, try again later\n"
rsyncFail=1
fi
# Send the complete log file to destination
scp ${logFile} ${remoteLogPath}
if [ "$?" -eq "0" ]
then
rm ${logFile}
fi
exit ${rsyncFail}