Subject: Re: Shell hack -- getting files with dates
To: Steven M. Bellovin <smb@cs.columbia.edu>
From: Jan Danielsson <jan.m.danielsson@gmail.com>
List: netbsd-users
Date: 02/04/2007 18:29:13
This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enig152A1BB891F6F764CD8293D0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Steven M. Bellovin wrote:
[---]
> The return code from egrep is specified by Posix, as I recall.

   Excellent.

>However
> -- why not do
> 	for f in ~/backup/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].gpg

   The problem is that I need to process all the files, even those
without a date -- it's just that I need to determine if they do contain
a date, because they are handled a little differently.

  I have a backup in, say, /backup -- this is unencrypted -- but on an
encrypted partition. Some files here contain dates (these are created
twice a week), and others do not (these are created daily). For instance

   /backup
      documents.2007-01-01.tar.bz2
      documents.2007-01-04.tar.bz2
      documents.2007-01-07.tar.bz2
      documents.2007-01-10.tar.bz2
      documents.tar.bz2              <- updated (overwritten) daily


   I also have a ~/backup. This will be burned onto DVD:s from time to
time. I need these to be encrypted. Before I simply looped through all
files in /backup and encrypted them to ~/backup/<filename>.gpg. However,
that is beginning to take more time than it should. I could save a lot
of time by only encrypting the dated files that have not been encrypted
yet, and the undated files.

   So, I need to process all files, but if the file name contains a date
and its encrypted counterpart does not exist, OR the file name does not
contain a date, then encrypt it.

   I think I may have gotten this to work.. But I feel that it is quite
ugly. (Although a friend of mine would say "shell hacks *are* ugly,
that's why they are so beautiful!" -- so I guess I'm kind of set :-)

   Many thanks to all of you who have helped me with the bits and pieces.=



   In case anyone wonders, this is how I am doing it.. I welcome "You
st*pid sh*t! That won't work!", as long as there are corrections
included. :-)

   Please note: I know virtually NOTHING about shell scripting. The
limit of what I know can be seen below..

-------------------------------------------
#
# First determine of some of the original backups have been removed
# (they do get removed by a script when they are deemed to be too
# old)
#
for filepath in $SDST/*.gpg
do
        filename=3D${filepath##*/}
        ucfile=3D${filename%.gpg}
        if ! [ -f "$SRC/system/$ucfile" ] ; then
                rm "$filepath"
        fi
done


#
# Now, encrypt all the relevant files
#
for file in $SRC/system/*.bz2
do
   do_enc=3D1
   tgtfile=3D$SDST/${file##*/}.gpg
   if echo "$file" | egrep -q '[0-9]{4}-[0-9]{2}-[0-9]{2}' ; then
      # it is a dated file
      echo "Target $tgtfile is a dated file.."
      if [ -f "$tgtfile" ] ; then
         # target file exists
         echo "Target $tgtfile exists.."
         do_enc=3D0
      fi
   fi
   if [ $do_enc -ne 0 ] ; then
      echo "Encrypting $file to $tgtfile"
      nice -n 20 /usr/pkg/bin/gpg -r $RCP --encrypt -o - $file > $tgtfile=

   fi
done
-------------------------------------------


--=20
Kind regards,
Jan Danielsson



--------------enig152A1BB891F6F764CD8293D0
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (NetBSD)

iD8DBQFFxhfubgs4ia4rq9IRChHfAJ9TOr/4z6PYkiEMcGrF/z7TLHffhACfVfnw
JnmeOc5D9po2iz5+M9SHsEY=
=/Mt3
-----END PGP SIGNATURE-----

--------------enig152A1BB891F6F764CD8293D0--