-   Linux - Newbie (
-   -   Convert Windows xcopy script to Linux for Backup (

coderlen 05-31-2018 04:49 AM

Convert Windows xcopy script to Linux for Backup
I'm trying to convert a Windows script which I used for backups on my Windows computer. It worked very well in Windows, and now I would like to use the same idea on my Ubuntu files.

The Windows script just used a series of xcopy commands and named the directories I wanted backed up. It would go through and see if any of the files in each directory had changed, and then it would copy only those files to my flash drive. It worked like a charm. Very simple, very effective.

The flash drive ended up being a composite copy of all my important directories from the hard drive of my laptop. It was a life saver, and it saved me several times when my hard drive crashed.

I would like a Linux command line script to do the same. I did some research on the cp command, and it seems as if the "cp" command would be the answer, but I had some problems getting it to work correctly. I did get as far as getting the script to be executable, but it kept getting errors and never performed for me.

Here is an extract of the Windows bat file:
xcopy "C:\Bills" "D:\Bills" /s /m /y
xcopy "C:\Securities" "D:\Securities" /s /m /y
xcopy "C:\Taxes" "D:\Taxes" /s /m /y

Here is an extract of my Linux script, which is not working:
cp -ur /home/len/len/Bills /media/len/UUI/Bills
cp -ur /home/len/len/Securities /media/len/UUI/Securities
cp -ur /home/len/len/Taxes /media/len/UUI/Taxes

A little help, please. Thanks.

pierre2 05-31-2018 05:36 AM

that xcopy is an advanced version of copy,
- whereas cp in Linux is just a plain copy command.

and that UUI is also a variable, as well, and does that match the UUI of the destination ?.

teckk 05-31-2018 05:47 AM


cp -ur /home/len/len/Bills/* /media/len/UUI/Bills/

xamaco 05-31-2018 06:33 AM

I use rsync for my backups. It's fast and very versatile. It has a lot of options and needs a careful reading of its man page (which is good and contains examples).

TenTenths 05-31-2018 06:56 AM


Originally Posted by xamaco (Post 5861665)
I use rsync for my backups. It's fast and very versatile. It has a lot of options and needs a careful reading of its man page (which is good and contains examples).

And the advantage or rsync is that it won't copy files that are the same at the destination, making it much better and faster if there's a lot of files.

syg00 05-31-2018 06:57 AM

Yes, you need to look at rsync if you want to only copy the changes, not all the files all the time as "cp" will do.

Note that your strategy exposes you as you only have one copy of your backup data. Backup devices also fail.

coderlen 05-31-2018 07:41 AM

Thanks teckk. I tried what you suggested. Here is the result on my Linux command line:

~/len/Backup$ ./
cp: target '/media/len/UUI/Bills/'$'\r' is not a directory
cp: target '/media/len/UUI/Securities/'$'\r' is not a directory
cp: target '/media/len/UUI/Taxes/'$'\r' is not a directory

What do I do to correct this?

And the others who responded with the rsync suggestions, I tried rsync after reading the man page, but I was unable to get it to work. Please suggest an actual example with the rsync command, with options, so that I can give it a try. Thanks.

pan64 05-31-2018 07:46 AM

you wrote your script in windows and all the lines contain a \r. You need to remove it first. see the tool dos2unix.
how did you try rsync?

AwesomeMachine 05-31-2018 07:54 AM

You really don't need to write a script. There are programs that will do exactly what you want. xamaco mentioned that he uses rsync. But he also said to completely read the man page. You don't really need to do that. For any ordinary copying this will do

$ rsync -av source destination
That will transfer only new or newer (more recent modify time) files. And, rsync does integrity checking, so you know the back up is sound.

scasey 05-31-2018 06:57 PM

Install, configure and use rsnapshot , which uses rsync.
Its configuration file will set up and automate multiple generations of backups. I use it to perform daily off-site backups.

coderlen 06-07-2018 05:24 AM

I had my doubts about rsync. The first time I used it, it took a long time to back up all the files, I guess since it was the first time doing it.

rsync -av /home/len/len/* /media/len/56E9-5630/

But later, when I had made some modifications to some of the files, and I ran that same command again, magically, it backed up ONLY the files which were changed, and it did it in a hurry. Wow! That's just exactly what I needed.

When I used to use Windows and did my backup script with xcopy, it took forever, even if there was only one file changed. rsync is super fast.

I did try rshapshot, as rcasey recommended. It was a bit intimidating for me, so I didn't try it. It looks OK though. I just can't speak to it, as I didn't try it.

teckk and AwesomeMachine, your responses helped me the most, although it took me awhile to get my arms around it. The command is so simple, even a child can execute it.

The only real problem I had was identifying exactly what my flash drive was called for the Destination, and exactly how to code the Source. I hard-coded the reference to the Destination as "56E9-5630". There's probably a better way of doing it, but this works for me, for now.

Thanks everyone, for your responses. I am astounded as to how fast rsync executes.

All times are GMT -5. The time now is 11:57 AM.