-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-backup.sh
More file actions
executable file
·69 lines (57 loc) · 2.23 KB
/
Copy pathwp-backup.sh
File metadata and controls
executable file
·69 lines (57 loc) · 2.23 KB
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
#!/bin/bash
# written by Balazs in 2018
# predefined variables
LOCATION="/backup"
SEP="/"
LOGDIR="/backup-logs"
LOGFILE=$LOGDIR"wp.log"
CRED="/cred"
DATE=$(date +%F-%H-%M)
# Make sure that directories exist
if [ ! -d $LOGDIR ]; then
mkdir -p $LOGDIR
fi
if [ ! -d $LOCATION ]; then
if mkdir -p $LOCATION; then
echo "$DATE $LOCATION didn't exist, so it was created" >> $LOGFILE
else
echo "$DATE couldn't create backup location $LOCATION" >> $LOGFILE
fi
fi
# collecting all credentials from credential files and starting the backup
if cd $CRED; then
for CREDFILE in *; do
while read -r SITENAME FTPUSER FTPADDR DOCROOT DBADDR DBUSER DBNAME DBPWD; do
echo "sitename: $SITENAME"
# Database dump
if mysqldump --no-tablespaces -h $DBADDR -u $DBUSER -p$DBPWD $DBNAME > $LOCATION$SEP$SITENAME-db-$DATE.sql; then
echo "mysqldump successful for $SITENAME"
else
echo "$DATE mysqldump for $SITENAME $DBNAME was unsuccessful" >> $LOGFILE
fi
if [ "localhost" != "$FTPADDR" ]; then
# Perform secure file backup with ssh, tar and gzip
if ssh $FTPUSER@$FTPADDR "tar -cf - $DOCROOT | gzip -c" > $LOCATION$SEP$SITENAME-wp-$DATE.tar.gz; then
echo "file backup successful for $SITENAME"
else
echo "$DATE file backup for $SITENAME $FTPADDR was unsuccessful" >> $LOGFILE
fi
# Perform a local file backup
else
if tar -cf - $DOCROOT | gzip -c > $LOCATION$SEP$SITENAME-wp-$DATE.tar.gz; then
echo "file backup successful for $SITENAME"
else
echo "$DATE file backup for $SITENAME $FTPADDR was unsuccessful" >> $LOGFILE
fi
fi
done < $CREDFILE
done
else
echo "Directory $CRED doesn't exist or is unaccessible"
fi
# Check if there are only 4-5 backups saved and delete old ones
if cd $LOCATION; then
find $LOCATION -mtime +62 -exec rm {} \;
else
echo "$DATE $LOCATION doesn't exist, deletion wasn't executed" >> $LOGFILE
fi