Posts tagged “script”


September 3, 2010
  Mono / FastCGI Startup Script

We’ve begun running Mono on some Bit Badger Solutions servers to enable us to support the .NET environment, in addition to the PHP environment most of our other applications use. While Ubuntu has nice packages (and Badgerports even brings brought them up to the latest release), one thing that we were missing was a “conf.d”-type of configuration; my “/applications=” clause of the command was getting really, really long. We decided to see if we could create something similar to Apache / Nginx’s sites-available/sites-enabled paradigm, and we have succeeded!

To begin, you’ll need to create the directories /etc/mono/fcgi/apps-available and /etc/mono/fcgi/apps-enabled. These directories will hold files that will be used define applications. The intent of these directories is to put the actual files in apps-available, then symlink the ones that are enabled from apps-enabled. These files have no name restrictions, but do not put an extra newline character in them. The script will concatenate the contents of that file to create the MONO_FCGI_APPLICATIONS environment variable, which tells the server what applications exist. (The syntax is the same as that for the “/applications=” clause - [domain]:[URL path]:[filesystem path].) Here’s how the site you’re reading now is configured (from the file djs-consulting.com.techblog.conf)…

djs-consulting.com.techblog.conf
1
techblog.djs-consulting.com:/:/path/to/install/base/for/this/site

Finally, what brings it all together is a shell script. This should be named “monoserve” and placed in /etc/init.d. (This borrows heavily from this script a script we found online, which we used until we wrote this one.) Note the group of variables surrounded by the “make changes here” notes - these are the values that are used in starting the server. They are at the top so that you can easily modify this for your own needs.

monoserve
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
#/bin/bash
### BEGIN INIT INFO
# Provides: monoserve.sh
# Required-Start: $local_fs $syslog $remote_fs
# Required-Stop: $local_fs $syslog $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start FastCGI Mono server with hosts
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/mono
NAME=monoserver
DESC=monoserver
## Begin -- MAKE CHANGES HERE --
PROGRAM=fastcgi-mono-server2 # The program which will be started
ADDRESS=127.0.0.1 # The address on which the server will listen
PORT=9001 # The port on which the server will listen
USER=www-data # The user under which the process will run
GROUP=$USER # The group under which the process will run
## End -- MAKE CHANGES HERE --
# Determine the environment
MONOSERVER=$(which $PROGRAM)
MONOSERVER_PID=""
FCGI_CONFIG_DIR=/etc/mono/fcgi/apps-enabled
# Start up the Mono server
start_up() {
get_pid
if [ -z "$MONOSERVER_PID" ]; then
echo "Configured Applications"
echo "-----------------------"
# Construct the application list if the configuration directory exists
if [ -d $FCGI_CONFIG_DIR ]; then
MONO_FCGI_APPLICATIONS=""
for file in $( ls $FCGI_CONFIG_DIR ); do
if [ "$MONO_FCGI_APPLICATIONS" != "" ]; then
MONO_FCGI_APPLICATIONS=$MONO_FCGI_APPLICATIONS,
fi
MONO_FCGI_APPLICATIONS=$MONO_FCGI_APPLICATIONS`cat $FCGI_CONFIG_DIR/$file`
done
export MONO_FCGI_APPLICATIONS
echo -e ${MONO_FCGI_APPLICATIONS//,/"\n"}
else
echo "None (config directory $FCGI_CONFIG_DIR not found)"
fi
echo
# Start the server
start-stop-daemon -S -c $USER:$GROUP -x $MONOSERVER -- /socket=tcp:$ADDRESS:$PORT &
echo "Mono FastCGI Server $PROGRAM started as $USER on $ADDRESS:$PORT"
else
echo "Mono FastCGI Server is already running - PID $MONOSERVER_PID"
fi
}
# Shut down the Mono server
shut_down() {
get_pid
if [ -n "$MONOSERVER_PID" ]; then
kill $MONOSERVER_PID
echo "Mono FastCGI Server stopped"
else
echo "Mono FastCGI Server is not running"
fi
}
# Refresh the PID
get_pid() {
MONOSERVER_PID=$(ps auxf | grep $PROGRAM.exe | grep -v grep | awk '{print $2}')
}
case "$1" in
start)
start_up
;;
stop)
shut_down
;;
restart|force-reload)
shut_down
start_up
;;
status)
get_pid
if [ -z "$MONOSERVER_PID" ]; then
echo "Mono FastCGI Server is not running"
else
echo "Mono FastCGI Server is running - PID $MONOSERVER_PID"
fi
;;
*)
echo "Usage: monoserve (start|stop|restart|force-reload|status)"
;;
esac
exit 0

This needs to be owned by root and be executable (chmod +x monoserve). You can use update-rc.d monoserve defaults to set this to start at boot.

Categorized under , , , ,
Tagged , , , ,


March 28, 2008
  A Handy PHP Backup Script

I found a script over on the Lunarpages Forums about using PHP to back up your site. I have taken it, modified it a little, beefed up the documentation a lot, and am now posting it here. You can copy and paste it from below to customize it for your own use.

backup.php
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
<?php
/**
* Generic Backup Script.
*
* To configure this script for your purposes, just edit the parameters below.
* Once you have the parameters set properly, when the script executes, it will
* create an archive file, gzip it, and e-mail it to the address specified. It
* can be executed through cron with the command
*
* php -q [name of script]
*
* You are free to use this, modify it, copy it, etc. However, neither DJS
* Consulting nor Daniel J. Summers assume any responsibility for good or bad
* things that happen when modifications of this script are run.
*
* @author Daniel J. Summers <daniel@djs-consulting.com>
*/
// --- SCRIPT PARAMETERS ---
/* -- File Name --
This is the name of the file that you're backing up, and should contain no
slashes. For example, if you're backing up a database, this might look
something like...
$sFilename = "backup-my_database_name-" . date("Y-m-d") . ".sql"; */
$sFilename = "backup-[whatever-it-is]-" . date("Y-m-d") . ".[extension]";
/* -- E-mail Address --
This is the e-mail address to which the message will be sent. */
$sEmailAddress = "[your e-mail address]";
/* -- E-mail Subject --
This is the subject that will be on the e-mail you receive. */
$sEmailSubject = "[something meaningful]";
/* -- E-mail Message --
This is the text of the message that will be sent. */
$sMessage = "Compressed database backup file $sFilename.gz attached.";
/* -- Backup Command --
This is the command that does the work.
A note on the database commands - your setup likely requires a password
for these commands, and they each allow you to pass a password on the
command line. However, this is very insecure, as anyone who runs "ps" can
see your password! For MySQL, you can create a ~&#47;.my.cnf file - it is
detailed at //dev.mysql.com/doc/refman/4.1/en/password-security.html .
For PostgreSQL, the file is ~&#47;.pgpass, and it is detailed at
//www.postgresql.org/docs/8.0/interactive/libpq-pgpass.html . Both of
these files should be chmod-ded to 600, so that they can only be viewed by
you, the creator.
That being said, some common commands are...
- Backing Up a MySQL Database
$sBackupCommand = "mysqldump -u [user_name] [db_name] > $sFilename";
- Backing Up a PostgreSQL Database
$sBackupCommand = "pg_dump [db_name] -h localhost -U [user_name] -d -O > $sFilename";
- Backing Up a set of files (tar and gzip)
$sBackupCommand = "tar cvf $sFilename [directory]
Whatever command you use, this script appends .gz to the filename after the command is executed. */
$sBackupCommand = "[a backup command]";
// --- END OF SCRIPT PARAMETERS ---
//
// Edit below at your own risk. :)
// Do the backup.
$sResult = passthru($sBackupCommand . "; gzip $sFilename");
$sFilename .= ".gz";
// Create the message.
$sMessage = "Compressed database backup file $sFilename attached.";
$sMimeBoundary = "<<<:" . md5(time());
$sData = chunk_split(base64_encode(implode("", file($sFilename))));
$sHeaders = "From: $sEmailAddress\r\n"
. "MIME-Version: 1.0\r\n"
. "Content-type: multipart&#47;mixed;\r\n"
. " boundary=\"$sMimeBoundary\"\r\n";
$sContent = "This is a multi-part message in MIME format.\r\n\r\n"
. "--$sMimeBoundary\r\n"
. "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
. "Content-Transfer-Encoding: 7bit\r\n\r\n"
. $sMessage."\r\n"
. "--$sMimeBoundary\r\n"
. "Content-Disposition: attachment;\r\n"
. "Content-Type: Application/Octet-Stream; name=\"$sFilename\"\r\n"
. "Content-Transfer-Encoding: base64\r\n\r\n"
. $sData."\r\n"
. "--$sMimeBoundary\r\n";
// Send the message.
mail($sEmailAddress, $sEmailSubject, $sContent, $sHeaders);
// Delete the file - we don't need it any more.
unlink($sFilename);
Categorized under , , , ,
Tagged , , ,