Skip to content

Instantly share code, notes, and snippets.

@CNG
Created March 16, 2017 11:03
Show Gist options
  • Save CNG/603f5710e464f5d0bed541e395188a1d to your computer and use it in GitHub Desktop.
Save CNG/603f5710e464f5d0bed541e395188a1d to your computer and use it in GitHub Desktop.
Movable Type: Backup DB and app, set up publish queue, logging and PQManager plugin
MT=/var/www/cgi-bin/mt
ADMIN="arck"
if test -L $MT; then
# dereference symlink
TEMP=$(pwd)
cd -P "$MT"
MT=$(pwd)
cd $TEMP
fi
MTCFG=$MT/mt-config.cgi
# Gather database credentials
DBUSER=$(sed -nr 's/^DBUser\s+(.*)/\1/p' $MTCFG)
DBPASS=$(sed -nr 's/^DBPassword\s+(.*)/\1/p' $MTCFG)
DBHOST=$(sed -nr 's/^DBHost\s+(.*)/\1/p' $MTCFG)
DBNAME=$(sed -nr 's/^Database\s+(.*)/\1/p' $MTCFG)
# Test database credentials
if mysqlshow -h $DBHOST -u $DBUSER -p$DBPASS | grep -q $DBNAME; then
echo "Confirmed access to $DBUSER@$DBHOST:$DBNAME"
else
echo "ERROR: Unable to verify database credentials."
fi
# Backup DB and application
cd /tmp
TIMESTAMP=$(date +%Y-%m-%d-%H.%M.%S)
echo "Dumping database to $(pwd)/mt-$TIMESTAMP.sql.gz"
mysqldump --default-character-set=utf8 -a -h$DBHOST -u$DBUSER -p$DBPASS $DBNAME | gzip -c | cat > mt-utf8-$TIMESTAMP.sql.gz;
# can include following in case DB isn't utf8 # TODO: actually check
# mysqldump -a -h$DBHOST -u$DBUSER -p$DBPASS $DBNAME | gzip -c | cat > mt-$TIMESTAMP.sql.gz;
STATICPATH=$(sed -nr 's/^StaticFilePath\s+(.*)/\1/p' $MTCFG)
## if grep -qP '^StaticFilePath ' $MTCFG; then # MT static is defined
if [[ $STATICPATH == $MT* ]] || [[ $STATICPATH == "" ]]; then
# MT static is within main MT dir or not defined, only one archive needed
echo "Application size: $(du -shL $MT)"
tar -czf mt-$TIMESTAMP.tgz $MT;
echo "Archived application to $(pwd)/mt-$TIMESTAMP.tgz"
else
# MT static is outsite MT dir, archive separately
echo "Application size: $(du -shLc $STATICPATH $MT | tail -n 1 | awk '{print $1}')"
tar -czf mt-app-$TIMESTAMP.tgz $MT;
tar -czf mt-static-$TIMESTAMP.tgz $STATICPATH;
echo "Archived application to $(pwd)/mt-$TIMESTAMP.tgz and mt-static-$TIMESTAMP.tgz"
fi
SITE_PATH=$(mysql -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME -e 'SELECT blog_site_path FROM mt_blog WHERE blog_class="website" LIMIT 1' -B --disable-column-names)
INDEX=$(find $SITE_PATH -maxdepth 1 -name "index.*" | head -1)
WEBUSER=$(ls -ld $INDEX | awk '{print $3}')
WEBGROUP=$(ls -ld $INDEX | awk '{print $4}')
# Set up MT background publishing log rotation
LOGCFG="/etc/logrotate.d"
MTLOGDIR="/var/log/movabletype"
if [ -d $LOGCFG ]; then
# set up directory for MT RPT logs
sudo mkdir -p $MTLOGDIR
sudo chown -R $WEBUSER:$WEBGROUP $MTLOGDIR
# add config for logrotate
RPTCFG="$MTLOGDIR/rpt.log {\n missingok\n notifempty\n daily\n rotate 30\n copytruncate\n}\n"
echo -e $RPTCFG | sudo tee --append "$LOGCFG/movabletype-rpt"
# add scheduled task a la http://stackoverflow.com/a/8106460/172602
COMMAND="cd $MT; perl ./tools/run-periodic-tasks -verbose >> $MTLOGDIR/rpt.log 2>&1"
JOB="*/3 * * * * $COMMAND"
cat <(fgrep -i -v "$COMMAND" <(sudo crontab -l -u $WEBUSER)) <(echo "$JOB") | sudo crontab -u $WEBUSER -
# check job was indeed added to crontab
CRONTAB=$(sudo crontab -l -u $WEBUSER | tail -1)
if [ "$CRONTAB" == "$JOB" ]; then
echo "Scheduled task added"
else
echo "ERROR: Scheduled task not added. Check crontab -e -u $WEBUSER"
fi
else
echo "Logrotate config not in expected location, skipping logging setup"
fi
echo "Configuring RPTProcessCap"
if grep ^RPTProcessCap $MTCFG; then
echo "NOTICE: RPTProcessCap already set"
else
# Cap at number of processors
TMP="RPTProcessCap $(nproc)"
echo "Appending $TMP to mt-config.cgi"
echo -e "\n$TMP\n" | sudo tee --append "$MTCFG"
fi
echo "Installing PQManager"
if [ -d $MT/plugins/PQManager ]; then
echo "NOTICE: PQManager already exists in plugins"
else
cd /tmp
wget -q -O mt-plugin-pqmanager.zip https://github.com/endevver/mt-plugin-pqmanager/archive/master.zip
unzip -uqq mt-plugin-pqmanager.zip
if sudo mv mt-plugin-pqmanager-master/plugins/PQManager $MT/plugins; then
sudo chown -R $WEBUSER:$WEBGROUP $MT/plugins/PQManager
perl $MT/tools/upgrade --name $ADMIN
echo "Installed PQManager"
else
echo "ERROR: Failed to install PQManager to plugins (code $?)"
fi
rm -f mt-plugin-pqmanager.zip
rm -rf mt-plugin-pqmanager-master
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment