Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

Anyone has any bash script that does a daily/weekly/yearly backup script that I can use with cron.

Basically it will put the backups into 3 folders. Daily backup would only have this week's backup files. Weekly backup would only have this month's weekly backup file and month backup folder would yearly backup file.

Thanks!

share|improve this question

closed as off-topic by MadHatter, mdpc, HBruijn Feb 13 at 23:23

  • This question does not appear to be about server, networking, or related infrastructure administration within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Take a look at my script: serverfault.com/questions/575163/… – Florin Andrei Feb 12 '14 at 21:12
2  
I'm voting to close this question as off-topic because we are not a script-writing service. – MadHatter Feb 12 at 12:54

Look at these tools:

You might find that these tools satisfy the requirements and eliminate your need for scripts. I use scripts also but they are for particular situations. If you want examples of what other people have done, two people have posted their backup scripts here.

Backups have been covered exhaustively on ServerFault. If you are trying to develop a backup strategy, I suggest searching the site. If you are unable to find a particular question answered, you might ask that in a separate question.

share|improve this answer

No - but its very trivial to do. OTOH its far from trivial to provide an answer to your question based on the information you have suplied.

Certainly, it'd be a lot simpler to control the backup type (daily/weekly/yearly) and destination from the crontab rather than the bash script (although that might simply be a matter of sending a different parameter to the has script).

Backups are not intrinsically important - being able to restore your data is very important - so for your backup to have any value then it must be in a format which allows you to restore it. We can't tell you:

1) Which backup format is appropriate for your data
2) what backup tools you have on your system which are scriptable
3) what restore tools you are likely to have access to if you suffer a complete system failure
4) whether you need to support multi-volume / media changes in your backup

e.g. tar and compress are almost universally available - but its not very robust. CPIO is more robust but doesn't incorporate compression support.

share|improve this answer
    
The OP uses the word "folders" which implies that the backups would be on-line (on the same media or system) which makes the backups almost completely (but not quite totally) useless. OK, in the worst case scenario - completely useless. By the way, you forgot to mention rsync (and dd has its uses, too). +1 for the emphasis on restoring. It should be pointed out that the restores must be tested. – Dennis Williamson Aug 12 '10 at 14:10

Warning: Hacked together in 5 minutes and untested (I don't have Bash installed).

#!/bin/bash
#Usage: $0 [Yearly,Monthly,Weekly]

set PROTECTEDFILES = "/etc /usr/home /usr/local/etc"
set BACKUPDIR = "/usr/backups"
set BACKUPTAR = backup-`hostname -s`-`date "+%F"`.tgz

tar czf $BACKUPDIR/$1/$BAKCUPTAR $PROTECTEDFILES

In crontab:

0   0   1   1   *   root   /root/sbin/my_backup Yearly
0   0   1   *   *   root   /root/sbin/my_backup Monthly
0   0   *   *   6   root   /root/sbin/my_backup Weekly
share|improve this answer

You can use Duplicity for that purpose. Compared to the popular rsnapshot and rdiff-backup it is still actively developed.

The only pitfall is that you can't create daily, weekly and montly backups but it's possible to create a simpler combination like "daily, weekly" or "weekly, monthly" or "daily, monthly" and so on.

For this I recommend this wrapper script: https://github.com/zertrin/duplicity-backup which simplifies the configuration significantly.

share|improve this answer

I have written the script that rotates all of the backups within one folder, keeping X daily, Y weekly and Z monthly backups.

I take all of the backups to folders

/root/backup/YYYYMMDD 

on daily basis.

Then, i have the following rotating script:

#!/bin/bash

echo > /root/keep.txt
#writing dates of the backups that should be kept to the array

for i in {0..7}; do ((keep[$(date +%Y%m%d -d "-$i day")]++)); done
for i in {0..4}; do ((keep[$(date +%Y%m%d -d "sunday-$((i+1)) week")]++)); done
for i in {0..12}; do
        DW=$(($(date +%-W)-$(date -d $(date -d "$(date +%Y-%m-15) -$i month" +%Y-%m-01) +%-W)))
        for (( AY=$(date -d "$(date +%Y-%m-15) -$i month" +%Y); AY < $(date +%Y); AY++ )); do
        ((DW+=$(date -d $AY-12-31 +%W)))
        done
        ((keep[$(date +%Y%m%d -d "sunday-$DW weeks")]++))
done
for i in {0..5}; do
        DW=$(date +%-W)
        for (( AY=$(($(date +%Y)-i)); AY < $(date +%Y); AY++ )); do
        ((DW+=$(date -d $AY-12-31 +%W)))
        done
        ((keep[$(date +%Y%m%d -d "sunday-$DW weeks")]++))
        done

#writing the array to file keep.txt line by line
for i in ${!keep[@]}; do echo $i >> /root/keep.txt; done

#delete all files that not mentioned in keep.txt
cd /root/backup
ls -1 /root/backup/ | sort /root/keep.txt /root/keep.txt - | uniq -u | xargs rm -rf
rm /root/keep.txt

This will keep 7 daily, 4 weekly, 12 monthly and 5 yearly backups.

I have got the most part of the logic from this topic

share|improve this answer

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