How to Backup Your Files Over SMB in Linux

1:47 PM

I just recently acquired a Zyxel media server, which I wanted to use to backup my files over the network. The only problem was that there are no Linux backup programs (that I know of) capable of backing up over SMB, which meant I would have to write a script that could accomplish this. In my research I found a lot of hints at what could be done to backup over SMB but no real step by step guides on how to do it. That's why, now that I have gotten it accomplished thanks to some help from the guys over at LinuxQuestions, I'm writing an article on how you can backup your files over SMB automatically in Linux. Hopefully this will help someone out there that may not be tech savvy but can follow some simple instructions. 


To start out, you will need to know the following things:
  • The IP address of the server you want to save your backups to
  • The directory on the server you want to save your backups to
  • The username/password of an account on the server that has full read/write access to the server

First of all we will need to mount the directory you want to save your backups to locally so the system can access it easily. Start out by making a directory in the /mnt directory (you will have to be root to do this):

mkdir /mnt/backup

Next you will want to mount the remote server's directory you want to save the backups to. This will be mounted inside the /mnt/backup directory we created. You will need to specify the IP address of the server, the directory you want to mount, the directory you want to copy over to the mounted directory, and of course you will also need to specify the username and password that you use to upload files to the server. 

First specify the username and password:

mount -o username=admin,password=password

Next specify the server IP address along with the path to the shared directory:

mount -o username=admin,password=password //192.168.1.9/music /

Now specify the directory you want to use to mount the server's directory. In this case we are using the directory we created, /mnt/backup:

mount -o username=admin,password=sedated1 //192.168.1.9/music /mnt/backup 

Now all you have to do is specify what directory you want to backup and what directory you want to send it to, which in this case is the /mnt/backup directory we created:

mount -o username=admin,password=password //192.168.1.9/music /mnt/backup && rsync -av /home/jonquil/Music /mnt/backup

When you are finished you should have a command string like what's shown above. Enter that and make sure the files will begin to be backed up.

Once you have ensured that command works, you'll want to create a script that uses it. Open up a text editor and put in the following:

#!/bin/sh

Then after that put in the command you want to use:

#!/bin/sh
mount -o username=admin,password=password //192.168.1.9/music /mnt/backup && rsync -av /home/jonquil/Music /mnt/backup

Save the script in your home folder as backup.sh. Since this script will contain your password, you don't want just anyone to be able to open and read it. So as root issue the following command from your home directory in terminal:

chmod 500 backup.sh

Now all we have to do is set up this script to run in crontab at a specified time. In terminal as root use the following command to edit crontab:

crontab -e

It will ask you what text editor you want to use. I recommend nano as it is the easiest. 

It is impossible for me to go over all the options you have for running scripts in crontab. So I will set you up with an example line and link to an article that explains crontab in depth:

0 1 * * 3 root run-parts /home/jonquil/backup.sh

This line tells crontab to run the script as root at 1 a.m. every Friday. If you would like to customize the time the script runs, look here for more information on how to schedule tasks in crontab.

****UPDATE**** I found out that Deja Dup carries the ability to backup files over SMB. However, it backs up files in its own compressed format, so this is still the only way I know of automatically backing up files over SMB uncompressed. 

You Might Also Like

4 comments

  1. You could also use automounter to have the share mounted automatically.

    Here's a pretty nice tutorial for that:
    [one-liner]: Mounting a SMB/CIFS Share as an Automount on CentOS/Fedora/RHEL

    Package names are not very different on e.g. Ubuntu autofs5. The basic instructions are valid still.

    Of course you would need to make your backup script check whether the mount is present so that if the device is a laptop it doesn't try to do the backup when the mount is not available keeping in mind that you need to attempt to access it before you can be certain of that.

    ReplyDelete
  2. Isnt there an opensource project called backuppc?

    ReplyDelete
  3. You could use backuppc http://backuppc.sourceforge.net/ which can do backups locally, over ssh and over SMB. It will also deduplicate and compress the data.

    ReplyDelete
  4. Thank you for mentioning Backuppc, because I had forgotten about it. I did try this tool, however the interface is very complicated and it seems to be more of a system administrator's tool than a common user's tool. I wanted something very simple to do, and unfortunately it was simpler to go about the backup process this way than it was to use Backuppc.

    Thanks for the tip about automount. It's not really necessary to keep the drive always mounted, but I'm sure someone else will be able to put that valuable info to use. :)

    ReplyDelete