forked from UCLALibrary/drupal_admin_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrupal_sftp_content
executable file
·117 lines (100 loc) · 4.01 KB
/
drupal_sftp_content
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
#!/bin/bash
## Provide a daily copy of the local Drupal instance's content, suitable
## for development environments: MySQL database and static files
## Send it to a subdirectory of our SFTP site www-sftp:/
## This script is run from druadmin's crontab
# Must be this user to run this script
# Assume that this user's ~/.ssh/known_hosts on the remote machine already has
# an entry for the local machine
USER=druadmin
if [ "`/usr/bin/id -urn`" != "$USER" ] ; then
echo "You must be the $USER user to execute this script - exiting."
exit 1
fi
DRUSH=/usr/local/bin/drush
# Get local drush alias
# Drush Alias - only need one as all webheads share the same database instance and the same static files
# All drush aliases can be found in ~/$USER/.drush/aliases.drushrc.php
# Our convention for Drush aliases: @rhel-[test|stage|prod][0-9]
SRC_DRUSH=@`$DRUSH site-alias --local`
if [ $? -ne 0 ] ; then
echo -e "Failed to get alias for local Drupal instance - exiting \n"
exit 1
fi
# We'll create our files in and send them from this temp directory
TMP_DIR=/tmp/`basename $0`.$$
# The SFTP server name
SFTP_SERVER=www-sftp
# We'll send the files to this directory on the SFTP server
# Our convention: /test, /stage and /prod are directories on the SFTP server
SFTP_DIR=/`echo $SRC_DRUSH | sed -e 's/@rhel-//' -e 's/[0-9]*$//'`
# Names for the MySQL dump file: plain and gzipped
SQL_DUMP=$TMP_DIR/`echo $SRC_DRUSH | sed 's/@//'`.sql
SQL_DUMP_GZ=${SQL_DUMP}.gz
# The static files are in one subdirectory: sites_default_files
# Each subdirectory will go into its own gzipped TAR file
STATIC_FILES_ROOT=/var/www/vhosts/webhead
STATIC_FILES_SUBDIR_LIST="sites_default_files"
# Create the temp directory to hold our files
if ! mkdir -m 700 $TMP_DIR; then
# Rely on mkdir's error msg
exit 1
fi
# Create MySQL database dump file
echo -e "\nCreating SQL dump file from $SRC_DRUSH Drupal instance \n"
$DRUSH $SRC_DRUSH sql-dump --result-file=$SQL_DUMP
if [ ! -s $SQL_DUMP ] ; then
echo -e "\nFailed to create the MySQL database dump file $SQL_DUMP - exiting \n"
exit 1
fi
# GZIP the MySQL database dump file
echo -e "\ngzipping $SQL_DUMP \n"
gzip $SQL_DUMP
if [ $? -ne 0 ] ; then
echo -e "Failed to gzip '$SQL_DUMP' - exiting \n"
exit 1
fi
# Create a gzipped TAR file of each static files subdirectory
for SUBDIR in $STATIC_FILES_SUBDIR_LIST; do
# Name of the gzipped TAR file
STATIC_FILES_TAR_GZ=$TMP_DIR/`echo $SRC_DRUSH | sed 's/@//'`_$SUBDIR.tar.gz
# Create the gzipped TAR file
echo -e "\nCreating gzipped $SUBDIR files tar archive... \n"
# -C: tar from within the static files directory
# Per akohler: Exclude the static files subdirectory itself, because it has a different
# name in the development environment
# --exclude:
# OS-X-specific .apdisk files
# OS-X-specific .DS_Store files
# OS-X-specific .TemporaryItems directories
# Windows-specific Thumbs.db files
# ./.check_mount, which Nagios uses to verify that the file system is mounted
tar czf $STATIC_FILES_TAR_GZ -C $STATIC_FILES_ROOT/$SUBDIR . \
--exclude ".apdisk" \
--exclude ".DS_Store" \
--exclude ".TemporaryItems" \
--exclude "Thumbs.db" \
--exclude "./.check_mount" > /dev/null 2>&1
# Exit codes 0 (sucess) and 1 (file changed) are acceptable - anything else is fatal error
# Exit code 1: Periodically Varnish cache runs status.php, which adds and deletes a file in sites_default_files
if [ $? -ne 0 -a $? -ne 1 ] ; then
echo -e "\nFailed to create the '$STATIC_FILES_TAR_GZ' archive - exiting \n"
exit 1
fi
done
# Send the files
# Assume that this is not the very first time that we have connected from here to there
echo -e "\nSending the files to $SFTP_SERVER... \n"
(
# Our convention: /test, /stage and /prod are directories on the SFTP server
echo "ls -l $SFTP_DIR"
echo "put $TMP_DIR/* $SFTP_DIR"
echo "ls -l $SFTP_DIR"
echo "bye"
) | sftp -b - $SFTP_SERVER
if [ $? -ne 0 ] ; then
echo -e "Failed to send one or more files - exiting \n"
exit 1
fi
# Clean up DB dump and static files
rm -rf $TMP_DIR