How to back up a Linux system over SMB the easy way

Written by Robert -

At work we are changing backup environments and I needed a simple way to create full backups of a Linux server to a Windows share. You can create the fileshare on another Linux machine and use the rest of the guide as is.

The first thing I've done is created a share on the Windows server where there is enough space to place the backups. I've secured it with a username and password. There are two ways to do this. The first is to create a backup locally and then send it over, the second is to directly send it over. I've gone for the second option in this situation. You can also use this script with a share on a Linux server. My advise is to create one share per server and a specific username and password for each server.

First create a folder where you want to mount the Windows server to.

You can change the name to anything you like. I used the name server for this example.

mkdir /mnt/server

Mount the Windows share to folder created above:

mount -t cifs //ServerName/LinuxBackup -o username=windowsusername,password=windowspassword /mnt/server

When I look at the df output I can see that the server is mounted:

Filesystem               Size  Used Avail Use% Mounted 
onrootfs              20G  3.8G   15G  20% 
//dev/sda2                 20G  3.8G   15G  20% 
//dev/sda3                 53G  592M   51G   2% 
/home//ServerName/LinuxBackup  137G   27G  110G  20% /mnt/server

Now I know that the fileshare works from Linux, let's unmount it:

umount /mnt/server

Now it's time to add this line to the /etc/fstab file to make sure it automatically mounts it at boot:

//ServerName/LinuxBackup /mnt/server cifs username=windowsusername,password=windowspassword,iocharset=utf8,sec=ntlm 0 0 

When done, save the file and use the line below to mount the added drive to your Linux machine:

mount -a 

Creating the first backup:

I like to create the first backup manually to make sure everything works as it should. Make sure you run this command as root or use the sudo command (super user do). Otherwise, critical parts of the system will not be backed up.

First make sure it’s mounted:

mount -t cifs //ServerName/LinuxBackup -o username=windowsusername,password=windowspassword /mnt/server 1>/root/mount1.txt 2>/root/mount2.txt

Let’s create a folder with the date of today:

cd /mnt/server  
mkdir $(date '+%d-%b-%Y')

Then we start the first backup:

tar cvpjf /mnt/server/$(date '+%d-%b-%Y')/backup.tar.bz2 --exclude=/proc --exclude=/dev --exclude=/lost+found --exclude=/mnt --exclude=/sys --exclude=/root/ /  1>/mnt/server/$(date '+%d-%b-%Y')/backup.txt 2>/mnt/server/$(date '+%d-%b-%Y')/backuperror.txt

This creates a tar archive with the following options:

c = create
v = verbose, not required but might be helpful if you want to see what it backed up.
p = absolute names. This makes sure all path names are correct.
j = creates a bzip2 archive to compress the data as much as possible. This can save a lot of storage
f = output to file

The following folders are excluded from the backup:

/proc = Virtual file system, this is populated at boot. No need to create a backup of this.
/dev = Device files. This folder contains links to your hardware. No need to create a backup of this.
/lost+found = Trash, you can make a backup of this if you really want to. I've excluded this.
/mnt = mounted filesystems. In my situation there is no need for this. If you have mounted filesystems that you wish to backup, remove this but make sure you do exclude the Windows share you added earlier.
/sys = Virtual file system, this is populated at boot. No need to create a backup of this.
/root = we will place the backup results here. If you want you can put these in another folder and exclude that specific folder.

Output 1 shows the files that are backed up. You can store this information at the location of the backup, locally or destroy the information by sending it to /dev/null Output 2 shows the errors.

I added this part to the file names:

$(date '+%d-%b-%Y')

This creates a file with the output of the echo's above in the name. If you use echo with this command you will see that it outputs the date:

 # echo $(date '+%d-%b-%Y')  

This makes sure the backup writes to a new folder each time it runs with the current date. This prevents the backup to overwrite the old backup every day.

After this we can unmount the share:

umount /mnt/server

Automatically create the backup.

Here is the fun part. I want to have a nightly backup of my system.

Everything I’ve written above resulted in the following batch script:

umount /mnt/server
mount -t cifs //ServerName/LinuxBackup -o username=windowsusername,password=windowspassword /mnt/server 1>/root/backupresult/mount1.txt 2>/root/backupresult/mount2.txt
cd /mnt/server
mkdir $(date '+%d-%b-%Y')
tar cvpjf /mnt/server/$(date '+%d-%b-%Y')/backup.tar.bz2 --exclude=/proc --exclude=/dev --exclude=/lost+found --exclude=/mnt --exclude=/sys --exclude=/root/backupresult/ /  1>/mnt/server/$(date '+%d-%b-%Y')/backup.txt 2>/mnt/server/$(date '+%d-%b-%Y')/backuperror.txt
umount /mnt/server

I placed this in a batch file called backup.tar and placed it in the /root directory. After that I made it executable by typing the following command:

chmod +x /root/backup.tar

Now you can open the crontab with the edit option:

sudo crontab -e

Because I want to start the backup at midnight I used the following command:

@midnight /root/backup.tar 


If you want to restore the backup, mount the file share, browse to it and execute the following command:

tar xvpfj backup.tar.bz2 -C /

Then update grub according to your distro and your done :)