Backup files from Linux to a Windows server

Ok, this may be my last disaster recovery and backup blog for a long time. As you can probably tell from the title this blog entry is all about keeping backup strategies as cheap as possible.

My strategy is to backup all of my Windows and Linux servers to one central Windows server that is running a Tivoli backup agent. All of my servers are hosted elsewhere, and since it costs $99.00 per server to backup I am getting the most for my money by only backing a single server to tape/SAN. However that single server carries all of the files that need to be remotely backed up to tape/SAN.

My earlier posts show how to backup the Windows servers:
Windows backup bat script using xcopy

Also, how to backup the Windows Domain Controller:
Backup Windows Domain Controller using NTBACKUP via cmd

And I also showed how to backup a Linux server to a local file:
Linux backup using CRON to local directory

Now I will show how I moved the files backed up on the Linux servers to the Windows server prior to tape/SAN backup. I have decided to use Samba and mount a directory pointing to a shared folder on the Windows server. Lets begin:

  1. Create a share on the Windows server to use as your backup location.
  2. Create a user to use this share, and give the user permissions to use it.
  3. Pick, or create, a user that you’re going to use to backup the files. Log in as this user and type ‘id’ to get the numeric ID of the user in the Linux OS. (You will need it later.)
  4. Become ‘root’. (This will enable you to perform the next tasks without problems.)
  5. Install samba and samba-client if they are not installed yet.
    yum install samba
    yum install samba-client

    You will need to answer ‘y’ to when prompted.

  6. By default only the root user can mount or unmount a cifs, so we must open it to other users.
    chmod 4755 /sbin/mount.cifs
    chmod 4755 /sbin/umount.cifs
  7. Now we need to make a directory to use as our mount point.
    mkdir /mnt/sharename
  8. To enable our desired user to mount and unmount this we need to assign ownership to that user. Changing permissions with chmod is not enough.
    chown userfrom#1 /mnt/sharename
  9. Now we need to edit the /etc/fstab file and add an entry. Replace the user ID of 500 with your user ID from #1. Replace sharename with your Windows share name. Replace WINDOWSHOST with your hostname or IP address. If you don’t know the shares run smbclient -L WINDOWSHOST
    //WINDOWSHOST/sharename /mnt/sharename cifs credentials=/root/smblogin,uid=500,noauto,user 0 0
  10. Create/Edit a file /root/smblogin and put the following two lines in it.
    username=WINDOWS USERNAME
    password=WINDOWS PASSWORD
  11. Now we must give our user from #1 access to the file, but still protect it.
    chown userfrom#1 /root/smblogin
    chmod 600 /root/smblogin
  12. If you have IPTABLES active on your server you will need to make sure to add a policy to allow OUTPUT port 445.
  13. Log in as the user from #1
  14. Try to mount the share:
    mount /mnt/sharename
    ls -la /mnt/sharename
    umount /mnt/sharename
  15. If that was successful you are now ready to write a script that will automate the moving of your backup files from the Linux to the Windows share. (Script/File = backup.sh)
    #!/bin/sh
    mount /mnt/sharename
     
    cp -r /path/to/file.tar.gz /mnt/sharename/path/to/file.tar.gz
     
    umount /mnt/sharename

    Of course you can add this to the end of the file described in Linux backup using CRON to local directory which would create a complete solution for backing up files, then relocating them to a remote Windows server.

  16. Next you will add an entry to the crontab of the user from #1 to happen automagically.
    crontab -e -u userfrom#1
  17. Enter this into the crontab.
    # Backup at 1:00 A.M. every day. Call 'man 5 crontab' for more information
    * 1 * * * /path/to/backup.sh

Now once this is all done you can rest a little bit easier knowing that your servers are backed up. I hear the beach calling me now….ttyl.

Published by

Adam

Application Architect from Florida

2 thoughts on “Backup files from Linux to a Windows server”

  1. Is there a way to copy a specific number of files in your backup? I have a directory which will have several thousands files created per day and I would like to move 500 at a time to a sub directory, zip them up, and then FTP them to another server as back up. Thanks for you help.

Leave a Reply

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