User:Arenium

From Gentoo Linux Wiki

Jump to: navigation, search

[edit] Simple Scripts

Mostly witten in Bash. Ick. Over the next few months, I'd like to see some Python or PHP slapped up here, but we'll see.

[edit] Scheduled, Scripted System Updating

Everybody wants an up to date system, right? Slacking on any sort of update regiment will eventuate in a system needing some serious TLC -- in the form of compile time. Ouch. This is just an example of an update script written in Bash. Naturally Bash isn't "my thing"; this alone took a handful of hours...but isn't that what learning a shell language with 4 different syntaxes for an if/then conditional statement supposed to do?

Use, change, analyze, comment on, laugh at, anything. Ideally used with cron (duh) and a working mail implementation.

File: update.sh
#!/bin/bash
# update.sh
# @desc
#       Script to work with CRON to ensure only desired
#       `emerge -pvuD world' output gets emailed
#       And ensure that no compiles are interrupted
#       Invoke with a cron entry similar to:
#       0 4 * * *       /bin/sh /root/cron-scripts/update.sh
# @author Kevin Martin
# @date   6/22/05

#
# First make sure nothing else is emerging...
#

NUM=`ps ax | grep 'emerge' | grep -v 'grep' | wc -l`
MINUTES=1
while (( $NUM > "0" ))
do
        echo "NOTICE: Emerge seems to be busy -- waiting $MINUTES minute(s) until retry"
        sleep ${MINUTES}m
        NUM=`ps ax | grep 'emerge' | grep -v 'grep' | wc -l`
done

#
# Proceed.
#

echo "+============ BEGIN ============+"
# Sync first
emerge --sync > /dev/null 2>&1
# Emerge next: the -pvuD will be output
echo
emerge --nospinner -pvuD world
CONTINUE=`emerge -pvuD world | grep '0 kB' | wc -l`
if (( $CONTINUE == "1" )); then # It's an estimate
        echo; echo "Nothing to emerge; exiting."; echo
        echo "+============  END  ============+"
        exit
else
        emerge -uD world > /dev/null 2>&1
        # Keep the stderr output if you want. Just be aware you'll
        # Also see slightly truncated output of the downloading process
        echo; echo "+============  END  ============+"
fi
# EOF

[edit] Scheduled, Scripted Backup Process

Here it is. It's essentially a one liner with the time function just so there's something to look at in the e-mails. Designed to work once SSH (and Keychain) have been setup to accept passwordless authorizations. This was a lifesaver when my harddrive housing Gentoo died of a bad sector. Don't forget to untar a stage and untar a portage snapshot before rsync'ing this stuff back when recovering. You may find it a bit tough to work without a /dev or /usr/portage. :D

NOTE: This is based off of the Backup guide.

File: backup.sh
#!/bin/bash
# The following line allows the passwordless connections
source ~/.keychain/arenium-sh
echo "+============ BEGIN ============+"
echo "             Incremental Rsync Backup"
echo "The backup took: "
time rsync -e ssh -axzl --delete --delete-excluded \
--exclude "/mnt/" --exclude "/proc/" --exclude ".ccache" \
--exclude "/sys/" --exclude "/dev/" --exclude "/usr/portage/" --exclude "/home/kevin/.mozilla/firefox/cnwt2y41.default/Cache/" \
/* root@node:/backup/arenium/
echo; echo "+============  END  ============+"
#EOF
Personal tools