0

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!

Patrick
  • 455
  • 1
  • 7
  • 10
  • Take a look at my script: http://serverfault.com/questions/575163/how-to-keep-daily-backups-for-a-week-weekly-for-a-month-monthly-for-a-year-a – 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 '16 at 12:54
  • This is the first hit on Google for this type of backup retention which is my primary requirement. However, I'm looking for something with a GUI, unless I'm forced to use a script so I created a post in SE-software-reqs here: https://softwarerecs.stackexchange.com/questions/84066/linux-ubuntu-backup-with-incremental-and-smart-retention – alchemy Sep 22 '22 at 02:19

5 Answers5

2

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.

Warner
  • 23,756
  • 2
  • 59
  • 69
1

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.

symcbean
  • 21,009
  • 1
  • 31
  • 52
  • 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
1

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
Chris S
  • 77,945
  • 11
  • 124
  • 216
0

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.

Stefan Profanter
  • 365
  • 1
  • 5
  • 17
  • I like Duplicity, except theres no way to do simple file copy backup.. it stores files inside a chunked/encrypted file system. – alchemy Sep 22 '22 at 02:18
0

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

riverfall
  • 101
  • 3