Linux : custom shell framework and scripts for my servers
As a lazy sysadmin, I try to make my servers as autonomous as possible : auto-backup, auto-sync tmpfs, report disk status by email, etc.
For all of that, I use a custom little shell framework and some scripts that I share with all my servers through my own git repository.
I decided to publish theses files : it might be useful for some one ;). The “framework” and configuration variables are in bluemind_shellFramework.zip whereas scripts are in bluemind_shellScripts.zip.
What the framework provides
Description
Basicaly, the framework has the following features :
- Redirect all output to a formated log file (eg: prefix added to every line)
- Log file send to admin
- Error manager (auto umounts, send mail, etc.)
- Provides base functionalities such as directory sync, log writing, mysql backup, directory backup, changes detection in directory, etc.
Output example
The framework allows me to write small scripts that output log file like the following (for instance, a backup script):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[17.04.2013-03:00:03][filesrv] ############################## [17.04.2013-03:00:03][filesrv] Home mirroring for filesrv [17.04.2013-03:00:03][filesrv] ############################## [17.04.2013-03:00:03][filesrv] [17.04.2013-03:00:03][filesrv] Mount mirror partition [17.04.2013-03:00:03][filesrv] Start Syncronization... [17.04.2013-03:00:03][filesrv] [17.04.2013-03:00:03][filesrv] building file list ... done [17.04.2013-03:00:03][filesrv] home/hidden/path/to/some/changed/file [17.04.2013-03:00:03][filesrv] home/hidden/path/to/some/changed/file/again [17.04.2013-03:00:03][filesrv] [17.04.2013-03:00:03][filesrv] sent 2399992 bytes received 34 bytes 62338.34 bytes/sec [17.04.2013-03:00:03][filesrv] total size is 925955506298 speedup is 385810.61 [17.04.2013-03:00:48][filesrv] [17.04.2013-03:00:48][filesrv] Unount mirror partition [17.04.2013-03:00:49][filesrv] [17.04.2013-03:00:49][filesrv] ############################## [17.04.2013-03:00:49][filesrv] |
Example scripts
To give some ideas of the framework usage, bellow is 2 examples of scripts I use (together with cron).
Sync logs and backup files from tmps mountpoint to a static storage, plain, simple:
1 2 3 4 5 |
#/bin/bash . /etc/bluemind/functions synclog syncbak cleanTmp |
Another one to backup whole file system and mysql databases with automatic detection of dedicated mount points for /boot and/or /var :
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 |
#!/bin/bash . /etc/bluemind/functions headerMessage "Starting backup of $HOSTNAME" apt-get clean # set BACKUP_DIR to local if needed if ! [ -d $BACKUP_DIR ] ; then message "no remote backup dir found : using local backup dir" BACKUP_DIR="$BACKUP_LOCAL_DIR" else message "mounting storage disk" mountBackupDir fi message "Starting backup of /" backup "/" $BACKUP_DIR "$BACKUP_FILENAME.ROOT" # make a separate backup if /boot is a partition if [ "`grep /boot /etc/fstab`" ] ; then message "Starting backup of /boot"; mountBoot backup "/boot" $BACKUP_DIR "$BACKUP_FILENAME.BOOT" fi # make a separate backup if /var is a partition if [ "`grep /var /etc/fstab | grep ext4`" ] ; then message "Starting backup of /var"; backup "/var" $BACKUP_DIR "$BACKUP_FILENAME.VAR" fi # mysql database backup if [ -x mysqld ] ; then message "MySQL detected !" backupMysql $BACKUP_DIR "$BACKUP_FILENAME.sql" fi message "Unount storage and snapshot" umountDirs footerMessage exitOK "backup" |