dsyleixa123
Posts: 343
Joined: Mon Jun 11, 2018 11:22 am

correct backup command?

Sun Nov 25, 2018 6:50 pm

hello,
I have an USB drive for backup/restore, and the following directories which have to be synced:
/media/pi/USB120DRV/sav/raspi_sav/raspiProgs
/home/pi/raspiProgs

I want to copy all files from the first raspiProgs dir to the 2nd raspiProgs dir.

1st question:
is the following command correct for this purpose?

Code: Select all

sudo cp -rf /media/pi/USB120DRV/sav/raspi_sav/raspiProgs  /home/pi/raspiProgs  


2nd,
can I extend this command so that only those files will be copied,
a) if a file in dir2 ist not existing at all, or
b) if a file in dir1 is newer than a similar one in dir2? (overwrite only if newer),
c) and additionally, identical files (by name and date) have to be ignored

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: correct backup command?

Sun Nov 25, 2018 10:00 pm

dsyleixa123 wrote:
Sun Nov 25, 2018 6:50 pm
can I extend this command so that only those files will be copied,
a) if a file in dir2 ist not existing at all, or
b) if a file in dir1 is newer than a similar one in dir2? (overwrite only if newer),
c) and additionally, identical files (by name and date) have to be ignored
Take a look at rsync, this is designed to copy changes only (and can optionally work over the network for remote paths).

In it's simplest form you might use something like

Code: Select all

rsync -av /source/dir/ /destination/dir/
Various options will control how the files are compared (date/checksum), whether to cross filesystem boundaries, whether to delete files from the destination if they're no longer in the source, whether to preserve hard links etc. The man page lists all the options and what they do.

jahboater
Posts: 4601
Joined: Wed Feb 04, 2015 6:38 pm

Re: correct backup command?

Sun Nov 25, 2018 10:04 pm

For cp to copy a directory tree, I prefer -a

-a, --archive
same as -dR --preserve=all

but rsync is the better option for backups.

dsyleixa123
Posts: 343
Joined: Mon Jun 11, 2018 11:22 am

Re: correct backup command?

Mon Nov 26, 2018 9:25 am

thank you about rsync!

tbh, I am unsure about the final slashes and the cmd line parameters:
if I want to mirror dir1 to dir2 including all files and subdirs, without any extra directory level, and keep all file rights,
is it

Code: Select all

rsync -avz --delete   /source/dir1/   /destination/dir2/
or
rsync -avz --delete   /source/dir1/   /destination/dir2         ###  edit, IIUC this one would match it
or
rsync -avz --delete   /source/dir1    /destination/dir2

2nd question:
if the dir tree in destination drive does not yet exist, will all dirs be created from the scratch?

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: correct backup command?

Mon Nov 26, 2018 5:35 pm

dsyleixa123 wrote:
Mon Nov 26, 2018 9:25 am
thank you about rsync!

tbh, I am unsure about the final slashes and the cmd line parameters:
Honestly I struggle to remember when you want trailing slashes on each path so I often end up with some trial and error to get the right set.
dsyleixa123 wrote:
Mon Nov 26, 2018 9:25 am
2nd question:
if the dir tree in destination drive does not yet exist, will all dirs be created from the scratch?
I'm not sure if all the containing folders would be created (i.e. you might need to have created /destination), but /destination/dir2 might get created for you

For rsync between folders on the same device I'm not sure the -z (compression) flag will make a difference, that's going to help more if your copying between difference machines.

dsyleixa123
Posts: 343
Joined: Mon Jun 11, 2018 11:22 am

Re: correct backup command?

Mon Nov 26, 2018 5:42 pm

ok, thank you so far!
tbh, I thought the z parameter was more about keeping all file attributes... :?
hmmmh.
Can someone else perhaps bring some light into this riddle about slashes and parameters? :roll:

Roken
Posts: 292
Joined: Sun Dec 31, 2017 4:35 pm
Location: UK

Re: correct backup command?

Mon Nov 26, 2018 8:11 pm

I've always found

Code: Select all

sudo rsync -aAXv  .................
Has worked well in all situations for me (including backing up the OS. NB, backing up the OS requires several --exclude s
Headless PI. OMG, someone cut it's head off. Oh, hang on. it didn't have one to start with.

dsyleixa123
Posts: 343
Joined: Mon Jun 11, 2018 11:22 am

Re: correct backup command?

Tue Nov 27, 2018 9:17 am

where can I find an understandable (beginner-friendly) list and description about all rsync parameters? the man page and https://ss64.com/bash/rsync_options.html are just puzzling me... :roll:

PhilBr
Posts: 62
Joined: Tue Oct 31, 2017 5:28 pm

Re: correct backup command?

Tue Nov 27, 2018 11:29 pm

I understand that you do require the trailing slashes for directory names for rsync.
See this description:
http://www.mikerubel.org/computers/rsyn ... ots/#Rsync
Phil

dsyleixa123
Posts: 343
Joined: Mon Jun 11, 2018 11:22 am

Re: correct backup command?

Wed Nov 28, 2018 12:32 pm

thanks a lot, these were the crucial statements:
Then this command:
rsync -a a b
produces b/a/foo,

whereas this command:
rsync -a a/ b
produces b/foo.

The presence or absence of a trailing slash on the destination argument (b, in this case) has no effect.
I also meanwhile understood that -z in fact is for compression, whilst -A keeps the attributes.

Return to “Beginners”