A small guide to: "CMSMS remote control with bash scripting"
Posted: Sun Aug 09, 2009 6:10 pm
Thanks to precious hints received from calguy1000, here you can find a smarty way to control your CMSMS site without opening a browser, manually logging-in and (best thing) with scheduled automatic bash scripts (GNU/Linux)
For security reason I suggest you to logon on CMSMS only via SSL. This prevents password sniffing.
Another safety measure is to protect bash script from unwanted reading. So set appropriate file permissions for the scripts.
Copy these scripts in a file (ie cmsms-rc.sh) and make it executable. (chmod +x cmsms-rc.sh)
The common part is login script that must be present in all control scripts.
Then you can operate on what part of CMSMS you need.
Clear cache
Set site offline: (Enable Site Down)
Set site online: (Disable Site Down)
And now the best action you can do...automatic backup of your site with Mysql Dump and FileBackup
Mysqldump
Filebackup:
These scripts can be invoked from cron, so you can schedule your job.
Almost any actions triggered in CMSMS can be replicated via scripting. If you install "Live HTTP Headers" plugin for Firefox you can read all url/get/post value when you trigger an action in you browser.
These scripts are only raw examples that can be expandend according your own needs. Moreover I haven't put any control to response value, so I don't check if there are errors or other response code from CMSMS. If you want you can improve scripts and post on the forum.
Hope this helps
regards
blast
For security reason I suggest you to logon on CMSMS only via SSL. This prevents password sniffing.
Another safety measure is to protect bash script from unwanted reading. So set appropriate file permissions for the scripts.
Copy these scripts in a file (ie cmsms-rc.sh) and make it executable. (chmod +x cmsms-rc.sh)
The common part is login script that must be present in all control scripts.
Code: Select all
#!/bin/bash
username="yourusername"
password="yourpasswordhere"
sitename="http://www.yourdomainhere.com"
_cookies="./cookie"
keyname="sp_"
admin_dir="admin"
# ************** Do the login ******************************
_postdata="username=${username}&password=${password}&loginsubmit=Submit"
wget -o log -O outfile.1 --save-cookies ${_cookies} --keep-session-cookies --post-data=$_postdata ${sitename}/$admin_dir/login.php
_x=`grep -c 'password incorrect' outfile.1`
if [ $_x -gt 0 ]; then
echo "FATAL: Authentication error";
exit 1
fi
# Get the session key
_line=`cat $_cookies | grep ${keyname}`
if [ ${line:-notset} != notset ]; then
_sessionkey=`cat $_cookies | grep ${keyname} | tr '\t' , | cut -d, -f7`
else
_sessionkey=`cat log | grep ^Location | cut -d= -f2 | cut -d" " -f1`
fi
Clear cache
Code: Select all
# Clear cache
_postdata="clearcache=clear&submit=Submit"
wget -o log -O outfile.1 --load-cookies ${_cookies} --post-data=$_postdata ${sitename}/$admin_dir/siteprefs.php?${keyname}=${_sessionkey}
Code: Select all
# Set site offline
_postdata="active_tab=sitedown&editsiteprefs=true&enablesitedownmessage=0&enablesitedownmessage=1&sitedownmessage=%3Cp%3ESite+is+currently+down+for+maintenance.%3C%2Fp%3E&sitedownexcludes=&submit=Submit"
wget -o log -O outfile.1 --load-cookies ${_cookies} --post-data=$_postdata ${sitename}/$admin_dir/siteprefs.php?${keyname}=${_sessionkey}
Code: Select all
# Set site online
_postdata="active_tab=sitedown&editsiteprefs=true&enablesitedownmessage=0&sitedownmessage=%3Cp%3ESite+is+currently+down+for+maintenance.%3C%2Fp%3E&sitedownexcludes=&submit=Submit"
wget -o log -O outfile.1 --load-cookies ${_cookies} --post-data=$_postdata ${sitename}/$admin_dir/siteprefs.php?${keyname}=${_sessionkey}
Mysqldump
Code: Select all
# ************** Mysqldump ***************************
# Create Mysqldump download to your local PC
admin_file="moduleinterface.php"
date=`date '+%Y-%m-%d_%H-%M-%S'`
out_file="backup-$date.sql"
# Launch Mysqldump
querystring="mact=MysqlDump%2Cm1_%2Cdumpdatabase%2C0&m1_filename=$out_file&m1_backup=Backup+Database&m1_path=%2Fusr%2Fbin&m1_options=--opt+--verbose+--compatible%3Dmysql40+--default-character-set%3Dlatin1"
wget -a log -O outfile1.htm --load-cookies ${_cookies} ${sitename}/$admin_dir/$admin_file?${keyname}=${_sessionkey}\&$querystring
# Retrieve file
wget -a log -O $out_file --load-cookies ${_cookies} ${sitename}/$admin_dir/backups/$out_file &
wait $!
# Delete backup from server
querystring="mact=MysqlDump%2Cm1_%2Cdeletedataset%2C0&m1_filename=%2Fvar%2Fwww%2Fadmin%2Fbackups%2F$out_file&m1_delete=Delete"
wget -a log -O outfile1.htm --load-cookies ${_cookies} ${sitename}/$admin_dir/$admin_file?${keyname}=${_sessionkey}\&$querystring
Code: Select all
# ************** Filebackup ***************************
# Do a backup with Filebackup
admin_file="moduleinterface.php"
date=`date '+%Y-%m-%d_%H-%M-%S'`
out_file="filebackup-$date.tgz"
# Launch file backup
#querystring="mact=FileBackup%2Cm1_%2Cdumpdatabase%2C0&m1_filename=$out_file&m1_backup=Backup+Files&m1_path=%2Fbin&m1_bck_options=-cpzvf"
#wget -a log -O outfile1.htm --load-cookies ${_cookies} ${sitename}/$admin_dir/$admin_file?${keyname}=${_sessionkey}\&$querystring
# Retrieve file
#wget -a log -O $out_file --load-cookies ${_cookies} ${sitename}/$admin_dir/backups/$out_file &
#wait $!
# Delete backup from server
#querystring="mact=FileBackup%2Cm1_%2Cdeletedataset%2C0&m1_filename=%2Fvar%2Fwww%2Fadmin%2Fbackups%2F$out_file&m1_delete=Delete"
wget -a log -O outfile1.htm --load-cookies ${_cookies} ${sitename}/$admin_dir/$admin_file?${keyname}=${_sessionkey}\&$querystring
Almost any actions triggered in CMSMS can be replicated via scripting. If you install "Live HTTP Headers" plugin for Firefox you can read all url/get/post value when you trigger an action in you browser.
These scripts are only raw examples that can be expandend according your own needs. Moreover I haven't put any control to response value, so I don't check if there are errors or other response code from CMSMS. If you want you can improve scripts and post on the forum.
Hope this helps
regards
blast