Is it possible to set up a back up routine to a NAS drive?

Connection sharing, Firewall, Samba..etc
Forum rules
Before you post please read how to get help
Post Reply
penciltester
Level 1
Level 1
Posts: 9
Joined: Wed Jul 10, 2019 10:32 am

Is it possible to set up a back up routine to a NAS drive?

Post by penciltester » Sun Jul 14, 2019 3:53 pm

Hi folks. What I would like to do is the following.

Write a script that would:
1) connect to a network
2) mount the NAS drive
3) backup a folder
4) run shutdown command

essential creating a script that I would run to backup on a NAS drive that is on a wireless network for in house sharing and when complete, execute Linux shut down command. I'm a newb to Linux. So is this possible? How difficult? Any resources I could be pointed to to help me understand and write this script?

The shutdown command is easy and im assuming since rsync is run in terminal anyway, its probably not that hard to find examples of the commands to run for backup. I'm at a lose on what i would need to do to have the system change networks and mount a drive once on the network though.

Thanks for any help in advance.

User avatar
AndyMH
Level 8
Level 8
Posts: 2386
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Is it possible to set up a back up routine to a NAS drive?

Post by AndyMH » Sun Jul 14, 2019 5:23 pm

Something to get you started. Does some of what you want. Did have a version that backed up to my synology NAS, but lost that. Also had a version that shutdown on completion, also lost. This was before I 'discovered' timeshift and backintime. It was the first script I wrote when I started with mint, very much a learning process.

Code: Select all

#!/bin/bash

#---------------------------------------------------------------
# RSYNC BACKUP OF DATA
# 23 August 2017
#---------------------------------------------------------------

# Selectively backups root, data and virtual machines
# Root is root plus all hidden files/folders in /home
# Data is all files/folders in /home less hidden files and VMs
# VMs is the contents of /home/andy/VirtualBox VMs

#------------
# DESTINATION
#------------
# Where are we backing up to
destination=/media/andy/SDbackup # the express card slot although might be 25% slower than USB3

# Check to see that the destination is available, if not quit.
if [ ! -d "$destination" ]; then #note ! is the NOT operator
	echo
	echo "Backup destination" $destination "not found."
	echo -n "Press any key to continue..."
	read -n 1
	echo
	exit
fi

#----------
# LOG FILES
#----------
logfile=$destination"/backup.log"
timelog=$destination"/time.log"
# backup.log will contain details of backups
# time.log will contain dates for when backups were done

#-----------
# START TIME
#-----------
StartTime=`date +%H`
StartTime=`expr $StartTime \* 60`
Mins=`date +%M`
StartTime=`expr $StartTime + $Mins`

BackupDate=$(date +"%a %d %b %Y %H:%M")

#---------------------
# WHICH TYPE OF BACKUP
#---------------------
clear
echo RSYNC BACKUP
echo 1. Data
echo 2. System inc. hidden files
echo 3. Virtual machines
echo -n "Backup type? "
read -n 1 BackupType
echo

#--------
# SOURCES
#--------

# Data
source1=/home/andy

# System
source2=/

#Virtual machines
source3="/home/andy/VirtualBox VMs"

#-------------
# DESTINATIONS
#-------------

# note - destination folders must exist before running rsync down to the penultimate level
# e.g. /root/home/andy must exist before VM backups are done. rsync won't create higher level folders

dest1=/root/home
dest2=/root
dest3=/root/home/andy

#----------------
# EXCLUSION FILES
#----------------
# Data excludes - not needed, embedded in rsync command line

# System excludes
SystemExcludes=/home/andy/.scripts/systemexcludes

# VM excludes - not needed, embedded in rsync command line

case "$BackupType" in

	1)	echo Data backup selected
		#databackup with excludes file is working
		echo "-----------------------------------------------------------------" >> $logfile
		echo "DATA BACKUP - " $BackupDate >> $logfile
		echo "-----------------------------------------------------------------" >> $logfile
		echo "Comment:"
		read Comment
		destination+=$dest1
		rsync -aAXh --stats --delete --no-inc-recursive --info=progress2 --log-file=$logfile --exclude={".*",".*/","VirtualBox VMs/"} "$source1" "$destination"
		# get finish time and close
		EndTime=`date +%H`
		EndTime=`expr $EndTime \* 60`
		Mins=`date +%M`
		EndTime=`expr $EndTime + $Mins`
		echo "Data             " $BackupDate Time: `expr $EndTime - $StartTime` "mins" >> $timelog
		echo "                 " $Comment >> $timelog
		;;

	2)	echo System backup selected
		echo "-----------------------------------------------------------------" >> $logfile
		echo "SYSTEM BACKUP - " $BackupDate >> $logfile
		echo "-----------------------------------------------------------------" >> $logfile
		echo "Comment:"
		read Comment
		destination+=$dest2
		sudo rsync -aAXh --stats --delete --no-inc-recursive --info=progress2 --log-file=$logfile --exclude-from=$SystemExcludes "$source2" "$destination"
		# get finish time and close
		EndTime=`date +%H`
		EndTime=`expr $EndTime \* 60`
		Mins=`date +%M`
		EndTime=`expr $EndTime + $Mins`
		echo "System           " $BackupDate Time: `expr $EndTime - $StartTime` "mins" >> $timelog
		echo "                 " $Comment >> $timelog
		;;

	3)	echo VM backup selected
		echo "-----------------------------------------------------------------" >> $logfile
		echo "VIRTUAL MACHINES BACKUP - " $BackupDate >> $logfile
		echo "-----------------------------------------------------------------" >> $logfile
		echo "Comment:"
		read Comment
		destination+=$dest3
		#note - need quotes round source3 because the variable has spaces in it
		rsync -aAXh --stats --delete --no-inc-recursive --info=progress2 --log-file=$logfile "$source3" "$destination"
		# get finish time and close
		EndTime=`date +%H`
		EndTime=`expr $EndTime \* 60`
		Mins=`date +%M`
		EndTime=`expr $EndTime + $Mins`
		echo "Virtual machines " $BackupDate Time: `expr $EndTime - $StartTime` "mins" >> $timelog
		echo "                 " $Comment >> $timelog
		;;

esac


# Rsync options:

# -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X). It is a quick way of saying you
#			      want recursion and want to preserve almost everything (with -H being  a notable omission)

# -A, --acls                  preserve ACLs (implies -p)

# -X, --xattrs                preserve extended attributes

# -v, --verbose               increase verbosity

# -b, --backup                With  this  option, preexisting destination files are renamed as
#		              each file is transferred or deleted.  You can control where  the
#			      backup  file  goes  and what (if any) suffix gets appended using
#		              the --backup-dir and --suffix options. Default is it add ~ to the 
#			      file extension. WE DO NOT WANT THIS OPTION AS IT IS GOING TO DOUBLE 
#			      UP THE AMOUNT OF SPACE REQUIRED.

# -h, --human-readable        output numbers in a human-readable format

# --delete		      Not sure about this, deletes extraneous files from the receiving side, but
#			      u must have asked rsync to send the whole directory (e.g. "dir" or "dir/") without
#			      using a wildcard for the directory’s contents (e.g. "dir/*")

# --stats		      Outputs: no of files, no of created files, no of deleted files, no files transferred
#			      total file size, total transferred size, literal data, matched data, file list size, 
#			      file list generation time, file list transfer time, total bytes sent and received

# --no-inc-recursive	      Disables recursion so that rsync accurately reports total no files to assess and progress. Otherwise
#			      rsync only reads a few files and then starts transfer and progress reported is wrong

# --info=progress2	      range of options for info, --info=help gives range. progress2 = total transfer progress


#-----------------
# BACKGROUND STUFF
#-----------------
# Previous rsync command line:

#	rsync -abh --stats --no-inc-recursive --delete --info=progress2 --log-file=$logfile --exclude-from=$excludes $source $destination

# rsync will work even while the system is running, but files changed during the transfer may or may not be transferred, which can cause
# undefined behavior of some programs using the transferred files.
# This approach works well for migrating an existing installation to a new hard drive or SSD.
# Run the following command as root to make sure that rsync can access all system files and preserve the ownership:

# rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /path/to/backup/folder


# By using the -aAX set of options, the files are transferred in archive mode which ensures that symbolic links, devices, permissions,
# ownerships, modification times, ACLs, and extended attributes are preserved, assuming that the target file system supports the feature.
# The --exclude option causes files that match the given patterns to be excluded. The contents of /dev, /proc, /sys, /tmp, and /run are excluded
# in the above command, because they are populated at boot, although the folders themselves are not created. /lost+found is filesystem-specific.
# The command above depends on brace expansion available in both the bash and zsh shells. When using a different shell, --exclude patterns should
# be repeated manually. Quoting the exclude patterns will avoid expansion by the shell, which is necessary, for example, when backing up over SSH.
# Ending the excluded paths with * ensures that the directories themselves are created if they do not already exist.
# Note:
# If you plan on backing up your system somewhere other than /mnt or /media, do not forget to add it to the list of exclude patterns to avoid an
# infinite loop.
# If there are any bind mounts in the system, they should be excluded as well so that the bind mounted contents is copied only once.
# If you use a swap file, make sure to exclude it as well.
# Consider if you want to backup the /home/ folder. If it contains your data it might be considerably larger than the system. Otherwise consider
# excluding unimportant subdirectories such as  /home/*/.thumbnails/*,  /home/*/.cache/mozilla/*, /home/*/.cache/chromium/*, and /home/*/.local
# /share/Trash/*, depending on software installed on the system. If GVFS is installed, /home/*/.gvfs must be excluded to prevent rsync errors.
# You may want to include additional rsync options, such as the following. See rsync(1) for the full list.
# If you use many hard links, consider adding the -H option, which is turned off by default due to its memory expense; however, it should be no
# problem on most modern machines. Many hard links reside under the /usr/ directory.
# You may want to add rsync's --delete option if you are running this multiple times to the same backup folder. In this case make sure that the
# source path does not end with /*, or this option will only have effect on the files inside the subdirectories of the source directory, but it
# will have no effect on the files residing directly inside the source directory.
# If you use any sparse files, such as virtual disks, Docker images and similar, you should add the -S option.
# The --numeric-ids option will disable mapping of user and group names; instead, numeric group and user IDs will be transfered. This is useful
# when backing up over SSH or when using a live system to backup different system disk.
# Choosing --info=progress2 option instead of -v will show the overall progress info and transfer speed instead of the list of files being
# transferred.

Homebrew i5-8400+GTX1080 Cinnamon 19.0, 3 x Thinkpad T430 Cinnamon 19.0, i7-3632 , i5-3320, i5-3210, Thinkpad T60 19.0 Mate

User avatar
phd21
Level 19
Level 19
Posts: 9160
Joined: Thu Jan 09, 2014 9:42 pm
Location: Florida

Re: Is it possible to set up a back up routine to a NAS drive?

Post by phd21 » Sun Jul 14, 2019 7:40 pm

Hi penciltester,

I just read your post and the good replies to it. Here are my thoughts on this as well.

Yes, it is possible to back up to any mounted drive, including networked drives, NAS, etc... using various options if you have the NAS always mounted. What is the make and model of NAS storage you have?

Mounting NAS Storage in Linux
https://cloud.ibm.com/docs/infrastructu ... ntNASLinux

Hope this helps ...
Phd21: Mint KDE 18.3 & 19, 64-bit Awesome OS, Ancient Dell OptiPlex 780 Core2Duo E8400 3GHz,4gb Ram,256gb SDD, Video: Intel 4 Graphics, DVD Lightscribe. Why I use KDE?:https://opensource.com/life/15/4/9-reasons-to-use-kde

penciltester
Level 1
Level 1
Posts: 9
Joined: Wed Jul 10, 2019 10:32 am

Re: Is it possible to set up a back up routine to a NAS drive?

Post by penciltester » Tue Jul 16, 2019 7:48 am

Hopefully I didn't use the incorrect term in the subject.

@phd21 My drive is a 3TB Seagate drive connected to a Netgear wireless modem/router using samba shares. I thought the term NAS included any type of network attached storage, now im reading that might not be so. Sorry for the confusion. Thanks for the link, I will give it a read.

@AndyMH Thanks for the script. Now I just have to do some studying to understand it, never written a script in my life. :)


I think one of the "rubs" is that i'm not always connected to this network. The internet network and the share network are not connected to each other.

Thanks again!

Post Reply

Return to “Other networking topics”