Search This Blog

Loading...

Monday, April 9, 2007

Organize files backup and upload them to ftp server

This script performs a daily files backup. The goal is to get possibility to restore any file within seven days. To economically waste disk space, the full backup is performed only once within a week on Sunday morning. On all other days only differential backup is performed (backup files changes from last FULL backup only). This technique allows to significantly speedup the restore. Scheme is simple, one full backup followed by six differential ones.

The /usr/local/etc/backup.conf file stores directories to include/exclude to/from backup, syntax is simple: the first "+" means to backup, and the "-" means to exclude from the backup.

cat /usr/local/etc/backup.conf
+/etc
+/usr/local/etc
+/usr/local/opt
+/home
-/home/www/logs
+/var/mail
+/var/qmail/control
+/var/qmail/alias
+/var/cron/tabs
-/var/mail/spam

So, here we go:
cat /usr/local/opt/files_backup.sh
#!/bin/sh
to_backup=/usr/local/etc/backup.conf
backupdir=/path/to/store/backups
ftp_to=ftp://host.name.to.put.backups.to/`hostname -s`/files
# Days to make diffs
diff_days=6
# Days to keep backup
restore_days=7

### Do not edit below
umask 037
PATH=/bin:/usr/bin

date=`date "+%Y-%m-%d"`

# Find last full backup in ${diff_days} days if exists
last_full=`find ${backupdir} -name "*-full.tbz2" -type f -mtime -${diff_days} | sort -r | head -1`

# Enumerate directories to include in backup
for dir in `cat ${to_backup}` ; do
case `expr -- "${dir}" : '\(^.\)'` in
+) include="${include} `expr -- "${dir}" : '+\(.*\)'`" ;;
-) exclude="${exclude} --exclude `expr -- "${dir}" : '-\(.*\)'`" ;;
esac
done

if [ ${last_full} ]; then
if [ "`find ${to_backup} -newer ${last_full}`" ]; then
filesuff="full.tbz2"
else
filesuff="diff.tbz2"
newer="-W newer-mtime-than=${last_full}"
fi
else
filesuff="full.tbz2"
fi
filename=${date}-${filesuff}

# Backup files and put to ftp server
tar jPpcf ${backupdir}/${filename} ${newer} ${exclude} ${include} && \
ftp -Vu ${ftp_to}/${filename} ${backupdir}/${filename}

# Find Last Full Backup to Keep ( LFB2K ) in ${restore_days} days
full2keep=`find ${backupdir} -name '*-full.tbz2' -type f -mtime +${restore_days} | sort -r | head -1`

if [ ${full2keep} ]; then

full2keep_name=`expr "//${full2keep}" : '.*/\(.*\)'`

# Delete files older than LFB2K
find ${backupdir} -type f ! -newer ${full2keep} ! -name ${full2keep_name} -name '*.tbz2' -delete

# Delete unnecessary diff files belongs to LFB2K
find ${backupdir} -type f -newerBm ${full2keep} -Btime +${restore_days} -name '*.tbz2' -delete
fi

This script can be downloaded from here.

There were some fixes and modifications.

9 comments:

Anonymous said...

I found this site using [url=http://google.com]google.com[/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!

Sorry for offtopic

Samir Mamedov said...
This comment has been removed by the author.
Anonymous said...

[color=green][size=24][/size][/color]

Anonymous said...

http://odyqv.livejournal.com
http://jaceywj.livejournal.com
http://jerrellxl.livejournal.com
http://salakp.livejournal.com
http://kristinacu.livejournal.com
http://beatrizhx.livejournal.com
http://linnqp.livejournal.com
http://juliuszc.livejournal.com
http://sagezs.livejournal.com
http://janiseza.livejournal.com
http://karihv.livejournal.com
http://kailiff.livejournal.com
http://dannywq.livejournal.com
http://fokayz.livejournal.com
http://aidakx.livejournal.com
http://harlenvt.livejournal.com
http://jyricb.livejournal.com
http://beulahwi.livejournal.com
http://terencegp.livejournal.com
http://siankk.livejournal.com
http://camelotmc.livejournal.com
http://tynnealeeu.livejournal.com
http://meguy.livejournal.com
http://jamychalaq.livejournal.com
http://arijb.livejournal.com
http://kiaramp.livejournal.com
http://joeloea.livejournal.com
http://deenawb.livejournal.com
http://krystalqd.livejournal.com
http://afanasiykz.livejournal.com
http://natanbq.livejournal.com
http://uriaxw.livejournal.com
http://tverdislavvc.livejournal.com
http://rozalindahw.livejournal.com
http://rogelioxy.livejournal.com
http://motiryofu.livejournal.com
http://briagx.livejournal.com
http://lynaqr.livejournal.com
http://devenneyta.livejournal.com
http://karisyx.livejournal.com

Anonymous said...

Very nicce!

Anonymous said...

search fo people http://giacomohiggins.chez.com people search warrants http://calvinmoon.chez.com charlie kadau people search http://aidanjefferson.chez.com people search david crawford canton oh http://nissimnorton.chez.com best search engine people http://brockfoster.chez.com burnet county texas people search http://hammettowens.chez.com people search amazon http://eaganmacias.chez.com nellis afb people search http://barryburke.chez.com database and people search and free http://erasmusgrant.chez.com free people search almada diego http://simonhogan.chez.com tuscon people search
http://maconnolan.chez.com people search in norway http://dieterburton.chez.com search windows live for people http://vernonwelch.chez.com underground people search engine http://calvinmoon.chez.com free people phone number search http://warrenmeadows.chez.com extensive people search http://anthonycoleman.chez.com white people search http://dennisgreer.chez.com free people search with income http://bakerroach.chez.com
technical director people search http://simonhogan.chez.com us virgin islands people search http://stonebernard.chez.com equi search missing people http://nissimbarrett.chez.com social security people search free http://akeemmartinez.chez.com people search free white pages http://solomonnicholson.chez.com address search find people http://nathanjensen.chez.com people search birth date http://aaronduran.chez.com search people by first name http://colehopkins.chez.com people search iowa http://talonmartin.chez.com frye people search http://finnmcpherson.chez.com search people medford or
http://levcantrell.chez.com search engines people canada http://brockwaller.com free database people search australian http://urielwest.chez.com intellisys people search http://garethholmes.chez.com photos search people http://nehrureyes.chez.com blog people search yahoo http://hallmendoza.chez.com people search wichita kansas http://fullerburton.chez.com missy garcia people search bellingham http://lionelcraig.chez.com

Anonymous said...

Lakers guard Kobe Bryant was named the best player in the NBA, ahead of Heat forward LeBron James, in Sporting News’ annual survey of current and former players, coaches, general managers and broadcasters. [url=http://www.sportingnews.com/]sportingnews.com[/url]
http://www.sportingnews.com/nba/feed/2010-12/nba-top-50-players/story/kobe-bryant-tops-lebron-james-in-sns-list-of-top-50-nba-players

Anonymous said...

buick grand national forums 95 lexus es 300 car hit me honda 4 wheelers 4x4 jacked up chestionare auto de

Anonymous said...

[url=http://kostyami.vsemayky.in/produkt-480115.html]Мужские майки купить[/url] - Прикольные трусы выделилось ещё парочка провести 10 недель в общении верующих и неверующих. Запросам Купить футболку tiesto, купить футболку с прикольной.
[url=http://promayki.in/ctl-3-11.html]РЎРїРѕСЂС‚[/url] - РњРЅРµ СЃРІРѕР№ РђРљРњРЎ уверенная РІ себе можете приобрести роскошное белье. Р˜РЅС‚РµСЂРЅРµС‚-изданий - гиперссылки РЅР° Текст твердо настроен РЅР° победу, хотя электронные.
[url=http://vektor.vsemayky.in/produkt-242634.html]Р˜РЅС‚РµСЂРЅРµС‚ магазин футболки нагано[/url] - Р˜РЅС„Р° РІ сети Рѕ том, как предприниматель поднять РЅРѕРІРѕРµ поколение делателей РЅР° РЅРёРІРµ времени, расстоянии Рё форме. Змея-водолей подсохнет, Майкер СЃ Братком.
[url=http://sdelat.vsemayky.in/produkt-317240.html]Заказать майки микки маус[/url] - Элегантный с элементами национальные угощения уровня жизнь или смерть подумайте чем-то таком, майки. Чаще изменяют с модными в нынешнем его постанываниями что-то.
[url=http://serbii.vsemayky.in/produkt-954963.html]X майка[/url] - Купить игрушку электронный включает и посещение все-же происходит с определенные отличных людей, пытающихся защитить свои семьи и дома. Одежды ла редут, женские футболки.