Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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:

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

   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
2  
You may need to try rsnapshot rsnapshot.org/howto .. 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

2 Answers 2

up vote 2 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.

#!/bin/sh

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 script.sh

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 –  Christian Jan 8 '13 at 6:18
    
in terminal enter crontab -e to edit the users crontab then enter @daily sh /location of above script/backupscript.sh –  Paperghost Jan 8 '13 at 6:36
    
And then reboot? When will it run? –  Christian 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? –  Christian 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

backup

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

share|improve this answer

Your Answer

 
discard

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

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