[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.

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

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.

touch timestamp

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

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.

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!

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:

touch -t 200913081200 copy.log

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

#!/bin/sh

cd /home

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

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

Regards

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!

I can't test it out but try to use a variable for the date:

CurrDate=$(date)

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

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:

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

so find should looks for files older the reference file.

Regards

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

#!/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 !

Note that the command copies only files newer then the reference file.
To get the desired format of the date you can use this command:

CurrDate=$(date "+%d-%m-%Y %H-%M")

Have a read of the manpage of date.

Regards

I've often used

cp -Ru $source $destination

The -u flag only copies files if source is newer (ie 'update'), and you may already be familiar that -R means recursive (scans through subdirectories too).

I often include -v flag (verbos) too so you get a list of all the files being copied. thus, if you wanted the process logged, then you can redirect the output to a log file:

cp -Ruv $source $destination > /var/logs/userbackups.log

I must emphasise that I've not used this in a production environment though - only for controlled "offline" back ups. (I use ZFS snapshots on the production server). So there may be a number of disadvantages to cp -u compared with the suggestions earlier in this thread.

Thanks for the replies, I am indeed familiar with cp and its options, like recursive. Having the verbose options print all the copied files would be nice in my case.
Like you said it is maybe better to not use cp; but cpio instead.
The only problem I experience now is that it doesn't copy recursive. (but only the single file in /home ; not the folders) Is there a solution to this?

Thanks in advance

Are you shure you have newer files then the reference file?

Regards

After checking the man pages, the only advantage of CPIO that I can see is compression.
If compression isn't an issue, then perhaps you're over engineering a solution (I'm a firm believer of KISS - automate everything, but don't build a sports car when all you need is scooter).
If compression is an issue (which I suspect it is), then perhaps it might be better looking into tar (which i believe can also do an update as well as recursive)

I don't even need compression, or maybe files can be compressed but the backup'ed folders have to look like regular folders. I hope you can follow... :slight_smile: But I'll leave it like that with cpio
@Franklin, idd, you were right the folders were older. Your script works perfect after all! Thx.
I just guessed it would first copy all the empty folders too (to have a basic structure), but in the end that's not really necessary.