-
Notifications
You must be signed in to change notification settings - Fork 0
/
backupdate_drupal.cron
96 lines (78 loc) · 3.44 KB
/
backupdate_drupal.cron
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
#!/bin/sh
## Drupal Daily backup + update. 1 week retention.
## Searches for any Drupal installs in /srv,
## backs them up, updates them,
## resets ownership, then restarts apache.
BACKUPDIR=/var/local/backups/drupal # backup destination
BACKUPOWNER=root:wheel # unix owner and group of the backups. owner can write, group can read.
SITESOWNER=apache:apache # unix owner and group of /srv.
## Don't edit below here.
PATH=/usr/local/bin:/usr/bin:/bin:/sbin
SUDO=''
if (( $EUID != 0 )); then
SUDO='/usr/bin/sudo'
fi
cd /srv
printf "Searching for Drupal sites.\n"
$SUDO find -L -wholename "./*/drupal/sites/*/settings.php" 2>/dev/null | while read -r SITE ; do
#$SUDO find -path "./srv/*/drupal/sites/*/settings.php" | while read -r SITE ; do
SITEPATH=`echo $SITE | /bin/sed -e "s,^.\(.*/drupal\)\(.*\),\/srv\1,g"`
printf "Backing up $SITEPATH\n"
DBNAME=`$SUDO drush status --fields=db-name -r $SITEPATH | $SUDO cut -d ":" -f 2 | $SUDO xargs`
printf "\t$DBNAME\n"
SITEBACKUPDIR=$BACKUPDIR/$DBNAME
TEMPDIR=$BACKUPDIR/temp # Temp space for operators
TIMEDIR=$SITEBACKUPDIR/last-full # where to store time of full backup
DOW=`date +%a` # Day of the week e.g. Mon
DOM=`date +%d` # Date of the Month e.g. 27
DM=`date +%d%b` # Date and Month e.g. 27Sep
# Make destination dirs if they don't exist
$SUDO mkdir -p $SITEBACKUPDIR;
$SUDO mkdir -p $TEMPDIR;
$SUDO mkdir -p $TIMEDIR;
## Set Drupal perms - allows group write
$SUDO find $SITEPATH -type d -exec chmod u=rwx,g=rwx,o= '{}' \;
$SUDO find $SITEPATH -type f -exec chmod u=rw,g=rw,o= '{}' \;
# Daily DB dump
$SUDO drush sql-dump -r $SITEPATH | $SUDO gzip -9 > $SITEBACKUPDIR/$HOSTNAME-$DOW.sql.gz
# Weekly full backup
if [ $DOW = "Sun" ]; then
NEWER=""
NOW=`date +%d-%b`
# Update full backup date
$SUDO echo $NOW > $TIMEDIR/$HOSTNAME-full-date
# Backup the parent folder of the drupal install to capture all assets
$SUDO tar $NEWER -czf $SITEBACKUPDIR/$HOSTNAME-$DOW.tar.gz $SITEPATH/../
# Make incremental backup - overwrite last weeks
else
# Get date of last full backup
NEWER="--newer `$SUDO cat $TIMEDIR/$HOSTNAME-full-date`"
# Backup the parent folder of the drupal install to capture all assets
$SUDO tar $NEWER -czf $SITEBACKUPDIR/$HOSTNAME-$DOW.tar.gz $SITEPATH/../
fi
# Set permissions for backups to root-writeable and wheel readable
printf "Setting permissions for $SITEBACKUPDIR\n"
$SUDO chown -R $BACKUPOWNER $BACKUPDIR
$SUDO chown -R $BACKUPOWNER $TIMEDIR
$SUDO chmod 750 $BACKUPDIR
$SUDO chmod 750 $SITEBACKUPDIR
$SUDO chmod 750 $TIMEDIR
$SUDO chmod 650 $SITEBACKUPDIR/$HOSTNAME-$DOW.tar.gz
$SUDO chmod 650 $SITEBACKUPDIR/$HOSTNAME-$DOW.sql.gz
$SUDO chmod 650 $TIMEDIR/$HOSTNAME-full-date
# Wheel can write to temp space
$SUDO chmod 770 $TEMPDIR
printf "Updating $SITEPATH\n"
$SUDO cp $SITEPATH/.htaccess /tmp/
$SUDO drush -y en update -r $SITEPATH
$SUDO drush rf -r $SITEPATH
$SUDO drush up -y --security-only -r $SITEPATH
$SUDO drush -y dis update -r $SITEPATH
$SUDO cp /tmp/.htaccess $SITEPATH/
$SUDO rm /tmp/.htaccess
printf "\n\n"
done
printf "Setting /srv permissions and restarting apache.\n"
$SUDO chown -R $SITESOWNER /srv
$SUDO service httpd restart
printf "Done.\n"