Backup Linux to a Windows Box

Been having a look on…

Login

Blog History

Been having a look on the web for options that allow backing up a Linux box to a Windows box without first having to create a massive file on the Linux box.

Found a good FTP option at http://www.squidoo.com/linux_backup namely under point 4:

# login
ftp TheFTPServersIPAddressHere

# set the transfer mode to binary
binary

# tar straight to the remote server
# the tar options are
# -c create an archive
# -v be verbose about it
# -l restrict creation to local disk
# -O pipe the archive to standard out
# -o make an old style archive 
put |"sudo tar -cvlOo /usr" myusr.tar

# the quotes around the "sudo tar -cvlOo /usr" are critical 
# as is there being no space between 
# the "|" pipe and the begining of the `sudo tar'

Which with sufficient automation would be one good backup solution.

But I would prefer to use windows networking because then I don't have to a. Install a copy of the IIS FTP Server or FileZilla Server and b. send an unencrypted FTP password.

At that point I struck trouble... Google failed to dish up a good example of what I was trying to achieve.

So I have come up with something that seems to work OK as follows.

How to Backup Linux to Windows using Tar and Smbclient
On your Linux Box - You know! The one you want to backup
Create a file in your home directory named .smbclient with the following contents.

# Enter your windows network credentials here
username=yourwindowsusername
password=yourwindowspassword
domain=YOURDOMAIN

Make sure you change the permissions of the .smbclient file to stop others (other than root) perusing your windows password

chmod 400 .smbclient

Create a file in your home directory named excludes.txt or whatever you want to call it with the patterns of what you want to avoid (check out the tar documentation on how these patterns work i'm a n00b when it comes to tar so you can probably do something more sophisticated).

# put anything in here that you don't want to backup
home/user/excludes.txt
home/user/.smbclient 
home/user/backup.log
/dev
/proc

Create a group and edit the /etc/sudoers file to allow you to perform a tar operation with superuser permissions without password authentication. Note: You may have to log out and back in to pick up the changes.

sudo groupadd smbclient
# add your self to the group
sudo usermod -a -G smbclient yourlinuxusername
# add the group to your sudoers file to allow it 
# to perform the backup as superuser
sudo visudo
# add this line to the sudoers file
%smbclient ALL=NOPASSWD:/bin/tar

# in english the above line means...
# anyone in the smbclient group on any host...
# can use sudo to run /bin/tar with root privileges

Now for the work to get done using tar and smbclient. Create a file named backup.sh and make it excuteable

touch ./backup.sh
chmod +x ./backup.sh

Add the following as contents of backup.sh edit it to taste:

#!/bin/bash
cd # everything is relative to the user user's home dir so go there

# you can backup as much `/' or as little `/path/to/one/dir/or/file' as you like
TOP_DIR=/home/username

# nice way to timestamp your backups with a YYMMDD stamp
MYDATE=`date +%y%m%d`
TARGET_DIR=$MYDATE
MYFILE=`hostname -s`-$MYDATE.tar
WINHOST=yourwindowsbox
SHARE=C$
LOG=backup.log
EXCLUDES=excludes.txt
AUTH=.smbclient

# now the work get's done
# the tar args -c create an archive, -v verbosely, -O pipe it to STDOUT
# -p preserve the permissions,
# -X get the list of what not to back up from excludes.txt
# using `2>' pipe STDERR (the verbose output of the -v switch) to backup.log 
sudo /bin/tar -cvOp $TOP_DIR  -X $EXCLUDES 2>$LOG  \
| smbclient //$WINHOST/$SHARE -A $AUTH -c "mkdir $TARGET_DIR; \
cd $TARGET_DIR;put - $MYFILE;exit"

Finally you can add this to your user crontab using

crontab -e

# Plonk this content into the crontab
# m h  dom mon dow   command
# at 1/4 past 2 every morning run the backup
15 2 * * * /home/user/backup.sh

Caveat's and Gotcha's:

  • Some files on a Linux installation have filenames incompatible with Windows for example the ":" character is acceptible in Linux but a no-no in Windows. So trying to untar an archive on Windows can be an excercise in futilty.
  • Until you have taken a complete backup and successfully restored it then assume your backup strategy isn't complete yet. A complete successful restore is really the only 100% reliable way of making sure you have captured everything you need to restore successfully. Remember what they say "In theory, theory is the same a practical. In practise it isn't"

The above seems to work on a Ubuntu 9.04 VM that I am running and copying up to a Windows XP Pro SP3 share. I managed to backup the entire / sans /dev and /proc to a 4GB tar file on the Windows box successfully.

1 Comment

  1. Dario the boring

    How do you restore files with this approach?
    thank you.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.