-
Notifications
You must be signed in to change notification settings - Fork 0
/
megabackup.sh
144 lines (107 loc) · 3.79 KB
/
megabackup.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
#!/bin/bash
timer_start=`date +%s`
# ORIGINAL IDEA
# http://forum.ubuntu-it.org/viewtopic.php?p=3284474#p3284474
# NEEDS MEGACMD FROM
# https://mega.nz/cmd
# - you need to log in first!
# - $ mega-cmd
# - $ login [email protected]
# - $ exit
####################
### CONFIGURATION
####################
MAIL='[email protected]'
ORIGIN='/var/www'
DESTINATION='/media/Storage/Backups/'
BACKUP_ON_MEGA=false
DB_USER='root'
DB_PASS=''
####################
### TODAY'S DATE
####################
DATE=`date '+%Y-%02m-%02d_%02k.%M'`
DAY=`date '+%02d'`
#######################
### BACKUP DATABASES
### if a $DESTINATION/00_databases.txt exists, grab the list of specified databases you want to backup, otherwise get them all
### 00_databases.txt content is just a line separated list of database names, e.g.
###
### database1
### mywebsite
### otherdb
#######################
cd $DESTINATION
# create a temporary directory for database backups if needed
if ! [ -d "database_dumps" ]; then
mkdir database_dumps
fi
if [ -f 00_databases.txt ]; then
# backup only specified databases
DATABASES=( `cat "00_databases.txt" `)
for (( i = 0 ; i < ${#DATABASES[@]} ; i++ )) do
db=${DATABASES[$i]}
mysqldump --force --opt --user="$DB_USER" --password="$DB_PASS" --databases $db > database_dumps/$db.sql
done
else
# backup all databases
DATABASES=`mysql --user="$DB_USER" --password="$DB_PASS" -e "SHOW DATABASES;" | grep -Ev "(Database|test|phpmyadmin|mysql|performance_schema|information_schema)"`
for db in $DATABASES; do
mysqldump --force --opt --user="$DB_USER" --password="$DB_PASS" --databases $db > database_dumps/$db.sql
done
fi
# create a compressed archive for databases
DBFILENAME=Backup-Databases-$DATE.tar.bz2
tar cpfj $DBFILENAME database_dumps
# remove the database dump's temporary directory
rm -rf database_dumps
###################
### BACKUP FILES
### Full backup every 1st day of month
### Differential backup every other day
###################
cd $ORIGIN
if [ $DAY = "01" ] || ! [ -f $DESTINATION/backup-log.snar ]; then
BACKUP_TYPE="Full backup"
# removing the .snar log tells tar to create a new full backup
rm -f $DESTINATION/backup-log.snar
FILENAME=$DESTINATION/Backup-FULL-$DATE.tar.bz2
tar cpfj $FILENAME --ignore-failed-read --listed-incremental $DESTINATION/backup-log.snar ./
# if the backup file was created successfully
# keep the last two full backups, the last 7 diff backups and remove everything else
if [ -f "$FILENAME" ]; then
ls -td $DESTINATION/Backup-DIFF* | tail -n +8 | xargs rm -f
ls -td $DESTINATION/Backup-FULL* | tail -n +3 | xargs rm -f
fi
else
BACKUP_TYPE="Differential backup"
# I'm saving a copy of backup-log.snar because we want a differential backup and not incremental
# I'll restore it later to have further differential backups based on the last full backup
cp $DESTINATION/backup-log.snar $DESTINATION/backup-log.snar.0
FILENAME=$DESTINATION/Backup-DIFF-$DATE.tar.bz2
tar cpfj $FILENAME --ignore-failed-read --listed-incremental $DESTINATION/backup-log.snar ./
mv $DESTINATION/backup-log.snar.0 $DESTINATION/backup-log.snar
fi
timer_end=`date +%s`
runtime=$((timer_end-timer_start))
echo "$BACKUP_TYPE finished in $runtime seconds."
##########################
### SAVE BACKUP ON MEGA
### and delete local backup file
##########################
if [ $BACKUP_ON_MEGA = true ]; then
# upload...
mega-put -c $FILENAME '/Backup'
mega-put -c $DESTINATION/$DBFILENAME '/Backup'
# if upload went successfull remove local files
UPLOADED=`mega-find /Backup/$DBFILENAME `
EXIST=${#UPLOADED}
if [ $EXIST = 0 ]; then
# files not uploaded. Leave files alone and send an alert email
mailx -s "Problema con il salvataggio dei backup su Mega. Per favore, verifica" < /dev/null $MAIL
else
# all good. Remove local file
rm $FILENAME
rm $DESTINATION/$DBFILENAME
fi
fi