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?

Zanna
  • 70,465
  • No i need one bash script and all process will be done in terminal, no GUI. – Anirban Ghosh Apr 02 '18 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 02 '18 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. – Anirban Ghosh Apr 02 '18 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 02 '18 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. – Anirban Ghosh Apr 02 '18 at 12:33
  • SSH keys is authentication with a key, and not a password. This allows passwordless authentication for ssh, which means it's easier to automate ssh operations. In short, generate a key with ssh-keygen and then follow this Q&A to copy it to the remote machine. This will allow passwordless login. Then put the scp command in a cronjob. But I urge you to look into for instance rsnapshot or duplicity... – vidarlo Apr 02 '18 at 12:36
  • Ok can you tell me one backup tools where i give all details of both target server and backup sever and its automatically backup in every day. and your link there are so many tools i cant decide, i think Rsync and Duplicati looks best. – Anirban Ghosh Apr 02 '18 at 12:49

1 Answers1

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.
vidarlo
  • 22,691
  • 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. ? – Anirban Ghosh Apr 02 '18 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 02 '18 at 13:10
  • ssh-copy-id copies the public key (the file ending in .pub) to the remote host. – vidarlo Apr 02 '18 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:~$ – Anirban Ghosh Apr 03 '18 at 06:33
  • I have no idea what you're trying to achieve with that command... – vidarlo Apr 03 '18 at 06:55
  • @vidarlo ok i will explain, already make one public key on my remote server in .ssh with name auth.pub but cant connect without ssh password. i think if .pub file work perfect i can login ssh without password am i right ? – Anirban Ghosh Apr 03 '18 at 09:04
  • On the remote server it should not be auth.pub, but authorized_keys. If you use ssh-keygen with default filenames, and ssh-copy-id it should work. – vidarlo Apr 03 '18 at 09:06
  • Already create authorized_keys in remote server now going to connect remote server from my local machine anirban@anirban-ThinkPad-T420:~$ ssh-copy-id v@192.168.0.110

    /usr/bin/ssh-copy-id: ERROR: failed to open ID file '/home/anirban/.pub': No such file (to install the contents of '/home/anirban/.pub' anyway, look at the -f option)

    – Anirban Ghosh Apr 03 '18 at 10:01
  • @AnirbanGhosh I suggest you follow instructions. There should be no file named .pub in your home directory. It should be ~/.ssh/id_rsa.pub... also see this Q&A – vidarlo Apr 04 '18 at 15:06