[LBo] howto generate file names with dates for TAR backups?

Chris F.A. Johnson cfajohnson at teksavvy.com
Wed Feb 7 21:35:07 CET 2007


On Wed, 7 Feb 2007, Andrew Henry wrote:

>> export FN=$(date +%Y-%m-%d-%a-%Hh%M.tgz)
>
> Ok thanks a lot for that tip, it worked a treat, but now the next issue...
>
> I want to autogenerate a logfile for the backup and i've already made an
> attempt at supplying a few things, but how do I get the CPU info,
> architecture and Linux version to be sent to the logfile?
>
> For CPU, I tried grep model\ name /proc/cpuinfo >> README but it does
> not work.  I also tried a pipe, but I do not think I am using it correctly:

    What does "does not work" mean? What, exactly, *does* happen?

    What happens when you give the command without redirection?

    Does it work at the command line?

    Did you try:

grep 'model name' /proc/cpuinfo

> grep model\ name /proc/cpuinfo | echo - README

    echo prints its arguments; it does not read stdin.

> I tried similar techniques with uname -r for the system version and
> $arch for architecture, but im obviosly missing something.

    Again, please describe exactly what you used and what happens.

> Here is my nearly complete script so you can see what I'm trying to attempt:
>
> #!/bin/sh
> ## remove previous snar files as performing a new level 0 backup
> ## root must run this script to be able to create the /var/log/ files
>
> ## depending on which level is being backuped up, comment out the
> relevant lines below

    When posting a script to a mailing list or newsgroup, please ensure
    that you post a valid script. If your mail or news client wraps
    lines, keep the lines short to prevent that (or get a better
    client).

> ## Level-0 header
> rm -f /var/log/snar*
>
> ## Level-1 header
> # cp /var/log/snar /var/log/snar-1
>
> ## Level-2 header
> # cp -u /var/log/snar /var/log/snar-1
> # cp /var/log/snar-1 /var/log/snar-2
>
> ## environment variables must be changed for different incremental levels
> export LVL=backup-lvl0-yearly
> export STAMP=$(date +%a-week%V-%Y%m%d-%Hh%M-%Z)
> export TARGET_DIR=/media/ieee1394disk/backups
> export SNARFILE=/var/log/snar

    YOu don't need to export variables unless thay are needed by a
    script you are calling from *this* script.

> ## important:
> ## cannot use --exclude as this breaks incrementals (fixed in tar 1.16).
> ## cannot use -z to gzip within tar command as this is unsupported with
> incrementals.
> ## need to run as root in case there are other users in /home
>
> echo "Archiving files..."
> tar -cvPWf \
>    $TARGET_DIR/$LVL-$STAMP.tar \
>    --listed-incremental $SNARFILE \
>        /home/andrew/ \
>        '/media/hda2/Documents and Settings/All Users/Documents/My
> Pictures/' \

    See above regarding word wrap.

>        '/media/hda2/Documents and Settings/TME/My Documents/' \
>        '/media/hda2/Documents and Settings/TME/Application Data/GnuPG/' \
>        '/media/hda2/Documents and Settings/TME/Application Data/Mozilla/' \
>        '/media/hda2/Documents and Settings/TME/Application
> Data/Thunderbird/' \

     Ditto

>            > $TARGET_DIR/$LVL-$STAMP.log
>
> ## make a backup of the Master Boot Record
> dd if=/dev/hda of=$TARGET_DIR/$LVL-$STAMP.MBR bs=512 count=1
>
> ## to restore MBR manually...
> #dd if=MBR-backup of=/dev/hda bs=512 count=1
>
> echo "Compressing archive with gzip..."
> gzip -v $TARGET_DIR/$LVL-$STAMP.tar
>
> ## make me the owner
> chown andrew:andrew $TARGET_DIR/$LVL-$STAMP.*
>
> ## encrypt the tarball
> echo "Encrypting archive with GnuPG..."
> gpg -r adhenry at bredband.net -o $TARGET_DIR/$LVL-$STAMP.tar.gz.gpg -ve
> $TARGET_DIR/$LVL-$STAMP.tar.gz
>
> ## remove tgz file as GPG doesn't do this automatically
> rm -f $TARGET_DIR/$LVL-$STAMP.tar.gz
>
> ## root gpg'd file so change ownership to me
> chown andrew:andrew $TARGET_DIR/$LVL-$STAMP.*
>
> ## use split to split a multiGIG file into manageable 2GB parts for
> burning to DVD
> #echo "Splitting encrypted archive into 2GB chunks..."
> #cd $TARGET_DIR
> #split -b 2147483648 $TARGET_DIR/$LVL-$STAMP.tar.gz.gpg
> #chown andrew:andrew $TARGET_DIR/xa*
>
> ## autocreate README for backup
> echo "backup-type       =  $LVL" > $TARGET_DIR/$LVL-$STAMP.README
> echo "backup-date       =  $STAMP" >> $TARGET_DIR/$LVL-$STAMP.README
> echo "backup-dirs       =  /home/andrew, Windows Users" >>
> $TARGET_DIR/$LVL-$STAMP.README
> echo "backup-file-name  =  $LVL-$STAMP.tar.gz.gpg" >>
> $TARGET_DIR/$LVL-$STAMP.README
> echo " " >> $TARGET_DIR/$LVL-$STAMP.README
> echo "Backup performed on Ubuntu 6.06 amd64 with following utilities:"
>>> $TARGET_DIR/$LVL-$STAMP.README
> echo " " >> $TARGET_DIR/$LVL-$STAMP.README
> tar --version >> $TARGET_DIR/$LVL-$STAMP.README
> echo " " >> $TARGET_DIR/$LVL-$STAMP.README
> gzip --version >> $TARGET_DIR/$LVL-$STAMP.README
> echo " " >> $TARGET_DIR/$LVL-$STAMP.README
> gpg --version >> $TARGET_DIR/$LVL-$STAMP.README

   Your script will be easier to read if you group you commands and use
   a single redirection:

{
   echo "backup-type       =  $LVL"
   echo "backup-date       =  $STAMP"
   echo "backup-dirs       =  /home/andrew, Windows Users"
   echo "backup-file-name  =  $LVL-$STAMP.tar.gz.gpg"
   echo " "
   echo "Backup performed on Ubuntu 6.06 amd64 with following utilities:"
   echo " "
   tar --version
   echo " "
   gzip --version
   echo " "
   gpg --version
} > $TARGET_DIR/$LVL-$STAMP.README

-- 
    Chris F.A. Johnson                      <http://cfaj.freeshell.org>
    ===================================================================
    Author:
    Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)


More information about the QnA mailing list