forked from alaub81/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-local-system.sh
executable file
·168 lines (148 loc) · 5.8 KB
/
backup-local-system.sh
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
#########################################################################
#backup-local-system.sh
#Backup Script
#by A. Laub
#andreas[-at-]laub-home.de
#
#License:
#This program is free software: you can redistribute it and/or modify it
#under the terms of the GNU General Public License as published by the
#Free Software Foundation, either version 3 of the License, or (at your option)
#any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#or FITNESS FOR A PARTICULAR PURPOSE.
#########################################################################
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
#########################################################################
#Backup Options
#########################################################################
#where to save the backup files
BACKUPPATH="/backup/system"
#Temporary backup files path
BACKUPTEMPPATH="/tmp"
#Backup filenames
FULLBACKUPFILE="backup-$(date +"%F-%H%M")-FULL.tar.gz"
DIFFBACKUPFILE="backup-$(date +"%F-%H%M")-DIFF.tar.gz"
#What you want to backup
FILESTOSAVE="/root/ /etc/ /usr/local/sbin/ /srv/ /var/ /home/ /boot/ /media/"
#Files you want to exclude from the backup (uncomment to use)
EXCLUDEFILES="--exclude=/var/lib/docker/* --exclude=/var/cache/apt/* --exclude=/var/swap --exclude=/var/spool/postfix/*"
# Make only a Full Backups and No Differential (yes/no)
FULLBACKUPONLY="no"
# Day for the FullBackup (Sun,Mon,Tue,Wed,Thu,Fri,Sat)
FULLBACKUPDAY="Sun"
#Delete old Backupfiles when they are xx Days old
DAYS="14"
#########################################################################
#Logging Options
#########################################################################
#Mail Notification (yes/no)
STATUSMAIL="yes"
#Email Adress, where to send the logfile
MAILADRESS="[email protected]"
#Logfile
LOGFILE="/var/log/systembackup.log"
##########################################################################
#Here the functions starting: Don't edit below
##########################################################################
function STARTBLABLA {
echo "############################################# " > $LOGFILE
echo "# Local Backup " >> $LOGFILE
echo "# started: $(date +"%k:%M %d.%m.%Y") " >> $LOGFILE
echo "############################################# " >> $LOGFILE
echo "" >> $LOGFILE
echo "" >> $LOGFILE
}
function BACKUPDIRCHECK {
if [ -d $BACKUPPATH ]; then
echo "Backup folder exists" >> $LOGFILE
else
mkdir -p $BACKUPPATH
echo "Backup folder created" >> $LOGFILE
fi
}
function REMOVE {
#Remove old Backup files
find $BACKUPPATH -iname "*.tar.gz" -daystart -mtime +$DAYS -delete >> $LOGFILE
#Remove Timestamp File on Fullbackupday
if [ $(date +%a) = $FULLBACKUPDAY -o $FULLBACKUPONLY = yes ]; then
rm -f $BACKUPPATH/timestamp.txt
fi
}
function PACKAGE {
#Backup Packagelist
#Help for restore:
#1. dpkg --set-selections < $BACKUPTEMPPATH/$BACKUPHOST_pkglist.txt
#2. dselect
echo "Packagelist Backup" >> $LOGFILE
dpkg --get-selections > $BACKUPTEMPPATH/pkglist.txt
#Backup Package Answers
#Help for restore:
#1. debconf-set-selections $BACKUPPATH/$BACKUPHOST_pkganswers.tx
echo "Pakage Answers Backup" >> $LOGFILE
debconf-get-selections > $BACKUPTEMPPATH/pkganswers.txt
#read OTHERBACKUPFILES
OTHERBACKUPFILES="$BACKUPTEMPPATH/pkglist.txt $BACKUPTEMPPATH/pkganswers.txt"
}
function FULLBACKUP {
echo "Full Backup:" >> $LOGFILE
date '+%Y-%m-%d %H:%M:%S' > $BACKUPPATH/timestamp.txt
tar czPf $BACKUPPATH/$FULLBACKUPFILE $EXCLUDEFILES $FILESTOSAVE $OTHERBACKUPFILES \
2> /tmp/tmp_file; grep "^tar:" /tmp/tmp_file | grep -v "file is unchanged" >> $LOGFILE
/bin/ls -lh $BACKUPPATH/$FULLBACKUPFILE | awk '{ print $5 " " $8 " " $9}' >> $LOGFILE
}
function DIFFBACKUP {
#Timestamp Datei Auslesen
echo "Diff Backup:" >> $LOGFILE
tar czPf $BACKUPPATH/$DIFFBACKUPFILE --newer $BACKUPPATH/timestamp.txt $EXCLUDEFILES $FILESTOSAVE $OTHERBACKUPFILES \
2> /tmp/tmp_file; grep "^tar:" /tmp/tmp_file | grep -v "file is unchanged" >> $LOGFILE
/bin/ls -lh $BACKUPPATH/$DIFFBACKUPFILE | awk '{ print $5 " " $8 " " $9}' >> $LOGFILE
}
function CLEANUP {
rm -f $OTHERBACKUPFILES
}
function ENDBLABLA {
echo "" >> $LOGFILE
echo "" >> $LOGFILE
echo "############################################# " >> $LOGFILE
echo "# Local Backup " >> $LOGFILE
echo "# ended: $(date +"%k:%M %d.%m.%Y") " >> $LOGFILE
echo "# runtime: $RUNTIME " >> $LOGFILE
echo "############################################# " >> $LOGFILE
}
function RUNNINGTIME {
# stop the timer ########################################
ende=$(date +%s)
diff=$[ende-anfang]
RUNTIME="Runtime: $[$diff / 60]min $[$diff % 60]s"
}
function MAIL {
mail -s "Local Backup on $(hostname -s)" $MAILADRESS < $LOGFILE
}
#####################################################################################
# All functions into 1 function :-)
#####################################################################################
function SYSTEMBACKUP {
# start timer ########################################
anfang=$(date +%s)
STARTBLABLA
BACKUPDIRCHECK
REMOVE
PACKAGE
if [ ! -f $BACKUPPATH/timestamp.txt ]; then
FULLBACKUP
elif [ -f $BACKUPPATH/timestamp.txt ]; then
DIFFBACKUP
fi
CLEANUP
RUNNINGTIME
ENDBLABLA
if [ $STATUSMAIL = yes ]; then
MAIL
fi
}
# at least start the backup
SYSTEMBACKUP