#!/bin/bash # # script to automate (remote) backup and restore using cpio (and remote access via ssk key exchange). # Pete Nesbitt, Dec 2002 # usage: # cpio_backup.sh [backup|restore] [archive file] # Description: # if cpio_backup.sh is called without any arguments, the user will be prompted to supply values or else to confirm internally defined values. # this is the basic idea: # find /home/pete/* -depth -print |cpio -o -H ustar -O /tmp/cpiotest ### SITE SPECIFIC VARIABLES ### # SOURCE: set the path for files to backup SOURCE_FILES="/home/pete/docs/" # TARGET: set remote host use "" for localhost TARGET_HOST="" #"nova:" TARGET_FILE="/home/pete/temp/cpio_test6backup" #"/dev/st0" # ERRORS and MESSAGES CPIO_OUTPUT="/home/pete/temp/cpio_messages.txt" ############################### ### GLOBAL VARIABLES ### # file to hold list of files to backup SOURCE_FILES="/tmp/s_files.tmp" # cleanup for security if [ -e $SOURCE_FILES ]; then rm -f $SOURCE_FILES if [ -e $SOURCE_FILES ]; then printf "\n\nCannot remove old file list $SOURCE_FILES!\nAborting script!\n" exit fi fi ####################### ### cpio Arguments ### CPIO_BACKUP_STRING="-o -H ustar -O $TARGET_HOST$TARGET_FILE" # -H is archive type CPIO_RESTORE_STRING="-i -I $TARGET_HOST$TARGET_FILE" ####################### ### What to do: BACKUP or RESTORE ### # use $1 as the function, else prompt, else default # first, check if there are enough vers, as I havn't figured a better way TOO_MANY_PATHS=$# if [ $TOO_MANY_PATHS -gt 6 ]; then printf "Sorry, there are a maximum of 5 path directories per run.\n" exit fi case $# in 0) echo "usage: cpio_backup.sh [backup|restore] [source files]" ;; 1) echo "usage: cpio_backup.sh [backup|restore] [source files]" ;; *) case $1 in backup) if [ -z $2 ]; then echo "The files or directory to backup does not exist" echo "Please try command again" echo "usage: cpio_backup.sh [backup|restore] [source files]" exit fi for i in $2 $3 $4 $5 $6 do echo $i >> $SOURCE_FILES done # use find to create a list of files to backup # use var to feed find list of paths find_files="`cat $SOURCE_FILES`" INPUT_FILES="/usr/bin/find $find_files -depth -print" # execute cpio using the output from find CPIO_COMMAND="/bin/cpio $CPIO_BACKUP_STRING" exec $INPUT_FILES|$CPIO_COMMAND >> $CPIO_OUTPUT 2>&1 # check the output file for errors, diff from prior # Report to user printf "Backup completed successfully!\n\nThe files in:\n`cat $SOURCE_FILES`\n\nwere backed up to:\n $TARGET_HOST$TARGET_FILE.\n\n" ;; restore) #(jump to restore); echo "sorry, I haven't done this part yet, you'll need to use cpio manually :(" ;; *) echo "usage: cpio_backup.sh [backup|restore] [source files]" ;; esac ;; esac ##################################### ### Cleanup ### if [ -e $SOURCE_FILES ]; then rm -f $SOURCE_FILES fi