2

I am trying to scp from a remote and I want to copy the whole filesystem (it is a small embedded linux device). SCP does not like the way I am trying it:

scp -r name@ip:/ ./local_folder

I get the following error:

error: unexpected filename:

And if I run it with -v the last message looks like so:

debug1: Sending command: scp -v -r -f /
Entering directory: D0755 0 
Sink: D0755 0 
error: unexpected filename:

If I instead do

scp -r name@ip:/etc ./local_folder

The same debug snippet looks like this:

debug1: Sending command: scp -v -r -f /etc
Entering directory: D0755 0 etc
Sink: D0755 0 etc

It is kind of hard to search, I cannot find any people with the same problem if I try search terms like "scp root directory slash unexpected filename" etc.

Charlee
  • 13

5 Answers5

2

Since scp follows any encountered symbolic link, it would make more sense to use a tool that copies files as they are.

The rsync program is able to recreate a directory structure better than scp:

rsync -av name@ip:/ ./local_folder

This not only preserves file metadata (timestamps etc.), but also copies hidden files found in the / directory (if there are any).

You may make rsync preserve hard links by additionally using -H.

rsync uses ssh as its default transport.


You could also use tar on the remote system, over ssh:

ssh name@ip tar -c -f - -C / . >local_folder/filesystem.tar

or

ssh name@ip tar -c -f - -C / . | tar -v -x -f - -C local_folder
Kusalananda
  • 333,661
2

tldr: try name@ip:// instead of name@ip:/

I was trying similar thing. I wanted to run an scp script using WINScp and without specifying default remote directory, my script always connected to the same, seemingly random, folder inside the file system. I guess it was a home directory for my username.

If the url ended with /, i.e name@ip:/, this resulted in connecting to that random home directory. Even if that directory didn't exist (which resulted in weird behaviour when executing some commands...). After executing cd / explicitly, I have been connected to the root directory.

After I changed the / into //, i.e. name@ip://, the scp connected to the root directory, without needing to execute cd /.

andowero
  • 121
  • 2
0

Use scp -r name@ip:/. ./local_folder.

Alternatively, the dd command can be used for bit by bit backups (See this)

  • 1
    This does not work. Thanks for the dd suggestion but I actually want to actually inspect the filesystem – Charlee Jun 24 '21 at 11:26
-2

AdminBee and roaima gave the right solution:

scp -r name@ip:'/*' ./local_folder

Allowed me to copy the whole file system (it also works without quotes btw)

Charlee
  • 13
  • 1
    Does this include any hidden names in the / directory? – Kusalananda Jun 24 '21 at 11:39
  • 1
    It may do something entirely different without quotes. Consider mkdir 'name@ip:'; touch 'name@ip:/boot' and then rerun the scp command "without quotes". You'll be very disappointed because the scp will only copy from the /boot directory. ALWAYS quote wildcards when you don't want the local shell to try and expand them – Chris Davies Jun 24 '21 at 13:17
-2

So actually, for what I wanted I found a much faster option, which also allowed me to ignore some files:

sshfs user@server: sshfsdir
rsync --recursive --exclude=whatever sshfsdir/path/on/server /where/to/store

(from https://stackoverflow.com/questions/15121337/recursively-use-scp-but-excluding-some-folders)

Charlee
  • 13
  • 1
    No... that's much slower than a solution that avoids the sshfs part. Just use rsync -av user@server:/path/on/server/ /where/to/store – Chris Davies Jun 24 '21 at 13:13