If you don't have installed rsync
install them
sudo apt-get install rsync
Create script backup_script.sh
#!/bin/bash
# Script to backup personal files to the external USB drive.
# Specify the mount point here (DO NOT end mount_point with a forward-slash).
mount_point='/mnt/'
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.";
fi
else echo "${mount_point} is already mounted.";
fi
# 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
fi
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.)
backup_path=${mount_point}'/rsync-backup/'${current_year}
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" [email protected] < /path_to_log/rsync-output.txt
Setup cron to do for you
0 4 * * * mail -s "SMB backup" [email protected] < /path_to_log/rsync-output.txt