Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Shell script to backup files/dir - Any other useful scripts?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
mc_barron
Apprentice
Apprentice


Joined: 28 Aug 2003
Posts: 230
Location: Chicago, IL

PostPosted: Wed Oct 29, 2003 12:47 am    Post subject: Shell script to backup files/dir - Any other useful scripts? Reply with quote

Hey All,
Just finished writing my first shell script; thought it would be nice to share just in case others might find it useful. (remember to set the file to be executable: chmod a+x filename)

I run it daily (it's in my /etc/cron.daily/ folder). It backs up folders and files I specifiy on a daily basis. It also removes old copies if the are older than 10 days and there is more then 2 backups.

Without furthur ado, here it is:
Code:

#! /bin/sh
# Script to backup files or directories.
# I put this in my /etc/cron.daily folder so every day
# the folders that I specify are backed up.

# Define the backup and source folder/file names
sourceDir='/home/mcbarron'
backupDir='/home/mcbarron/backup'
fileList=$backupDir'/files.txt'

todays_date=`date +%F`
if ! [ -x $backupDir/$todays_date ]
then
   mkdir $backupDir/$todays_date
   chown $backupDir/$todays_date --reference=$backupDir
fi

# Backup the specified files (they must all be in the source directory)
for FILE in `cat $fileList`
do
   cp -au $sourceDir/$FILE $backupDir/$todays_date/$FILE
done

# Loop through any other backups - removing folders that are more than 10 days old.
# 86400 seconds in a day, 864000 seconds every ten days.
seconds=`date +%s`

for FILE in `ls $backupDir`
do
   # If the backup count is currently less than 3, do not remove anything.
   if [ `ls -1F $backupDir | egrep '[0-9]+\-[0-9]+\-[0-9]+' | wc -l | sed 's/ //g'` -gt 3 ]
   then
      oldSeconds=`date -d$FILE +%s`
      if [ `expr $oldSeconds + 864000` -lt $seconds ]
      then
         rm -rf "$backupDir/$FILE"
      fi
   fi
done


Enjoy,
Mike
Back to top
View user's profile Send private message
razamatan
Apprentice
Apprentice


Joined: 28 Feb 2003
Posts: 160

PostPosted: Wed Oct 29, 2003 10:51 pm    Post subject: Reply with quote

try swinging my script around... does pretty much the same thing, but goes further to compress things into a tarball, and the lists tell you what's in where...

https://forums.gentoo.org/viewtopic.php?p=401456
_________________
a razamatan doth speaketh,
"Never attribute to malice, that which can be adequately explained by stupidity"
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum