Skip to main content.
Find on this site:

Me as a baby.Mark A. Taff
Location: Home Code Viewer
  1.  #!/bin/bash
  2.  
  3.  # backup.websites
  4.  
  5.  # This script creates backups of the websites.
  6.  # Copyright 2006 Mark A. Taff <marktaff@comcast.net>
  7.  # Licensed under the GNU GPL, version 2.
  8.  
  9.  # Only keep backups for this many fortnights
  10.  FORTNIGHT=12; # A fortnight is 14 days
  11.  
  12.  # Assume failure
  13.  STATUS=failure;
  14.  
  15.  # Get list of websites
  16.  for spec in `grep -v \# /home/mark/bin/backup.websites.specfile | grep @`; do
  17.   leftPart=${spec%@*};
  18.   rightPart=${spec#*@};
  19.  
  20.   user=${leftPart%:*};
  21.   pw=${leftPart#*:};
  22.   server=${rightPart%:*};
  23.   path=${rightPart#*:};
  24.   dt=`date +"%Y%m%d"`;
  25.  
  26.   # Make backup directory, if needed
  27.   localPath=/backups/$server/current;
  28.   datePath=/backups/$server/$dt;
  29.  
  30.   # Check for dated archive with today's date. If exists, prompt if
  31.   # we should skip this website.
  32.   if [ -d $datePath ]; then
  33.   echo;
  34.   echo " The backup $datePath already exists."
  35.   echo " Do you want to overwrite the existing backup, or skip to the next website to archive?";
  36.  
  37.   OPTIONS="Overwrite Skip";
  38.   select opt in $OPTIONS; do
  39.   if [ "$opt" = "Skip" ]; then
  40.   echo "Skipping archive of $server.";
  41.   continue 2;
  42.   elif [ "$opt" = "Overwrite" ]; then
  43.   echo "Overwriting archive $datePath";
  44.   break 1;
  45.   else
  46.   clear;
  47.   echo "Bad option. Enter number 1 or number 2. Press the enter key to redisplay the options.";
  48.   fi
  49.   done;
  50.   fi
  51.  
  52.   mkdir -p $localPath;
  53.   mkdir -p $datePath;
  54.  
  55.   # FTP files down
  56.   echo Mirroring $server
  57.   echo "Command is: lftp -u $user,$pw $server -e 'mirror --verbose=1 $path $localPath; exit;'"
  58.   mirror=`lftp -u $user,$pw $server -e "mirror --verbose=1 $path $localPath; exit;"`;
  59.  
  60.   # Create a dated archive copy of current
  61.   echo Creating dated archive of $localPath;
  62.   archive=`cp -a -f $localPath/* $datePath`;
  63.   # cp -a -f /backups/ksbn.net/current/ /backups/www.ksbn.net/20060613
  64.  
  65.   if [ "$mirror"=0 ] && [ "$archive"=0 ]; then
  66.   STATUS=success;
  67.   fi
  68.  
  69.   # tar + gzip all archives older than 1 fortnight
  70.   if [ "$STATUS"="success" ]; then
  71.   oldDate=`date --date="-1 fortnight" +'%Y%m%d'`;
  72.  
  73.   for i in `ls --hide=*tar.gz --hide=current /backups/$server`; do
  74.   if [ $i -le $oldDate ]; then
  75.   echo Compressing aged backup of $server dated $i, and removing uncompressed version;
  76.  
  77.   # Compress
  78.   `tar -czf /backups/$server/$i.tar.gz /backups/$server/$i/`
  79.   if [ -f /backups/$server/$i.tar.gz ]; then
  80.   # Remove
  81.   `rm -f -r /backups/$server/$i`;
  82.   else
  83.   echo "Compression of archive failed. Removal of uncompressed version aborted."
  84.   fi
  85.  
  86.   fi
  87.   done;
  88.   fi
  89.   echo;
  90.  
  91.  
  92.   # Remove aged backups
  93.   if [ "$STATUS"="success" ]; then
  94.   oldDate=`date --date="-$FORTNIGHT fortnight" +'%Y%m%d'`;
  95.  
  96.   for i in `ls /backups/$server/*.tar.gz|cut -d / -f 4|cut -d . -f 1`; do
  97.   if [ $i -le $oldDate ]; then
  98.   echo Deleting aged backup of $server dated $i;
  99.   `rm -f -r /backups/$server/$i.tar.gz`;
  100.   fi
  101.   done;
  102.   echo Successfully archived $server;
  103.   else
  104.   # Failed to backup files
  105.   echo Failed to archive $server;
  106.   fi
  107.   echo;
  108.  done;
CC-GNU GPL
Photos & software licensed under the CC-GNU GPL
unless otherwise noted in the JPEG comments field or source code.