Use RSync to backup to another directly attached storage
From SynologyWiki
Contents
|
First script
Here is a script, originally written by forum user ian, that can use the Rsync functions of the Synology product to backup one volume to another DAS, which maybe another internal HDD, or external USB Drive.
# Simple script to backup data from BACKUP_SOURCE to BACKUP_TO_DEVice # # this can be any drive attached to your synology box ... # ... internal drive(s), external USB drive(s), external SATA drives(s) # # To keep the script short and simple, it doesn't check that the source # and/or target directories exists -- If they don't then it fails! # It also makes no check that you have sufficient space on the drive. # Backup source device/share/directory/etc BACKUP_SOURCE=/volume1/public/test/ # Backup destination device/directory -- DO NOT include the trailing slash BACKUP_TO_DEV=/volumeUSB1/usbshare/backup # For a seven day cyclical backup, remove the comment from the line 'DAY=options' and # set the preferred directory name format 'Monday, etc or Mon, etc' # A = long day name (i.e. Monday, Tuesday, ... Sunday) # a = short day name (i.e. Mon, Tue, ... Sun) # This will create a daily backup directory (i.e. backup/Sat backup/Sun) # DAY=/`date +%a` # NOTE # If you are backing up files created on a Mac ... # ... then remove the exclude option # (the @eaDir directories hold the Mac file resources information) # rsync -q -ax --delete $BACKUP_SOURCE $BACKUP_TO_DEV$DAY/ rsync -q -ax --delete --exclude="@eaDir" $BACKUP_SOURCE $BACKUP_TO_DEV$DAY/ # end |
Second Script
Here is another script, originally written by forum user rabraham, that can use the Rsync functions of the Synology product to backup any volume on the Synology box to an external USB or SATA Drive. Use -h to get the help option
#!/bin/sh
##########################################################################
# RSYNC BACKUP SCRIPT
#
# -h = help
# -s = source of rsync backup (music, photo, web, ....) - No Spaces
# -e = Ext Drive destination of backup (USB1, USB2, SATA)
# -d = Destination Directory on External Drive from top level *share/(.....) - No Spaces
# -p = Backup Partition Type is EXT3
# -l = log file
# -q = quiet
# -v = verbose
# -n = No Execute Rsync just check validity of other switches
##########################################################################
#***************************************************************
# Default Variables
#**************************************************************
VERSION="0.1 beta"
#***************************************************************
# Functions
#***************************************************************
print_header()
{
echo ""
echo ""
echo "##############################################################################"
echo "# rsync_backup.sh - version $VERSION "
echo "##############################################################################"
echo ""
}
#***************************************************************
# Get Options from the command line
#***************************************************************
while getopts "s:d:e:l:phqvn" options
do
case $options in
s ) opt_s=$OPTARG;;
d ) opt_d=$OPTARG;;
e ) opt_e=$OPTARG;;
l ) opt_l=$OPTARG;;
p ) EXT3_MODE="-go";;
h ) opt_h=1;;
n ) opt_n=1;;
v ) RSYNC_MODE="-v";;
q ) RSYNC_MODE="-q";;
\? ) opt_h=1;;
esac
done
#***************************************************************
# Print Help
#***************************************************************
if [ $opt_h ]; then
print_header
echo "Usage: "
echo " "
echo " rsync_backup.sh -s <dir> -e <ext_drive> -d <dir> [-l <log>] [-q -h -v -n]"
echo " "
echo " -h = Help "
echo " "
echo " * List this help menu "
echo " "
echo " -s = Source of rsync backup "
echo " "
echo " * Directory in the /volume1/ that should be backed up "
echo " * i.e. (music, photo, web, ....) "
echo " * No Spaces, no trailing / "
echo " "
echo " -e = External drive destination for the backup "
echo " "
echo " * i.e. (USB1, USB2, SATA) "
echo " "
echo " -d = Destination directory on Exernal Drive for the backup "
echo " "
echo " * i.e. the path from top level [usb/sata]share directory "
echo " * No Spaces, no trailing / "
echo " "
echo " -p = Backup Partition Type is EXT3 "
echo " "
echo " * Uses the -g and the -o options for the rsync "
echo " to maintains group and owner on files. Do not use on FAT32 "
echo " "
echo " -l = Log file for output "
echo " "
echo " * If specified any output of the rsync will go to the log "
echo " file passed in "
echo " "
echo " -q = Quiet mode "
echo " "
echo " * Run rsync command with -q switch to supress all output "
echo " except errors "
echo " "
echo " -v = Verbose mode "
echo " "
echo " * Run rsync command with the -v switch to use verbose ouput "
echo " "
echo " -n = No Execute Mode "
echo " "
echo " * Dont Run rsync command just check validity of other switches "
echo " "
echo "----------------------------------------------------------------------------"
echo " "
echo " Examples: "
echo " "
echo " - Backup music to USB1 drive in the directory foo using verbose mode. "
echo " "
echo " % rsync_backup.sh -s music -e USB1 -d foo -v "
echo " "
echo " - Backup photo to SATA drive in the top directory to log backup.log "
echo " "
echo " % rsync_backup.sh -s photo -e SATA -d . -l backup.log "
echo " "
echo " - Backup web to USB2 drive in the 2008/jan directory using quiet mode "
echo " "
echo " % rsync_backup.sh -s web -e USB2 -d 2008/jan -q "
echo " "
echo "----------------------------------------------------------------------------"
exit 1
fi
#***************************************************************
# Get the source of the rsync from the command line -s
#
# - This should be the directory you wish to back up
# in the /volume1/ directory on the DS.
#
# i.e music, photo, web, ........
#***************************************************************
#--------------------------------------
# Check to see if -s was passed in
#--------------------------------------
if [ $opt_s ]; then
PASSED_SOURCE_DIR=$opt_s
else
print_header
echo "No source directory for the backup specified."
echo ""
echo " Use -s <src_dir> on the command line "
exit 1
fi
BACKUP_SOURCE_PREFIX="/volume1/"
BACKUP_SOURCE_DIR=$BACKUP_SOURCE_PREFIX$PASSED_SOURCE_DIR
#---------------------------------------------
# Check to see if specified source dir exists
#---------------------------------------------
if [ -e $BACKUP_SOURCE_DIR ]; then
echo ""
else
print_header
echo "The specified source directory for the backup: $BACKUP_SOURCE_DIR "
echo "does not exist. "
echo ""
echo "Please check the -s option and pass in a valid directory "
exit 1
fi
#******************************************************************
# Get the ext dest drive of the rsync from the command line -e
#
# This should be USB1 or USB2 or SATA
#
# Mounted by the DS as
# - /volumeUSB1/usbshare
# - /volumeUSB2/usbshare
# - /volumeSATA/satashare
#
#******************************************************************
#--------------------------------------
# Check to see if -e was passed in
#--------------------------------------
if [ $opt_e ]; then
PASSED_EXT_DRIVE=$opt_e
else
print_header
echo "No External Drive for the backup specified."
echo ""
echo " Use -e [USB1,USB2,SATA]"
exit 1
fi
#--------------------------------------------
# validate the destination Drive passed in
#--------------------------------------------
if [ $PASSED_EXT_DRIVE = "USB1" ] || [ $PASSED_EXT_DRIVE = "USB2" ]; then
BACKUP_DEST_DRIVE="/volume$PASSED_EXT_DRIVE/usbshare/"
elif [ $PASSED_EXT_DRIVE = "SATA" ]; then
BACKUP_DEST_DRIVE="/volume$PASSED_EXT_DRIVE/satashare/"
else
print_header
echo "Invalid External Backup Destination specified: $PASSED_EXT_DRIVE."
echo ""
echo " Use -d [USB1,USB2,SATA]"
exit 1
fi
#--------------------------------------------------
# Check to see if specified dest drive is mounted
#--------------------------------------------------
if [ -e $BACKUP_DEST_DRIVE ]; then
echo ""
else
print_header
echo "The specified external drive is not mounted: $BACKUP_DEST_DRIVE "
echo ""
echo "Please check the -d option and pass in a mounted external drive "
exit 1
fi
#***************************************************************
# Get the destination of the rsync from the command line -d
#
# - This should be the directory you wish to back up to go to
#
#***************************************************************
#--------------------------------------
# Check to see if -d was passed in
#--------------------------------------
if [ $opt_d ]; then
PASSED_DEST_DIR=$opt_d
else
print_header
echo "No destination directory for the backup specified."
echo ""
echo " Use -d <dest_dir> on the command line "
exit 1
fi
BACKUP_DEST_DIR=$BACKUP_DEST_DRIVE$PASSED_DEST_DIR
#---------------------------------------------
# Check to see if specified source dir exists
#---------------------------------------------
if [ -e $BACKUP_DEST_DIR ]; then
echo ""
else
print_header
echo "The specified destination directory for the backup: $BACKUP_DEST_DIR "
echo "does not exist. "
echo ""
echo "Please check the -d option and pass in a valid directory "
exit 1
fi
#-------------------------------------------------------
# Log Files for Backups
#
# If log file is specified redired standard out and
# standard error to the log file
#
# Will append log and not over write it
#
# > log 2>&1
#-------------------------------------------------------
#--------------------------------------
# Check to see if -l was passed in
#--------------------------------------
if [ $opt_l ]; then
PASSED_LOG_FILE=$opt_l
LOG_FILE=$BACKUP_DEST_DRIVE$PASSED_LOG_FILE
else
LOG_FILE="/dev/null"
fi
#---------------------------------------------
# Check to see if specified log dir exists
#---------------------------------------------
BASENAME=`dirname "$LOG_FILE"`
if [ -e $BASENAME ]; then
echo ""
else
print_header
echo "The specified log directory for the backup: $BASENAME "
echo "does not exist. "
echo ""
echo "Please check the -l option and pass in a valid directory "
exit 1
fi
#***************************************************************
# Execute the Rsync
#***************************************************************
echo $0 $* | tee -a $LOG_FILE
echo " " | tee -a $LOG_FILE
echo " " | tee -a $LOG_FILE
echo "######################################################################" | tee -a $LOG_FILE
echo "# rsync_backup.sh - version $VERSION " | tee -a $LOG_FILE
echo "######################################################################" | tee -a $LOG_FILE
echo "# " | tee -a $LOG_FILE
echo "# Starting the requested rsync backup " | tee -a $LOG_FILE
echo "# " | tee -a $LOG_FILE
echo "# -Command : rsync -rlptD --delete --exclude="@eaDir" $RSYNC_MODE $EXT3_MODE" | tee -a $LOG_FILE
echo "# -Backup Src : $BACKUP_SOURCE_DIR " | tee -a $LOG_FILE
echo "# -Backup Dest : $BACKUP_DEST_DIR" | tee -a $LOG_FILE
echo "# -Log File : $LOG_FILE" | tee -a $LOG_FILE
echo "# " | tee -a $LOG_FILE
echo "######################################################################" | tee -a $LOG_FILE
echo " " | tee -a $LOG_FILE
((echo " rsync start at: `date` " | tee -a $LOG_FILE) 3>&1 1>&2 2>&3 | tee -a $LOG_FILE) 3>&1 1>&2 2>&3
echo " " | tee -a $LOG_FILE
#--------------------------------------
# Check to see if -n was passed in
#--------------------------------------
if [ $opt_n ]; then
echo " ---------------------------------------------------- " | tee -a $LOG_FILE
echo " WARNING: NOT RUNNING RSYNC -n SWITCH WAS PASSED IN " | tee -a $LOG_FILE
echo " ---------------------------------------------------- " | tee -a $LOG_FILE
else
((rsync -rlptD --delete --exclude="@eaDir" $RSYNC_MODE $EXT3_MODE $BACKUP_SOURCE_DIR $BACKUP_DEST_DIR | tee -a $LOG_FILE) 3>&1 1>&2 2>&3 | tee -a $LOG_FILE) 3>&1 1>&2 2>&3
fi
echo " " | tee -a $LOG_FILE
((echo " rsync end at : `date` " | tee -a $LOG_FILE) 3>&1 1>&2 2>&3 | tee -a $LOG_FILE) 3>&1 1>&2 2>&3
|
Third Script (Automatic Backup of Synology DS to a directly attached USB-Drive)
The purpose of this script is to backup files from the Synology Diskstation to a directly attached USB-Drive.
ArchitectureHere's the architecture used in the backup script below:
Shared Folder StructureHere's the NAS shared folder structure (as shown in the Disk Station Manager under section "Privileges/Shared Folder"): + ... + 01nasbackup (folder for PC-backups) + music (folder for music files) + ... + script (folder for maintenance scripts and logfiles) <<<< add this shared folder to your folders! + ... Directory StructureHere's the directory structure of the NAS and the backup device (as can be seen when logging in to the DS via telnet):
/
+ ...
+ volume1 (the volume to be backuped)
+ ...
+ 01nasbackup
+ music
+ ...
+ script
+ ...
+ volumeUSB1 (the backup volume) <<<< this volume automatically will be added for your USB drive!
+ usbshare
+ BUP_DS <<<< this drectory structure will be created by the backup script!
+ ...
+ 01nasbackup
+ music
+ ...
+ script
+ ...
Script and Logfile Structure in the Shared Folder "script"In the shared folder "script" (folder for maintenance scripts and logfiles) the directories and files are structured as follows. Please add the same directory and file structure to your NAS.
+ backup2nas <<<< maintenance scripts for backup's from PCs in the local LAN to the NAS
- ... <<<< scripts for backup PC's to NAS (see **)
+ backup2usb <<<< scripts for backup the Synology DS to USB drive)
- cronjob_backup2usb.sh <<<< backup script to be started by cronjob via crontab)
- cronjob_backup2usb_dirlist <<<< NAS directory list for backup script - which directories should be backuped)
+ crontab <<<< see hints below
- ...
+ logfile <<<< logfiles)
- backup2usb.log <<<< will be created by the backup script
- ... <<<< logfiles from backup PC's to NAS (see **)
Remark ** Backup to the Synology Server using Robocopy Backup ScriptThis is the backup script "cronjob_backup2usb.sh" which is started via cronjob, placed in the directory "backup2usb" in share "script". Please copy the text to your script!
#!/bin/ash
# MG
echo "-----------------------------------------------"
echo "Start:" $0
echo "-----------------------------------------------"
date "+%a %d.%m.%y %H:%M:%S"
echo "-----------------------------------------------"
options="-rtv --stats --modify-window=1"
destin=/volumeUSB1/usbshare/BUP_DS
for source in $(cat /volume1/script/backup2usb/cronjob_backup2usb_dirlist)
do
echo "Source: " $source
echo "Destination: " $destin
rsync $options $source $destin
# echo "rsync" $options $source $destin
retcode=$?
if [ $retcode -gt 0 ]
then
echo "ERROR: rsync " $source "failed with code " $retcode
fi
echo "-----------------------------------------------"
date "+%a %d.%m.%y %H:%M:%S"
echo "-----------------------------------------------"
done
echo "-----------------------------------------------"
echo "End:" $0
echo "-----------------------------------------------"
exit 0
Directory List for Backup ScriptThis is an example for the textfile "cronjob_backup2usb_dirlist" (NAS directory list for the backup script - which directories should be backuped), placed in the directory "backup2usb" in share "script". Please create a similar textfile for your purposes. /volume1/01nasbackup /volume1/music /volume1/script CrontabThis is an example of the crontab entry. in this example the backup script will be started 0:30 am every day. The crontab can be found in "/etc/crontab". Please edit your crontab file in a similar way.
#minute hour mday month wday who command 0 0 1 * * root /usr/sbin/ntpdate -b ntp.via.ecp.fr 30 0 * * * root /bin/ash /volume1/script/backup2usb/cronjob_backup2usb.sh >> /volume1/script/logfile/backup2usb.log LogfileThe logfile of the backup script has the following structure: ----------------------------------------------- Start: /volume1/script/backup2usb/cronjob_backup2usb.sh ----------------------------------------------- Sun 02.11.08 00:30:16 ----------------------------------------------- Source: /volume1/01nasbackup Destination: /volumeUSB1/usbshare/BUP_DS building file list ... done ... ... ... Number of files: xxxxx Number of files transferred: xxxx Total file size: xxxxx bytes Total transferred file size: xxxx bytes Literal data: 0 bytes Matched data: 0 bytes File list size: xxxxx File list generation time: xx.xxx seconds File list transfer time: 0.000 seconds Total bytes sent: xxxxx Total bytes received: xxxxx sent xxxxx bytes received xxxxx bytes xxxx.xx bytes/sec total size is xxxxxx speedup is xxxxx ----------------------------------------------- Sun 02.11.08 00:33:23 ----------------------------------------------- Source: /volume1/music Destination: /volumeUSB1/usbshare/BUP_DS building file list ... done ... ... ... sent xxxxxx bytes received xxxxxx bytes xxxxx.xx bytes/sec total size is xxxxxxxx speedup is xxxx.xx ----------------------------------------------- Sun 02.11.08 00:35:07 ----------------------------------------------- ----------------------------------------------- End: /volume1/script/backup2usb/cronjob_backup2usb.sh ----------------------------------------------- HintsHere you can find some hints for using the script above: Map "script" share to Windows driveTo have easy access to your scripts and logfiles just map "script" within your windows explorer to drive letter S: Editing Files On Diskstation with a Windows EditorGeneral hint: When you edit text files (e.g. the script "cronjob_backup2usb.sh") with built in Windows editors like WordPad (wordpad.exe) you will probably have problems with the character set (espacially with the carriage return - linefeed sequence at each end of a line in the textfile) on the linux based NAS then.
To avoid this problems always edit your files with WordPad and afterwards convert it with tools like "fromdos.exe" (tofrodos)
Editing the crontab: The easiest way to edit the crontab (/etc/crontab) is the built in vi program - unfortunately difficult for non unix people like me. So I copied the file /etc/crontab with the cp command using telnet to /volume1/script/crontab (see directory structure above). After that I edited the crontab under Windows with WordPad. After that I used tofrodos (see above hint) and finally copied it back to /etc/crontab.
How to Backup your PCs to the NASIf you're also interested in backing up your PCs to NAS, have a look here:
Further Resources |
