[bash] Simple backup (cp) script but incremental


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash] Simple backup (cp) script but incremental
# 1  
Old 08-12-2009
MySQL [bash] Simple backup (cp) script but incremental

Hi all,

I would need a rather simple bash backup script that loops throught the (local) users and for each users backs up (cp!) its /home/username folder.
About the functionalities:

The script has to run every 2 hours (that's cron, so don't mind about that) and the files should be copied to the folder /oldversions/username/date-hh-mm (which have to be created every time)
Also it has to be incremental, only the modified files have to be copied to the folder. Anyways, compression (.tar, etc) is not needed!
The backups should be kept 7 days (168h) -> less important for now

As the permissions of the files&folders have to be kept, I found these lines of code that should copy preserving all this.
Code:
cd /old_dir
find . -depth -print | cpio -pdlmv /new_dir

I'm just finding my way through VBscript but still struggling with this bash scripting jobs. So thanks a lot, in advance, for helping me out!

Regards
# 2  
Old 08-12-2009
At the beginning of the backup process, use the "touch" command to create a file called, say, "timestamp." This file will have a modification time corresponding to when the "touch" command was executed.

Code:
touch timestamp

then, use the -newer switch in the find command of the backup:

Code:
find . -newer timestamp ...

This will find only the files and directories newer than the file "timestamp."

If you're sure no files will be created after commencing the backup, you could execute the "touch" command after the "find" command instead of before it.
# 3  
Old 08-13-2009
Thx, Miller ! That's a good idea for starters. But could you maybe be a little more specific ?
When the script.sh is executed the first time it has to copy everything but at the same time it has to check for this timestamp and if the files are newer only copy those, this for all the next times (from the 2nd you run it)
And preserving the folder's permissions.
Would it be possible demonstrating with a code example of such a script ?

Thanks in advance!
# 4  
Old 08-13-2009
This script creates directories like /oldversions/`date`/user1, /oldversions/`date`/user2, and so on.
If that's not acceptable you have to do much more work......

Create a reference file with the touch command in the /home directory and set the date and time of the file with the last copy date.
If the last copy is from 13 aug 2009 12:00 you can do something like:

Code:
touch -t 200913081200 copy.log

Now your script should looks like (not tested!):

Code:
#!/bin/sh

cd /home

find * -newer /home/copy.log -print | cpio -pvdmu /oldversions/`date`

echo "Last copy on: `date`" >> copy.log

Regards
# 5  
Old 08-13-2009
Thanks a lot Franklin! Only; i get "cpio: Too many arguments" ?
Also, can it be reused, I guess it doesn't compare with a timestamp in the beginning ? Thx!

Last edited by laurens; 08-13-2009 at 08:23 AM..
# 6  
Old 08-13-2009
Quote:
Originally Posted by laurens
Thanks a lot Franklin! Only; i get "cpio: Too many arguments" ?
I can't test it out but try to use a variable for the date:

Code:
CurrDate=$(date)

find * -newer /home/copy.log -print | cpio -pvdmu /oldversions/"$CurrDate"

Quote:
Originally Posted by laurens
Also, can it be reused, I guess it doesn't compare with a timestamp in the beginning ? Thx!
As I mentioned earlier you have to create a file the first time with the touch command with the last copy date and .... yes, the modifation time of the reference file is updated after the copy action with:

Code:
echo "Last copy on: `date`" >> copy.log

so find should looks for files older the reference file.

Regards
# 7  
Old 08-19-2009
Hi!
Thanks for your last reply. I'm sorry to just come back to it now but I was busy with something else lately.

When I execute the script now
Code:
#!/bin/sh

cd /home

CurrDate=$(date)

find * -newer /home/copy.log -print | cpio -pvdmu /oldversions/"$CurrDate"

echo "Last copy on: `date`" >> copy.log

only one file is copied to the newly made folder. This is the -only- file is the home folder, the rest are (user)folders.
There should be something like userX, userY and userZ
(knowing that userX, userY and userZ have each a home dir with a few files)

Also, possibly, userX should not have access to the files of userY etc
But I guess thats just a matter of copying the file and folder permissions.

Oh, something else, for the date; is it possible to write the date like dd-mm-yyyy hh-mm ? now I get a very long and unhandy Date as foldername.

Thanks in advance !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Create a simple bash backup script of a file

This is the problem: Write a script that will make a backup of a file giving it a ‘.bak’ extension & verify that it works. I have tried a number of different scripts that haven't worked and I haven't seen anything really concise and to the point via google. For brevity's sake this is one of the... (4 Replies)
Discussion started by: demet8
4 Replies

2. Homework & Coursework Questions

incremental or full backup ???

Hi. Can someone tell me if the following script that i have made is a script for INCREMENTAL BACKUP or FULL BACKUP. My teacher told me that is doing an FULL BACKUP. • find /etc /var /home -newer /backups/.backup_reference > /backups/.files_to_archive • touch /backups/.backup_reference • tar... (1 Reply)
Discussion started by: bender-alex
1 Replies

3. UNIX for Dummies Questions & Answers

incremental and full backup.. please help me

Hi, i'm new here(and a newbie) and i need some help with a project. I need to write a script for an incremental backup (this must be executed every day at 24:00) and a full backup (executed once a month) for etc/var/home directories. Can someone please help me with this? And a small explanation of... (9 Replies)
Discussion started by: bender-alex
9 Replies

4. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

5. Windows & DOS: Issues & Discussions

Incremental Backup

I have a folder /root/test in a centos 5.3 system. I want to take an incremental backup of the contents of the folder in the C:\Downloads folder of a windows system present in the same lan as the linux system. What are the ways of executing this plan? Kindly help (0 Replies)
Discussion started by: proactiveaditya
0 Replies

6. Shell Programming and Scripting

Incremental backup

Hi, I would like to create a daily incremental backup of a directory with all of the files within and add a timestamp (year-month-day) to the tar.gz file. I have the following, but it doesn't backup the inside files of the directory. #!/bin/bash tar -czf... (1 Reply)
Discussion started by: agasamapetilon
1 Replies

7. UNIX for Dummies Questions & Answers

Best unix incremental backup utility?

Hello everyone. Could you please advise of what would be the best Unix (Debian 4) program for regular (daily or weekly) incremental backups? I'm not sure whether the backups will be stored on a "backup" drive on the same system or on an external "backup" system, but we would like to have a... (2 Replies)
Discussion started by: nottrobin
2 Replies

8. UNIX for Dummies Questions & Answers

incremental backup

Hi All.. i am trying to write a script which will give the incremental tar backup of all files with latest timestam. i tried with find -mmin -2 but if it takes half on hour or something to creat the tar itself, then no meaning in using the above command. so please help me to find the... (2 Replies)
Discussion started by: Usha Shastri
2 Replies

9. Shell Programming and Scripting

tar - incremental backup

Hello everyone! I'm trying to make incremental tar archives of a folder for an example. On the box I use is UNIX AIX installed. I tried some sample codes I found on several web pages but with no success. Don't know what I'm doing wrong. Please write some sample code to make incremental tar... (0 Replies)
Discussion started by: Funky_ass
0 Replies

10. Shell Programming and Scripting

How to reinvent incremental backup in bash?

I want to backup two important files everytime they are modified. How would I write a bash script that would check the dates of my zip files and my data files and only create a new zip file if the zip file is older than the two data files? Thanks, Siegfried (1 Reply)
Discussion started by: siegfried
1 Replies
Login or Register to Ask a Question