phirebird

Backup a Linux box through FTP (push) and tar

Another quick Linux how-to. This time how to backup up an entire box using into a tar archive and push it to an FTP server. In my case, this ran every night and the FTP server was the central tape backup unit.

It’s a good idea to create yourself a separate directory to store the backup shell script and FTP script. For want of a better name, I created /back (10 out of 10 for originality, right?). In there, first create the backup script dobackup.sh:

rm /back/*.tgz
d=$(date +%y%m%d)
tar cvpzf /back/back$d.tgz --exclude=/proc --exclude=/lost+found --exclude
    =/back --exclude=/mnt --exclude=/sys /
lftp -f /back/backftpscript
Then change the mode of the file to allow execution:
[root@nas01 /]# chmod a+x dobackup.sh
[root@nas01 /]#
And then create the second file backftpscript (the FTP script):
open -u <ftp username>,<ftp password> <ftp host>
mput /back/*.tgz
bye
That’s it – you’re good to go! Just execute /dobackup.sh and it’ll tar everything and then upload it to the specified FTP server.

Here’s an explanation as to how it works and line-by-line what’s going on:

rm /back/*.tgz
Remove last nights backup! (or traces of any others)

d=$(date +%y%m%d)
Grab todays date (in the format yymmdd) and stuff it a variable ($d)

tar cvpzf /back/back$d.tgz –exclude=/proc –exclude=/lost+found –exclude=/back –exclude=/mnt –exclude=/sys /
Create an archive, make it Verbose, Preserve the permissions, filter the archive through gZip, call the File /back/back$d.tgz (e.g back101022.tgz for 22nd Oct 2010).  Exclude from the archive useless stuff like /back (the backup dir itself), /sys, /mnt, etc. And finally the target of the archive, /

lftp -f /back/backftpscript
lftp is like the ftp command, but with a lot more functionality and protocols. Probably warrants a how-to of its own, but needless to says it’s a lot more useful for use in situations like these. This particular line starts lftp and runs our ftp script, backftpscript. The contents of which are:

open -u <ftp username>,<ftp password> <ftp host>
Pretty self explanatory. Open a connection to <ftp host> and login as <ftp username> using the password <ftp password>

mput /back/*.tgz
After we’ve connected, upload (or put) all the .tgz files from our local /back directory

bye
Bid farewell and drop the connection (in a nice, polite sort of way)

Need this to run on a nightly basis? Take a look at my other how-to on cron jobs

Are you wondering how to learn more about Linux? Well, here’s a few books that I’ve found useful over the years that might just help you more:


Linux Command Line and Shell Scripting Bible
 
Linux: The Complete Reference, Sixth Edition
 
Linux Pocket Guide – Essential Commands
 
Linux All-in-one Desk Reference

 

 



Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

phirebird