Index:


SysAdmin Tips and Tricks

  • Restoring files with Amanda

    Amanda is a great backup system, although it tends to be a bit of a bandwidth hog — I use rsync over SSH for most daily backups, and then Amanda for full / weekly backups. 

    Go to the Amanda server and become root. Change to whatever directory you want the files restored to.

    root@ns03 amtest]# /usr/sbin/amrecover daily
    AMRECOVER Version 2.4.5. Contacting server on localhost ...
    220 ns03 AMANDA index server (2.4.5) ready.
    200 Access OK
    Setting restore date to today (2005-12-14)
    200 Working date set to 2005-12-14.
    Scanning /var/tmp...
    host_0: skipping cruft file, perhaps you should delete it.
    200 Config set to daily.
    501 Host ns03 is not in your disklist.
    Trying host ns03...
    200 Dump host set to <MACHINE>.
    Trying disk / ...
    Trying disk rootfs ...
    Can't determine disk and mount point from $CWD '/tmp/amtest'
    amrecover> sethost www.kumari.net
    200 Dump host set to www.kumari.net.
    amrecover> setdisk /home/wkumari
    200 Disk set to /home/wkumari.
    amrecover> ls
    <shows some files!>
    amrecover> add <files / directories, etc>
    amrecover> extract
    Extracting files using tape drive file:/amandatapes/daily on host localhost.
    The following tapes are needed: daily3
    Restoring files into directory /tmp/amtest
    Continue [?/Y/n]? y
    Extracting files using tape drive file:/amandatapes/daily on host localhost.
    Load tape daily3 now
    *** At this point, open another window,, and as user "amanda" do":
    amanda:$> /usr/sbin/amtape daily slot 3
    amtape: changed to slot 3 on file:/amandatapes/daily
    *** Go back to this window...
    Continue [?/Y/n/s/t]? y

    This can now also be done from the client end — just run amrecover from there.

    If you are using an encrypted backup, you will need to place the correct passphrase in /var/lib/amanda/.am_passphrase

  • Redirecting bash STDOUT, STDERR

    For some reason I can never remember this, so I have to store it for reference:

    Bash Redirect STDOUT and STDERR to /dev/null

    grep * &>/dev/null

     

    Bash Redirect STDERR to STDOUT

    This will cause the stderr output of a program to be written to the same file descriptor as stdout.

    grep * 2>&1

     

  • Rsync over SSH backups (backing up UNIX machines)

    Backups are important!

    Many years ago, one of my wife’s clients experienced a hard drive crash on her business machine — unfortunately she had gotten tired of replacing the tapes in her drive, so her backups were completely useless. She was quite upset, so I was looking for something helpful to say — unfortunately the only thing that came to mind was something I had read in some magazine that morning: “Did you know that, according to the Gartner Group, 95% of all companies that experience a data loss like your go out of business within 6 months”.

    The sad thing is that I thought that I was helping…

     


    This is the rsync based backup system that I use to backup my servers / workstations, etc.

    Notes:

    backuppc is the name of the server to back things up to!
    You need to setup SSH keys so that this machine can login to the backup server. I use chroot’ed jails and a key just for this, In the example it just logs in as root.
    This uses rsyncs bandwith limit — it is in KiloBytes, NOT KiloBits!
    This also dumps your MYSQL databases and backs them up.

    ssh keys

    First I need an ssh key so that the client can login to the backup machine. In order to make this more secure, I use “forced command” keys.

     

    root@vimes:~# ssh-keygen -C "SSH Single Use Key -- backup from Source to BackupDest -- generated on Source"
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/source_to_backupdest_rsync
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /root/.ssh/source_to_backupdest_rsync.
    Your public key has been saved in /root/.ssh/source_to_backupdest_rsync.pub.

     

    Now I copy the .pub file over to the backup machine and place it in /root/.ssh/authorized_keys2. The super important bit to do is to add:

    command="/usr/local/sbin/secure_rsync",no-port-forwarding,no-X11-forwarding,no-agent-forwarding 

    before the key to make sure that this key can only be used for backups.

    Example:

    command="/usr/local/sbin/secure_rsync",no-port-forwarding,no-X11-forwarding,no-agent-forwarding ssh-dss AAAdFAaefat4re43tr... 56lblAzAT5K9w== SSH Single Use Key (rsync) -- generated on Homer
     

    I am planning on soon moving to SSH certificates where I can use certificate constraints to further enforce limits, but am waiting to get an HSM first, as having an unprotected CA cert lurking makes me itch..

     

    The list of things to include or exclude is in /etc/backup/backup_include and /etc/backup/backup_exclude. Example files below:

    #!/bin/bash
    unset PATH
    # USER VARIABLES
    BACKUPDIR=/backup # Folder on the backup server
    KEY=/root/.ssh/id_rsa # What key to use to login to the backup server
    MYSQLUSER=root
    MYSQLPWD= **** PUT PASSWORD HERE! ***
    MYSQLHOST=localhost
    MYSQLBACKUPDIR=/mysql_backup
    BACKUP_USER=root@backuppc
    EXCLUDES=/etc/backup/backup_exclude # File containing exludes
    INCLUDES=/etc/backup/backup_include # What to backup.
    # PATH VARIABLES
    CP=/bin/cp;
    MK=/bin/mkdir;
    SSH=/usr/bin/ssh;
    DATE=/bin/date;
    RM=/bin/rm;
    GREP=/bin/grep;
    MYSQL=/usr/bin/mysql;
    MYSQLDUMP=/usr/bin/mysqldump;
    RSYNC=/usr/bin/rsync;
    TOUCH=/bin/touch;
    HOSTNAME=`/bin/hostname`;
    CAT=/bin/cat;
    ##
    # CREATING CURRENT DATE / TIME
    NOW=`$DATE '+%Y-%m'-%d_%H:%M`
    MKDIR=$BACKUPDIR/$NOW/
    # CREATE MYSQL BACKUP
    # Remove existing backup dir
    $RM -Rf $MYSQLBACKUPDIR
    # Create new backup dir
    $MK $MYSQLBACKUPDIR
    #Dump new files
    for i in $(echo 'SHOW DATABASES;' | $MYSQL -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST|$GREP -v '^Database$'); do
    $MYSQLDUMP \
    -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST \
    -Q -c -C --add-drop-table --add-locks --quick --lock-tables \
    $i > $MYSQLBACKUPDIR/$i.sql;
    done;
    # CREATE NEW BACKUPDIR
    $SSH -i $KEY $BACKUP_USER "$MK -p $MKDIR"
    # RUN RSYNC INTO CURRENT
    for TODO in `$CAT ${INCLUDES}`; do
    echo "-------- Begin backup: $TODO --------";
    $SSH -i $KEY $BACKUP_USER "$MK -p $BACKUPDIR/current/$HOSTNAME$TODO"
    $RSYNC \
    -apcz --stats --bwlimit=50 --delete --delete-excluded \
    --exclude-from="$EXCLUDES" \
    -e "$SSH -i $KEY" \
    $TODO/ $BACKUP_USER:$BACKUPDIR/current/$HOSTNAME$TODO ;
    echo "------- End backup: $TODO -------";
    echo
    echo
    echo
    done
    # UPDATE THE MTIME TO REFELCT THE SNAPSHOT TIME
    $SSH -I $KEY $BACKUP_USER "$TOUCH $BACKUPDIR/current"
    # MAKE HARDLINK COPY
    $SSH -i $KEY $BACKUP_USER "$CP -al $BACKUPDIR/current/* $MKDIR"

    backup_include and backup_exclude

    [root@homer ~]#  cat /etc/backup/backup_include 
    /etc
    /home/wkumari
    /root
    /mysql_backup
    /var/www
    /usr/local
    /usr/share
    [root@homer ~]# cat /etc/backup/backup_exclude
    /backup/
    /backups/
    /bin/
    /boot/
    /dev/
    /etc/selinux/
    /lib/
    /lost+found/
    /mnt/
    /media/
    /opt/
    /proc/
    /selinux/
    /sbin/
    /sys/
    /tmp/
    /usr/
    /var/log/
    /var/spool/
    /var/lib/php4/
    /var/lib/mysql/

    crontab invocation

    [root@homer ~]# crontab -l
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=[INSET YOUR EMAIL ADDRESS]
    HOME=/root
    # field allowed values
    # ----- --------------
    # minute 0-59
    # hour 0-23
    # day of month 1-31
    # month 1-12 (or names, see below)
    # day of week 0-7 (0 or 7 is Sun, or use names)
    # M H D M DOW
    # Four times a day, when the minutes are 0, run the backup.
    0 */6 * * * /root/backup.sh 2>&1 | mail -s "Backup report: `date`" [EMAIL ADDRESS] -- -F [EMAIL ADDRESS]