You are not logged in.

#1 2010-09-16 17:05:22

jlacroix
Member
Registered: 2009-08-16
Posts: 576

[SOLVED] Script to Copy Folder Automatically?

I know I should probably know how to do this but for some reason I can't think of the command. What I basically want to do is take one folder, and make a copy of it on a set schedule each week.

Basically I want to take the contents of ~/writings and copy it to /mnt/Backup/writings/date each week, but the "date" folder has to be the date that it's backing up the files. For example, if the script ran today I would want it to take the contents of ~/writings and copy the contents of it to /mnt/Backup/2010-09-16. The next time it runs would be a different date. Does this make sense?

I do this manually right now but sometimes I forget to back up this folder and today I lost some of my writing (although a trivial amount) and I could have avoided it should I have had a backup.

Further, it would be pretty cool if it would automatically delete folders that have a date older than six months, but I don't know how to do that either. I feel that I should know this and would probably shoot myself once I see the command but I'm not coming up with anything...

Last edited by jlacroix (2010-09-19 21:53:21)

Offline

#2 2010-09-16 17:20:01

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Script to Copy Folder Automatically?

How about a cron job?
'man date' will tell you what does e.g.

$(date +"%Y%m%d-%H%M%S").tar

mean.
'man find' will tell you how to find files 'older than ...'.

Last edited by karol (2010-09-16 17:22:48)

Offline

#3 2010-09-16 17:23:19

milomouse
Member
Registered: 2009-03-24
Posts: 940
Website

Re: [SOLVED] Script to Copy Folder Automatically?

i'd also suggest a cron job. maybe to run an {r}sync script that checks if -d /mnt/Backup/(date '+%Y-%m-%d') exists and if not, create it then sync. not sure about the old directory rotation. haven't tested this: find $1 -type d -mtime +$2 -exec rm -r {} \;

edit: ah, edit! too slow, then. :{

Last edited by milomouse (2010-09-16 17:23:50)

Offline

#4 2010-09-16 17:26:37

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Script to Copy Folder Automatically?

May backup scheme rsyncs my documents to ~/backup/docs (so i don't copy the files that didn't change since the last time) and then I create tar archives with the current date / time. Compress them and e-mail them to my gmail account.

Offline

#5 2010-09-16 17:27:01

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,774

Re: [SOLVED] Script to Copy Folder Automatically?

set up a cron job.  The command is cp  --preserve -r /the/full/path/to/writings /mnt/Backup/Writings/$(date +%y.%m.%d)
This assumes you want to retain ownership and permission information.
The full path is required because it runs as root.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#6 2010-09-16 17:28:19

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Script to Copy Folder Automatically?

ewaller wrote:

set up a cron job.  The command is cp  --preserve -r /the/full/path/to/writings /mnt/Backup/Writings/$(date +%y.%m.%d)
This assumes you want to retain ownership and permission information.
The full path is required because it runs as root.

Will this create the folder or exit because there's none?

Offline

#7 2010-09-16 17:33:23

milomouse
Member
Registered: 2009-03-24
Posts: 940
Website

Re: [SOLVED] Script to Copy Folder Automatically?

it wont create them. i'd almost suggest using the "install" command. something like:

install --compare -D /full/path/writings/* /mnt/Backup/$(date '+%Y-%m-%d') 

or something similar.
edit: accidentally added leading '-'
edit2: actually i don't think that will work either. :{

Last edited by milomouse (2010-09-16 17:34:50)

Offline

#8 2010-09-16 17:35:20

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,774

Re: [SOLVED] Script to Copy Folder Automatically?

It should create the new date directory.  It depends on the existence of  /mnt/Backuo/Writings. 
In a full blown script, I'd check that the volume is mounted and that Writings exists.  If not, an email could be sent to the interested parties notifying them that the backup did not occur.    If it is okay to fail, this is less important.  The error messages would be logged by cron.

I could be wrong.  I have not tried this :-)

Last edited by ewaller (2010-09-16 17:36:27)


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#9 2010-09-16 17:36:06

jlacroix
Member
Registered: 2009-08-16
Posts: 576

Re: [SOLVED] Script to Copy Folder Automatically?

Thanks guys, I'm going to play with this when I get home. smile

Offline

#10 2010-09-16 17:38:35

jlacroix
Member
Registered: 2009-08-16
Posts: 576

Re: [SOLVED] Script to Copy Folder Automatically?

ewaller wrote:

It should create the new date directory.  It depends on the existence of  /mnt/Backuo/Writings. 
In a full blown script, I'd check that the volume is mounted and that Writings exists.  If not, an email could be sent to the interested parties notifying them that the backup did not occur.    If it is okay to fail, this is less important.  The error messages would be logged by cron.

I could be wrong.  I have not tried this :-)

Actually, emailing me when that fails would probably be the next step. Right now I'm just focusing on getting it to work, and then I could set up an email job, but I've always had problems doing that as the computer is not on a FQDN.

Offline

#11 2010-09-16 17:40:16

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Script to Copy Folder Automatically?

http://wiki.archlinux.org/index.php/Backup <- full-blown backup apps
http://pbrisbin.com/pages/scripts.html? … bin/backup <- a nice script you can "borrow"

Last edited by karol (2010-09-16 17:41:24)

Offline

#12 2010-09-16 17:42:25

milomouse
Member
Registered: 2009-03-24
Posts: 940
Website

Re: [SOLVED] Script to Copy Folder Automatically?

p.brisbin has a script for everything. :} here's what i use, although pretty personalized and derived from simple examples. http://github.com/milomouse/code/blob/master/shell/zinc

edit: just posting as another example. follow karol's links preferably.

Last edited by milomouse (2010-09-16 17:43:38)

Offline

#13 2010-09-16 17:43:47

jlacroix
Member
Registered: 2009-08-16
Posts: 576

Re: [SOLVED] Script to Copy Folder Automatically?

karol wrote:

The thing is I already have a backup solution in place (Unison) but it synchronizes two folders, and the Writings folder is the one folder that I want to have a weekly history of. Those scripts may still be useful though.

Last edited by jlacroix (2010-09-16 17:44:06)

Offline

#14 2010-09-19 21:53:49

jlacroix
Member
Registered: 2009-08-16
Posts: 576

Re: [SOLVED] Script to Copy Folder Automatically?

Thanks everyone, the solution from ewaller worked for me. smile

Offline

Board footer

Powered by FluxBB