forked from aweijnitz/pi_backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·199 lines (165 loc) · 6.45 KB
/
backup.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
# ___ CREDITS
# This script started from
# http://raspberrypi.stackexchange.com/questions/5427/can-a-raspberry-pi-be-used-to-create-a-backup-of-itself
# which in turn started from
# http://www.raspberrypi.org/phpBB3/viewtopic.php?p=136912
#
# Users of this script can just modify the below marked values (stopService,startservice function and directory to
# store the backup
#
# 2013-Sept-04
# Merged in later comments from the original thread (the pv exists check modified) and added the backup.log
#
# 2013-Sept-05
# Remved tar compression, since it takes FOREVER to complete and I don't need it.
#
# 2014-Feb-12
# Merged in comments from http://www.raspberrypi.org/forum/viewtopic.php?f=63&t=12079&p=494488
# Moved interesting variables to top of script
# Added options around updates and gzips
#
# Add an entry to crontab to run regurlarly.
# Example: Update /etc/crontab to run backup.sh as root every night at 3am
# 01 4 * * * root /home/pi/scripts/backup.sh
# ======================== CHANGE THESE VALUES ========================
function stopServices {
echo -e "${purple}${bold}Stopping services before backup${NC}${normal}" | tee -a $DIR/backup.log
sudo service sendmail stop
sudo service cron stop
sudo service ssh stop
sudo pkill deluged
sudo pkill deluge-web
sudo service deluge-daemon stop
sudo ervice btsync stop
sudo service apache2 stop
sudo service samba stop
#sudo service noip stop
#sudo service proftpd stop
#sudo service webmin stop
#sudo service xrdp stop
#sudo service ddclient stop
#sudo service apache2 stop
#sudo service samba stop
#sudo service avahi-daemon stop
#sudo service netatalk stop
}
function startServices {
echo -e "${purple}${bold}Starting the stopped services${NC}${normal}" | tee -a $DIR/backup.log
sudo ervice samba start
sudo service apache2 start
sudo service btsync start
sudo service deluge-daemon start
sudo service ssh start
sudo service cron start
sudo service sendmail start
}
# Setting up directories
SUBDIR=raspberrypi_backups
MOUNTPOINT=/media/usbstick64gb
DIR=$MOUNTPOINT/$SUBDIR
RETENTIONPERIOD=1 # days to keep old backups
POSTPROCESS=0 # 1 to use a postProcessSucess function after successfull backup
GZIP=0 # whether to gzip the backup or not
# Function which tries to mount MOUNTPOINT
function mountMountPoint {
# mount all drives in fstab (that means MOUNTPOINT needs an entry there)
mount -a
}
function postProcessSucess {
# Update Packages and Kernel
echo -e "${yellow}Update Packages and Kernel${NC}${normal}" | tee -a $DIR/backup.log
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get autoclean
echo -e "${yellow}Update Raspberry Pi Firmware${NC}${normal}" | tee -a $DIR/backup.log
sudo rpi-update
sudo ldconfig
# Reboot now
echo -e "${yellow}Reboot now ...${NC}${normal}" | tee -a $DIR/backup.log
sudo reboot
}
# =====================================================================
# Setting up echo fonts
red='\e[0;31m'
green='\e[0;32m'
cyan='\e[0;36m'
yellow='\e[1;33m'
purple='\e[0;35m'
NC='\e[0m' #No Color
bold=`tput bold`
normal=`tput sgr0`
# Check if mount point is mounted, if not quit!
if ! mountpoint -q "$MOUNTPOINT" ; then
echo -e "${yellow}${bold}Destination is not mounted; attempting to mount ... ${NC}${normal}"
mountMountPoint
if ! mountpoint -q "$MOUNTPOINT" ; then
echo -e "${red}${bold} Unable to mount $MOUNTPOINT; Aborting! ${NC}${normal}"
exit 1
fi
echo -e "${green}${bold}Mounted $MOUNTPOINT; Continuing backup${NC}${normal}"
fi
#LOGFILE="$DIR/backup_$(date +%Y%m%d_%H%M%S).log"
# Check if backup directory exists
if [ ! -d "$DIR" ];
then
mkdir $DIR
echo -e "${yellow}${bold}Backup directory $DIR didn't exist, I created it${NC}${normal}" | tee -a $DIR/backup.log
fi
echo -e "${green}${bold}Starting RaspberryPI backup process!${NC}${normal}" | tee -a $DIR/backup.log
echo "____ BACKUP ON $(date +%Y/%m/%d_%H:%M:%S)" | tee -a $DIR/backup.log
echo ""
# First check if pv package is installed, if not, install it first
PACKAGESTATUS=`dpkg -s pv | grep Status`;
if [[ $PACKAGESTATUS == S* ]]
then
echo -e "${cyan}${bold}Package 'pv' is installed${NC}${normal}" | tee -a $DIR/backup.log
echo ""
else
echo -e "${yellow}${bold}Package 'pv' is NOT installed${NC}${normal}" | tee -a $DIR/backup.log
echo -e "${yellow}${bold}Installing package 'pv' + 'pv dialog'. Please wait...${NC}${normal}" | tee -a $DIR/backup.log
echo ""
sudo apt-get -y install pv && sudo apt-get -y install pv dialog
fi
# Create a filename with datestamp for our current backup
OFILE="$DIR/backup_$(hostname)_$(date +%Y%m%d_%H%M%S)".img
# First sync disks
sync; sync
# Shut down some services before starting backup process
stopServices
# Begin the backup process, should take about 45 minutes hour from 8Gb SD card to HDD
echo -e "${green}${bold}Backing up SD card to img file on HDD${NC}${normal}" | tee -a $DIR/backup.log
SDSIZE=`sudo blockdev --getsize64 /dev/mmcblk0`;
if [ $GZIP = 1 ];
then
echo -e "${green}Gzipping backup${NC}${normal}"
OFILE=$OFILE.gz # append gz at file
sudo pv -tpreb /dev/mmcblk0 -s $SDSIZE | dd bs=1M conv=sync,noerror iflag=fullblock | gzip > $OFILE
else
echo -e "${green}No backup compression${NC}${normal}"
sudo pv -tpreb /dev/mmcblk0 -s $SDSIZE | dd of=$OFILE bs=1M conv=sync,noerror iflag=fullblock
fi
# Wait for DD to finish and catch result
BACKUP_SUCCESS=$?
# Start services again that where shutdown before backup process
startServices
# If command has completed successfully, delete previous backups and exit
if [ $BACKUP_SUCCESS = 0 ];
then
echo -e "${green}${bold}RaspberryPI backup process completed! FILE: $OFILE${NC}${normal}" | tee -a $DIR/backup.log
echo -e "${yellow}Removing backups older than $RETENTIONPERIOD days${NC}" | tee -a $DIR/backup.log
sudo find $DIR -maxdepth 1 -name "*.img" -o -name "*.gz" -mtime +$RETENTIONPERIOD -exec rm {} \;
echo -e "${cyan}If any backups older than $RETENTIONPERIOD days were found, they were deleted${NC}" | tee -a $DIR/backup.log
if [ $POSTPROCESS = 1 ] ;
then
postProcessSucess
fi
exit 0
else
# Else remove attempted backup file
echo -e "${red}${bold}Backup failed!${NC}${normal}" | tee -a $DIR/backup.log
sudo rm -f $OFILE
echo -e "${purple}Last backups on HDD:${NC}" | tee -a $DIR/backup.log
sudo find $DIR -maxdepth 1 -name "*.img" -exec ls {} \;
exit 1
fi