I am using Ubuntu 12.10 server edition Now i need script which will backup Samba Share to USB drive attached to the server and also generate the back up log with Log File format containing File Name, Size and Time

Also confirm the Crontab Settings

Thanks in advance

  • 1
    12.10 is EOL(End Of Life). please upgrade to a newer version. 16.04 will be release this Thursday, and will be supported for 5 years
    – blade19899
    Apr 19 '16 at 8:53
  • 12.10 is super dead. Replacing it with a fresh install should be your priority for the day. Anything as old as 12.10 connected to the internet, especially running services, is almost certainly infected with something by now. It hasn't had a single security update in 2 years.
    – Oli
    Apr 19 '16 at 10:31

If you don't have installed rsync install them

sudo apt-get install rsync

Create script backup_script.sh


# Script to backup personal files to the external USB drive.
# Specify the mount point here (DO NOT end mount_point with a forward-slash).

echo "#####"
echo ""
# Check whether target volume is mounted, and mount it if not.
if ! mountpoint -q ${mount_point}/; then
    echo "Mounting the external USB drive."
    echo "Mountpoint is ${mount_point}"
    if ! mount ${mount_point}; then
        echo "An error code was returned by mount command!"
        exit 5
    else echo "Mounted successfully.";
else echo "${mount_point} is already mounted.";
# Target volume **must** be mounted by this point. If not, die screaming.
if ! mountpoint -q ${mount_point}/; then
    echo "Mounting failed! Cannot run backup without backup volume!"
    exit 1

echo "Preparing to transfer differences using rsync."

# Use the year to create a new backup directory each year.
current_year=`date +%Y`
# Now construct the backup path, specifying the mount point followed by the path
# to our backup directory, finishing with the current year.
# (DO NOT end backup_path with a forward-slash.)

echo "Backup storage directory path is ${backup_path}"

echo "Starting backup of /home/XXX/Pictures . . . "
mkdir --parents ${backup_path}/Pictures
# This time use the -a flag with the tee command, so that it appends to the end
# of the rsync-output.txt file rather than start a new file from scratch.
sudo rsync --archive --verbose --human-readable --itemize-changes --progress --no-o --no-g \
--delete --delete-excluded \
/home/XXX/Pictures/ ${backup_path}/Pictures/ 2>&1 | tee -a /home/XXX/rsync-output.txt

echo ""
echo "####"

You must change

  • mount point of your ext USB - in example I use /mnt/
  • path to folder you wish to backup - in exampl I use /home/XXX/Pictures
  • place to to write log file - in example I use /home/XXX/

Give script execute privilege

chmod +x /path_to_script/backup_script.sh

Setup crontab to run this command on every x period of time

For editing crontab run

crontab -e

For running command on every 5 min code is

*/5 * * * * /path_to_script/backup_script.sh

on every hour

0 */1 * * * /path_to_script/backup_script.sh

to run on 4 am

0 4 * * * /path_to_script/backup_script.sh

Optionaly, you can send log file to your mail with command

mail -s "SMB backup" your@mail.com < /path_to_log/rsync-output.txt

Setup cron to do for you

0 4 * * * mail -s "SMB backup" your@mail.com < /path_to_log/rsync-output.txt

There are different packages you can use for backups. I personally use rsnapshot which encapsulates rsync.

There you have some configuration-file where you can specify all your wishes.

Not the answer you're looking for? Browse other questions tagged or ask your own question.