#!/bin/csh -f # backup_clsi_www1 # ----------------------------------------------------------------------------- # Shell script to backup all modified CLSI files into a fully populated # directory tree, while keeping previous copies of the files in sparse # timestamped directory trees. # ----------------------------------------------------------------------------- # Usage: Typically run as root with no arguments via a cron job. # All output of such cron jobs is automatically e-mailed to root. # Assumptions: # Effects: # - Does backup of CLSI files. # Notes: # Implementation Notes: # Portability Issues: # Revision History: # $Log$ # ----------------------------------------------------------------------------- set source_root = "/ebs" set copies_root = "/ebs/copies" set backup_drive = "/ebs2" set backup_root_relative = "backup" set backup_root_absolute = "${backup_drive}/${backup_root_relative}" set full = "${backup_root_absolute}/full" set sparse_root = "${backup_root_absolute}/PriorTo" set timestamp = `date "+%Y_%m_%d__%H_%M_%S"` set sparse = "${sparse_root}/$timestamp" set excludes = "--exclude=${source_root}/lost+found" @ max_used_space_percent = 80 echo "************************************************************************" echo "Deleting old backups to reduce used space to ${max_used_space_percent}%..." # Note: Count iterations to prevent an infinite loop if there are no more # old backups to delete. @ count = 0 while ($count < 100) @ count++ # Get used space as a percent followed by "%" (e.g., "56%"), and strip # off the "%". @ used_space_percent = `df $backup_drive | tail -1 | awk '{print $5}' | tr -d "%"` echo "" echo "Used space on $backup_drive is ${used_space_percent}%" if ($used_space_percent <= $max_used_space_percent) then if ($count == 1) then echo "No old backups were deleted" endif break endif # Delete oldest sparse backup set oldest_sparse_backup = `ls -drt ${sparse_root}/* | head -1` if ($oldest_sparse_backup == "") then echo "Warning: Can't free more space. No sparse backups exist." break else echo "" echo "Deleting $oldest_sparse_backup..." rm -rv $oldest_sparse_backup endif end echo "************************************************************************" echo "" echo "" echo "" echo "" # Copy other files to /ebs that don't or can't already reside in /ebs, # so that copies of them will be backed up with the rest of the /ebs drive. # For example, /etc/fstab must be found during bootup, before the /ebs # drive is mounted, to tell Linux to mount the /ebs drive. echo "************************************************************************" echo "Copying /etc to ${copies_root}..." mkdir -p -v ${copies_root}/etc echo "rsync -rvltpog --stats --del /etc/ ${copies_root}/etc" rsync -rvltpog --stats --del /etc/ ${copies_root}/etc set rc = $status if ($rc != 0) then echo "Error copying /etc to ${copies_root}" exit $rc endif echo "************************************************************************" echo "" echo "" echo "" echo "" echo "************************************************************************" echo "Copying updated files to $full" echo "and moving old copies to $sparse..." echo "rsync -rvltpog --stats --del $excludes --backup --backup-dir=${sparse} ${source_root}/ ${full}" rsync -rvltpog --stats --del $excludes --backup --backup-dir=${sparse} ${source_root}/ ${full} # -r = Recursive # -v = Verbose # -l = Preserve symbolic links, even those pointing outside the copied tree # -t = Preserve times # -p = Preserve permissions # -o = Preserve owner # -g = Preserve group # --stats = Show detailed stats after the copy, like: # Number of files: 47 # Number of files transferred: 0 # Total file size: 87207 bytes # Total transferred file size: 0 bytes # Literal data: 0 bytes # Matched data: 0 bytes # File list size: 789 # File list generation time: 0.001 seconds # File list transfer time: 0.000 seconds # Total bytes sent: 805 # Total bytes received: 20 # --del = Delete files from destination if missing from source # --backup = Move old files to backup-dir instead of overwriting # or deleting them from the target tree # --backup-dir = Directory tree to store old files set rc = $status if ($rc != 0) then beep "Error copying ${source_root} to $full and $sparse" exit $rc endif echo "************************************************************************" echo "" echo "" echo "" echo "" echo "************************************************************************" echo "Disk space:" echo "df -h" df -h echo "" echo "If too little space is left on ${backup_drive}, you should decrease the" echo "max_used_space_percent value of the $0" echo "script and then run the script again. It will delete some old backups" echo "before creating the next backup." echo "************************************************************************" echo "" exit $rc