Sunday 15 September 2013

auto backup local file in folder using crontab and rsync

it is really fast working at local hard disk. but the disadvantage is that it is not backuped. so a better way would be working at local hard disk that are automatically backed up as schedule.
two problems here.
1. how to make a schedule
2. how to sync with two folders.

slolution to problem 1:

using command

$ crontab

two useful parameters:

$ crontab -e

after executing this, a text editor pops out, which is the place one lodge a schedule, the format is like this (one can use # to comment out unnecessary schedules )


Code:
* * * * * /path/to/the/bash/script

the first star means minutes, it can be 0 to 59 or *. if it is star (*), it means every minute
the second star means hour, it can be 0 to 23 or *. if it is star (*), it means every hour
the third star means day of the month, it can be 0 to 31 or start*. if it is star (*), it means every day
the fourth star means month of the year, it can be 1 to 12. if it is star (*), it means every month
the fifth star means day of the week. if it is star (*), it means every day.

my final script after executing crontab -e is:
0 18 * * * /home/chenming/Hdrive/backup6.4

which means run backup6.4 at 18:00 of each day.

one needs to make sure that backup6.4 is a executable file (chmod +x backup6.4)

some other useful commands:

$ crontab -l

list schedules 

I usually use the following one to test:

schedule:
* * * * * /home/chenming/Hdrive/backup6.4

commands:
#!/bin/bash
echo $(date)>>/home/chenming/test

which writes date and time in one file per minute.




solution to problem 2:

use command rsync. put the following line in /home/chenming/Hdrive/backup6.4

rsync -a --progress --human-readable --states --update /path/to/folder/needs/to/be/synced --delete /path/to/folder/backup/file/stores/
here --progress shows the results (not useful as a schedule, though)
       --stats vobose mode
       --update skip files that are newer on the receiver (this is not useful if one doesn't change anything in receiver)
       --delete delete  extraneous files from dest dirs

Notes for systemd user:

you need to make sure that the service dcron is enabled:
 systemctl status dcron
systemctl is-enabled dcron
To enable it, you can do:
systemctl enable dcron

1 comment:

  1. Good to find a code that can enable auto backup of local files.

    Thanks
    Silvester Norman

    Change MAC Address

    ReplyDelete