6

I would like to backup a folder using cron on a centos. The folder c2duo_mms is located in /usr/local/src/djcode/c2duo_mms. I would like it backed ip every 1:00pm on Tuedays to my home folder /home/sh.

  • Are you backing up a folder or a file? Your question has contradictory bits. For a single file cp is fine, for a directory rsync is a better too. Please fix your question to get a precise answer. – Caleb Jul 19 '11 at 10:53
  • @Caleb c2duo_mms it is a folder -- fixed – Shehzad009 Jul 19 '11 at 10:57
12

A good thing to do would be to create a new compressed archive in your home.

Create this script named for exmaple */home/sh/c2duo_mms_backup.sh*:

#!/bin/bash

cd /usr/local/src/djcode/
tar zcf /home/sh/c2duo_mms-`date +%Y%m%d`.tar.gz c2duo_mms

Be sure to add the executable permission to the script:

chmod +x /home/sh/c2duo_mms_backup.sh

Then add the relevant crontab entry with the crontab -e command:

0 13 * * 2 /home/sh/c2duo_mms_backup.sh

The script will create a new compressed archive every Tuesday with the date in the filename, so that you can keep older backups if you want. File name will look like this:

c2duo_mms_20110719.tar.gz 
| improve this answer | |
  • This is what I have been looking for!!! thanks a lot. – Shehzad009 Jul 19 '11 at 12:41
  • a better solution would be to use rsync or unison. – symcbean Jul 19 '11 at 14:58
3
$ crontab -e
0 13 * * 2 cp -b /usr/local/src/djcode/c2duo_mms /home/sh/

The crontab -e command should pull up the crontab file for editing in your preferred editor (Set by the EDITOR or VISUAL environment variabels). The crontab line says to run the command on the 0th minute, 13th hour, 2nd day of the week, any day of the month any year. The command itself is a simple single file copy, except that I added the -b argument so that cp makes a backup file. This should leave you with TWO backups at all times, the current one and the previous one (with a .bk extension).

Edit: For a folder instead of a file, try rsync:

0 13 * * 2 rsync -av /usr/local/src/djcode/c2duo_mms/ /home/sh/c2duo_mms/
| improve this answer | |
  • 1
    That would be "perform this on the second day of every month at 1am", right? – Janne Pikkarainen Jul 19 '11 at 10:54
  • @caleb: For some reason it is not copying the folder to my home directory. Is there like some way that I can find what is causing this problem? – Shehzad009 Jul 19 '11 at 11:25
  • @Shehzad009: If you are using the rsync, you might make sure the target folder exists first before syncing it. Some will create it, but some won't. Also, to debug this, run th rsyncs e command manually yourself instead of from cron to see the output and make sure the backup part does what you want before sticking it in cron. – Caleb Jul 19 '11 at 11:27
  • @caleb: After using rsync e I get this "/home/sh/e" failed: No such file or directory (2). Not so sure what that e is. – Shehzad009 Jul 19 '11 at 11:33
  • The e is a bogus typo on your end somewhere, it doesn't appear in the command I suggested. The syntax is rsync -[options] [source] [target], and it's important to end with trailing slashes on the source and targets if you are syncing directories. – Caleb Jul 19 '11 at 11:36
2

Use command crontab -e and add this line to your crontab:

0 13 * * 2 cp -pra /usr/local/src/djcode/c2duo_mms /home/sh
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.