aimew4
Posts: 36
Joined: Wed May 06, 2020 2:12 am

cron + rsync

Fri Aug 07, 2020 10:02 pm

I have a Pi set up with samba. I want to have what is saved to this network drive automatically copied/synchronized onto a few separate file locations. I think I should be using rsync and a cron job. How?

(I am not an advanced user so much as I think this is an advanced use)

thx :D

LTolledo
Posts: 3778
Joined: Sat Mar 17, 2018 7:29 am
Location: Anime Heartland

Re: cron + rsync

Fri Aug 07, 2020 10:08 pm

first make a bash script for the rsync command you want to use
test it several times to see it it works as you intended.
then use cron to run that bash script at the time you want it to run.

its best to do the test on another separate setup
so as not to mess up your current and valued setup.
"Don't come to me with 'issues' for I don't know how to deal with those
Come to me with 'problems' and I'll help you find solutions"

Some people be like:
"Help me! Am drowning! But dont you dare touch me nor come near me!"

bjtheone
Posts: 896
Joined: Mon May 20, 2019 11:28 pm
Location: The Frozen North (AKA Canada)

Re: cron + rsync

Sun Aug 09, 2020 2:58 pm

I am hybrid booting and run off a SSD. I backup my user account to the boot SD card. While testing, using the -av switch for rsync so you can see exactly what you are backing up. Obviously the paths would have to change but it would look something like:

Code: Select all

bj@devnull:~/bin $ crontab -l
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
7 2 * * * /home/bj/bin/account_backup.sh > /home/bj/logs/backup.log 2>&1
with a scipt file like:

Code: Select all

bj@devnull:~/bin $ more /home/bj/bin/account_backup.sh 
#!/bin/bash
#
# Backup /home/bj to SD card
#
NOW="$(date)"
echo "Backup starting: $NOW"
/usr/bin/rsync -a /home/bj /media/bj/rootfs/home
NOW="$(date)"
echo "Backup ending: $NOW"

aimew4
Posts: 36
Joined: Wed May 06, 2020 2:12 am

Re: cron + rsync

Mon Aug 10, 2020 2:37 am

Thank you :)

I am using rsync right. Need help with the cron. What is the > for?

I entered:

rsync_backup.sh

Code: Select all

NOW="$(date)"
echo "Backup starting: $NOW"
# rsync -r /home/pi/Desktop/Synching/ /media/pi/A16a
# rsync -r /home/pi/Desktop/Synching/ /media/pi/A16b
rsync -r --delete /home/pi/Desktop/Synching/ /media/pi/A16a
rsync -r --delete /home/pi/Desktop/Synching/ /media/pi/A16b
NOW="$(date)"
echo "Backup ending: $NOW"
with

Code: Select all

crontab -e
using

Code: Select all

25 */3 * * * /home/pi/Desktop/rsync_backup.sh

User avatar
rpdom
Posts: 17424
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: cron + rsync

Mon Aug 10, 2020 7:04 am

aimew4 wrote:
Mon Aug 10, 2020 2:37 am
What is the > for?
A ">" on the end of a command line redirects the standard output of the command to a filename which follows the ">".

A "2>" on the end of the line redirects the error output of the command to the following filename, or if "2>&1" is used, it means to redirect the error output to the same place as standard output.

These are very useful when running a cron job as you are able to check the output instead of it being thrown away and you wondering why it didn't work.
Unreadable squiggle

bjtheone
Posts: 896
Joined: Mon May 20, 2019 11:28 pm
Location: The Frozen North (AKA Canada)

Re: cron + rsync

Mon Aug 10, 2020 4:37 pm

In this case I am mainly concerned about rsync issues, but it also would capture cron issues. As a suggestion, all automated tasks should have some sort of logging captured so their correct operation can be validated. Without the verbose option on rsync, the log file stays small as a successful run just captures the start and end time of the backup, and overwrites the log file each time it runs.

Return to “Advanced users”