0

How can I connect to a remote host using SSH and create one Bash script to copy all files and folder from my old server to my new server for backup every day?

7
  • No i need one bash script and all process will be done in terminal, no GUI. Apr 2, 2018 at 12:19
  • Please read the question and answers I provided a link to. Quite a few of the tools, like duplicity and rsnapshot, is command line applications! You first need to decide which tool to use, and then you can automate it with scripting.
    – vidarlo
    Apr 2, 2018 at 12:21
  • No I don't need any external tools for backup system, i need a bash script write in terminal and like cron job where i give ssh details and its copy all folder from target server to my new server in everyday. Apr 2, 2018 at 12:27
  • 2
    Then write a script with scp /path/to/backup user@remote:/storage/space as the sole content, and set up key based ssh login. But trust me, you do want a backup system, such as rsnapshot or duplicity, so that you can go back in time. What you propose is a mirror...
    – vidarlo
    Apr 2, 2018 at 12:29
  • i cant understand where i give ssh details password in your above script. and its run every day like a cron job not manually, and your link also not understand what is ssh key ? and how its relate with my question and ssh key. Apr 2, 2018 at 12:33

1 Answer 1

3

Set up key-based ssh authentication

First, you need to generate a ssh key. On the machine you're connecting from, run:

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vidarlo/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/vidarlo/.ssh/id_rsa.
Your public key has been saved in /home/vidarlo/.ssh/id_rsa.
The key fingerprint is:
SHA256:/jxfxiWiao0m7YG9MiHgXBFKoo7kJcgTOrPtAZNtpVg vidarlo@hannah.bitsex.no
The key's randomart image is:
+---[RSA 2048]----+
|..E o.           |
|=B.+.            |
|@==. .           |
|=O= .            |
|o=oo    S   . . .|
| .o.. .+   . o o |
|  .  ..o+o.   +  |
|      + =*o  o   |
|       B+ oo.    |
+----[SHA256]-----+
[~]$ 

Just press enter when asked; the default locations and no passphrase is OK.

This will generate a private and public key. Next step is to copy the public key to the remote server, so that it can be used. ssh-copy-id can be used for this:

$ ssh-copy-id user@host
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/vidarlo/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
user@host's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'user@host'"
and check to make sure that only the key(s) you wanted were added.

At this stage you should be able to run ssh user@host, and get logged in without entering a password.

Backup job

You want a simple scp. This is bad for several reasons:

  1. You don't get any history. If a file is overwritten by mistake, and you do not discover it before the next backup job, scp will happily overwrite the content.
  2. You have to copy all the content every night.
  3. You don't get a status report.
  4. If a backup job does not finish in time, you risk having two backup jobs writing to the same content.

But, anyway. This can be done, as long as you're aware of the caveats. Use crontab -e to edit your users crontab. Insert a line like this:

0 5 * * * /usr/bin/scp "/path/to/backup" "user@remote:/path/to/store/backups"

This command will run nightly at 05:00. This can be changed if you desire. The explanation of the fields is as follows:

  1. minutes, 0-60. 0 means run it at xx:00, * means run it every minute
  2. hours, 0-23. 02 means 02:xx. * means every hour.
  3. Day of month, 1-31. * means every day.
  4. Month, 1-12. * is every month
  5. Day of Week, 1-7.
9
  • got "The key fingerprint" and "The key's randomart image" in terminal and got two file in my file location one demo and one demo.pub but what is my "ssh-copy-id" in my new server. ? Apr 2, 2018 at 13:02
  • +1 for solid and detailed answer BUT vidarlo is 100% correct that SCP is NOT the way to go for standard nightly backups. I know vidarlo already said that, but just reiterating.
    – Robby1212
    Apr 2, 2018 at 13:10
  • ssh-copy-id copies the public key (the file ending in .pub) to the remote host.
    – vidarlo
    Apr 2, 2018 at 13:11
  • @vidarlo anirban@anirban-ThinkPad-T420:~$ /home/anirban/demo_script.pub v@192.168.0.110 bash: /home/anirban/demo_script.pub: Permission denied anirban@anirban-ThinkPad-T420:~$ Apr 3, 2018 at 6:33
  • I have no idea what you're trying to achieve with that command...
    – vidarlo
    Apr 3, 2018 at 6:55

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