Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm hosting my own web server, and would like to regularly take a backup of my files. So I decided to write a bash script. However, I'm stuck with this:

# /etc/init.d/backup
# version 0.3.9 2012-08-13 (YYYY-MM-DD)

   NOW=`date "+%Y-%m-%d_%Hh%M"`
   tar -czvf  Backup${NOW}.tar.gz /var/www/
   mv Backup${NOW}.tar.gz /home/user/backup/

exit 0

I'm wondering how I will run it, how would I make it run every day at certain time.

Any help?

share|improve this question
You may need to try rsnapshot .. you don't have to reinvent the wheel buddy – BluesRhythm Jan 7 '13 at 13:06
Or rdiff-backup. – Halfgaar Jan 7 '13 at 13:32
up vote 1 down vote accepted

I would suggest using rsync,

Rync Backup script -This will make a backup folder for today ,then sycn yesterdays files todays then it will rsync todays to be backed up.


mkdir -p /storage/backups/`date +\%Y-\%m-\%d`-`date +\%A`/

rsync -avz /storage/backups/`date --date=yesterday +\%Y-\%m-\%d`-`date--date=yesterday    +\%A`/ /storage/backups/`date +\%Y-\%m-\%d`-`date +\%A`/

rsync -avz -e ssh  /var/www/ /storage/backups/`date +\%Y-\%m-\%d`-`date +\%A`/

# Definitions
sevendaysago=$(date --date='6 days ago' +%Y-%m-%d-%A)

# Delete backups from 7 days ago
rm -rf /storage/backups/$sevendaysago

you will need to run this to make scripts run-able

chmod u+x

In cron you will just need to add via crontab -e

@daily sh backupscript

Taken from my blog

share|improve this answer
When I did @daily sh backup, it simply gave me -bash: @daily: command not found – user145429 Jan 8 '13 at 6:18
in terminal enter crontab -e to edit the users crontab then enter @daily sh /location of above script/ – Paperghost Jan 8 '13 at 6:36
And then reboot? When will it run? – user145429 Jan 8 '13 at 6:43
It will then run at script once daily at mid night. It the same as putting 0 0 * * * in your crontab. You will not need to reboot. To test the script works go to the file location and run source "script name" this will execute the script and display any errors – Paperghost Jan 8 '13 at 7:43
Good! So it will delete any bacups older than 7 days? – user145429 Jan 8 '13 at 7:53

First, this script will not do anything. You just defined a function backup and did not call it. Just add a line like this below exit 0


Cron is the best tool to run specific tasks/scripts regularly.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.