Shell Script for taking backup

Friday, December 28, 2012 , 0 Comments

This below script is found to be very useful for me.It does four things specifically.
  • Creates a folder by date. 
  • Compresses the files for backup
  • Sticks them in your backup directory
  • Clears out backups older than 3 days.
#!/bin/bash

cd /home/backups
mkdir $(date +%Y-%m-%d)
cd /opt/
tar -pczf /home/backups/$(date +%Y-%m-%d)/opt.tar.gz code
cd /var/
tar -pczf /home/backups/$(date +%Y-%m-%d)/var.tar.gz work
cd /home/backups/
threedaysago=`date -d "3 days ago" +%Y%m%d`
for backup in [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
 do
 backupdate=`echo "$backup" | tr -d -` # remove dashes
  if test "$backupdate" -lt "$threedaysago"
  then
   rm -rf "$backup"
  fi
 done
exit

0 comments: