3

I am trying to automate an import backup script.

For that, I am using this command to get the last backup file name:

BACKUP_NAME=$(ssh user@XX.XX.XX.XX-t "ls -w1 /mnt/prod_backup" | tail -1)

And then I use

scp -3 user@$YY.YY.YY.YY:/mnt/prod_backup/$BACKUP_NAME /home/coldstorage_import/

to try download the file so I can use it to restore the local database.

But I get this error:

: No such file or directoryackup/backup_prod_2021-11-29-03-00-01.sql

The patch error looks wrong.

When, I try to download the file by copy/pasting the file name manually the command works.

My guess is that there is a special/hidden char somewhere perhaps but I can't figure it out..

Edit:

When running this command to try see the hidden characters

echo $BACKUP_NAME | cat -v

I get

bizstore_backup_prod_2021-11-16-03-00-01.sql^M

I am not sure if ^M is normal or not.

5
  • 4
    Looks like there was a CR character in the output of ssh. Is the remote or local machine running some Microsoft OS? Nov 29 '21 at 17:21
  • @StéphaneChazelas Both servers are running the same version of Ubuntu. But I am using Git bash from windows to connect to one of them. I edited my answer, to show the result of a command which should show the hidden char. Nov 29 '21 at 17:26
  • @Archemar It is a typo, I forget to update one of the command when trying to simplify the path. I edited my question, sorry for that. Nov 29 '21 at 17:27
  • 1
  • 2
    Not an answer to your question, but an optimisation/potential bug-fix: You should use ls -rt /mnt/prod_backup/*.sql so that the files are sorted by reverse time and only those ending in .sql are listed. Note: if you have filenames with newlines in them, you'll have to do something like (requires GNU utils): find /mnt/prod_backup/ -maxdepth 1 -type f -iname '*.sql' -printf '%T@\t%p\0' | sort -zrn | head -zn1 | cut -zf2-. Also, double-quote your variables when you use them - it only takes one filename with an unexpected space (or ; or & or whatever) to break your script.
    – cas
    Nov 30 '21 at 2:36
6

Drop the -t from your ssh command and the ^M won't appear in the filename:

BACKUP_NAME="$(ssh user@XX.XX.XX.XX "ls -w1 /mnt/prod_backup" | tail -1)"
1
  • 3
    Once you remove the -t you also don't need -w1 on ls because with stdout to a pipe (not a pty) it automatically does one-entry-per-line. Nov 30 '21 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.