scp file to new directory











up vote
0
down vote

favorite












I have written the following script. It creates a new directory formatted like this Year,Month,Day. What it suppose to do is copy a file to the newly created directory, the script makes the directory on the remote server, but copies the file to the directory not the subdirectory which is suppose to be the 2017-08-18, and so on.



[root@hostname ~]# cat ontape.sh
#!/bin/bash
#
sshpass -p 'PASSWORD' ssh root@hostname mkdir -p /home/mybackup/ontape/$(date +%Y-%m-%d)
sshpass -p 'PASSWORD' ssh root@hostname mkdir -p /mybackup/ontape_tmp_backup/$(date +%Y-%m-%d)
for server in $(cat servers.txt)
do
sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* $server
done


Also, the /root/servers.txt file has the following code.



[root@hostname ~]# cat servers.txt
root@hostname:/mybackup/ontape_tmp_backup
root@hostname:/home/mybackup/ontape









share|improve this question




























    up vote
    0
    down vote

    favorite












    I have written the following script. It creates a new directory formatted like this Year,Month,Day. What it suppose to do is copy a file to the newly created directory, the script makes the directory on the remote server, but copies the file to the directory not the subdirectory which is suppose to be the 2017-08-18, and so on.



    [root@hostname ~]# cat ontape.sh
    #!/bin/bash
    #
    sshpass -p 'PASSWORD' ssh root@hostname mkdir -p /home/mybackup/ontape/$(date +%Y-%m-%d)
    sshpass -p 'PASSWORD' ssh root@hostname mkdir -p /mybackup/ontape_tmp_backup/$(date +%Y-%m-%d)
    for server in $(cat servers.txt)
    do
    sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* $server
    done


    Also, the /root/servers.txt file has the following code.



    [root@hostname ~]# cat servers.txt
    root@hostname:/mybackup/ontape_tmp_backup
    root@hostname:/home/mybackup/ontape









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have written the following script. It creates a new directory formatted like this Year,Month,Day. What it suppose to do is copy a file to the newly created directory, the script makes the directory on the remote server, but copies the file to the directory not the subdirectory which is suppose to be the 2017-08-18, and so on.



      [root@hostname ~]# cat ontape.sh
      #!/bin/bash
      #
      sshpass -p 'PASSWORD' ssh root@hostname mkdir -p /home/mybackup/ontape/$(date +%Y-%m-%d)
      sshpass -p 'PASSWORD' ssh root@hostname mkdir -p /mybackup/ontape_tmp_backup/$(date +%Y-%m-%d)
      for server in $(cat servers.txt)
      do
      sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* $server
      done


      Also, the /root/servers.txt file has the following code.



      [root@hostname ~]# cat servers.txt
      root@hostname:/mybackup/ontape_tmp_backup
      root@hostname:/home/mybackup/ontape









      share|improve this question















      I have written the following script. It creates a new directory formatted like this Year,Month,Day. What it suppose to do is copy a file to the newly created directory, the script makes the directory on the remote server, but copies the file to the directory not the subdirectory which is suppose to be the 2017-08-18, and so on.



      [root@hostname ~]# cat ontape.sh
      #!/bin/bash
      #
      sshpass -p 'PASSWORD' ssh root@hostname mkdir -p /home/mybackup/ontape/$(date +%Y-%m-%d)
      sshpass -p 'PASSWORD' ssh root@hostname mkdir -p /mybackup/ontape_tmp_backup/$(date +%Y-%m-%d)
      for server in $(cat servers.txt)
      do
      sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* $server
      done


      Also, the /root/servers.txt file has the following code.



      [root@hostname ~]# cat servers.txt
      root@hostname:/mybackup/ontape_tmp_backup
      root@hostname:/home/mybackup/ontape






      scripting directory scp






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 at 22:29









      Rui F Ribeiro

      38.3k1477127




      38.3k1477127










      asked Aug 18 '17 at 19:05









      D.F.

      62




      62






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          You should change your this line and add the directory you created to it as following.



          sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)"


          You also can use rsync instead as it will create last level directory in destination path if it doesn't exist and no need mkdir there as extra command.



          sshpass -p 'PASSWORD' rsync /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)"


          this will create directory from "$(date +%Y-%m-%d)" if it doesn't exist in destination path.



          so in your script as you're going to copy to 2 destinations paths and you are creating directories first, you could done it with only below script.



          for dest in dest1 dest2; do 
          sshpass -p 'PASSWORD' rsync -av /path/to/src/* "$dest/$(date +%Y-%m-%d)"
          done


          Please note that using this way of passing your password is bad practice as it visible to other users has access to your system or can watch via ps -aux command, instead you can set a publikKey authentication.



          ssh-keygen -t rsa
          ssh-copy-id USER@HOST





          share|improve this answer























          • Thank you so much.. I used this one sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)" and it worked great. So this will create a new directory and copy the file to it everyday correct if I run it has a cronjob?
            – D.F.
            Aug 18 '17 at 19:34












          • Yes, it will copy all files in your source path /dbbackup/backupdb/ontape/fullsize/ to destinations paths /mybackup/ontape_tmp_backup/YYYYmmdd and /home/mybackup/ontape/YYYYmmdd, the YYYYmmdd represents today's date and dir which you are creating in your script
            – αғsнιη
            Aug 18 '17 at 19:40








          • 1




            Thank you so much for you help with this. I really do appreciate it. I am new to scripting but needed to get this done and working and while doing this I realized how much i enjoy scripting and learning it.
            – D.F.
            Aug 18 '17 at 19:56






          • 1




            Congratulations; now your password for the remote host is stored in cleartext in your .history for anyone with pysical access to your box to see.
            – DopeGhoti
            Aug 18 '17 at 20:15










          • I got this script to run locally however over the weekend when I try to run the cronjob i notice the new directories were created, however the files did not get copied over. Here is the error message I got cat: servers1.txt: No such file or directory, cat: servers.txt: No such file or directory here is what my cron looks like...
            – D.F.
            Aug 21 '17 at 14:10













          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f387041%2fscp-file-to-new-directory%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          You should change your this line and add the directory you created to it as following.



          sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)"


          You also can use rsync instead as it will create last level directory in destination path if it doesn't exist and no need mkdir there as extra command.



          sshpass -p 'PASSWORD' rsync /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)"


          this will create directory from "$(date +%Y-%m-%d)" if it doesn't exist in destination path.



          so in your script as you're going to copy to 2 destinations paths and you are creating directories first, you could done it with only below script.



          for dest in dest1 dest2; do 
          sshpass -p 'PASSWORD' rsync -av /path/to/src/* "$dest/$(date +%Y-%m-%d)"
          done


          Please note that using this way of passing your password is bad practice as it visible to other users has access to your system or can watch via ps -aux command, instead you can set a publikKey authentication.



          ssh-keygen -t rsa
          ssh-copy-id USER@HOST





          share|improve this answer























          • Thank you so much.. I used this one sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)" and it worked great. So this will create a new directory and copy the file to it everyday correct if I run it has a cronjob?
            – D.F.
            Aug 18 '17 at 19:34












          • Yes, it will copy all files in your source path /dbbackup/backupdb/ontape/fullsize/ to destinations paths /mybackup/ontape_tmp_backup/YYYYmmdd and /home/mybackup/ontape/YYYYmmdd, the YYYYmmdd represents today's date and dir which you are creating in your script
            – αғsнιη
            Aug 18 '17 at 19:40








          • 1




            Thank you so much for you help with this. I really do appreciate it. I am new to scripting but needed to get this done and working and while doing this I realized how much i enjoy scripting and learning it.
            – D.F.
            Aug 18 '17 at 19:56






          • 1




            Congratulations; now your password for the remote host is stored in cleartext in your .history for anyone with pysical access to your box to see.
            – DopeGhoti
            Aug 18 '17 at 20:15










          • I got this script to run locally however over the weekend when I try to run the cronjob i notice the new directories were created, however the files did not get copied over. Here is the error message I got cat: servers1.txt: No such file or directory, cat: servers.txt: No such file or directory here is what my cron looks like...
            – D.F.
            Aug 21 '17 at 14:10

















          up vote
          1
          down vote













          You should change your this line and add the directory you created to it as following.



          sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)"


          You also can use rsync instead as it will create last level directory in destination path if it doesn't exist and no need mkdir there as extra command.



          sshpass -p 'PASSWORD' rsync /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)"


          this will create directory from "$(date +%Y-%m-%d)" if it doesn't exist in destination path.



          so in your script as you're going to copy to 2 destinations paths and you are creating directories first, you could done it with only below script.



          for dest in dest1 dest2; do 
          sshpass -p 'PASSWORD' rsync -av /path/to/src/* "$dest/$(date +%Y-%m-%d)"
          done


          Please note that using this way of passing your password is bad practice as it visible to other users has access to your system or can watch via ps -aux command, instead you can set a publikKey authentication.



          ssh-keygen -t rsa
          ssh-copy-id USER@HOST





          share|improve this answer























          • Thank you so much.. I used this one sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)" and it worked great. So this will create a new directory and copy the file to it everyday correct if I run it has a cronjob?
            – D.F.
            Aug 18 '17 at 19:34












          • Yes, it will copy all files in your source path /dbbackup/backupdb/ontape/fullsize/ to destinations paths /mybackup/ontape_tmp_backup/YYYYmmdd and /home/mybackup/ontape/YYYYmmdd, the YYYYmmdd represents today's date and dir which you are creating in your script
            – αғsнιη
            Aug 18 '17 at 19:40








          • 1




            Thank you so much for you help with this. I really do appreciate it. I am new to scripting but needed to get this done and working and while doing this I realized how much i enjoy scripting and learning it.
            – D.F.
            Aug 18 '17 at 19:56






          • 1




            Congratulations; now your password for the remote host is stored in cleartext in your .history for anyone with pysical access to your box to see.
            – DopeGhoti
            Aug 18 '17 at 20:15










          • I got this script to run locally however over the weekend when I try to run the cronjob i notice the new directories were created, however the files did not get copied over. Here is the error message I got cat: servers1.txt: No such file or directory, cat: servers.txt: No such file or directory here is what my cron looks like...
            – D.F.
            Aug 21 '17 at 14:10















          up vote
          1
          down vote










          up vote
          1
          down vote









          You should change your this line and add the directory you created to it as following.



          sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)"


          You also can use rsync instead as it will create last level directory in destination path if it doesn't exist and no need mkdir there as extra command.



          sshpass -p 'PASSWORD' rsync /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)"


          this will create directory from "$(date +%Y-%m-%d)" if it doesn't exist in destination path.



          so in your script as you're going to copy to 2 destinations paths and you are creating directories first, you could done it with only below script.



          for dest in dest1 dest2; do 
          sshpass -p 'PASSWORD' rsync -av /path/to/src/* "$dest/$(date +%Y-%m-%d)"
          done


          Please note that using this way of passing your password is bad practice as it visible to other users has access to your system or can watch via ps -aux command, instead you can set a publikKey authentication.



          ssh-keygen -t rsa
          ssh-copy-id USER@HOST





          share|improve this answer














          You should change your this line and add the directory you created to it as following.



          sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)"


          You also can use rsync instead as it will create last level directory in destination path if it doesn't exist and no need mkdir there as extra command.



          sshpass -p 'PASSWORD' rsync /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)"


          this will create directory from "$(date +%Y-%m-%d)" if it doesn't exist in destination path.



          so in your script as you're going to copy to 2 destinations paths and you are creating directories first, you could done it with only below script.



          for dest in dest1 dest2; do 
          sshpass -p 'PASSWORD' rsync -av /path/to/src/* "$dest/$(date +%Y-%m-%d)"
          done


          Please note that using this way of passing your password is bad practice as it visible to other users has access to your system or can watch via ps -aux command, instead you can set a publikKey authentication.



          ssh-keygen -t rsa
          ssh-copy-id USER@HOST






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 18 '17 at 20:36

























          answered Aug 18 '17 at 19:21









          αғsнιη

          16.5k102765




          16.5k102765












          • Thank you so much.. I used this one sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)" and it worked great. So this will create a new directory and copy the file to it everyday correct if I run it has a cronjob?
            – D.F.
            Aug 18 '17 at 19:34












          • Yes, it will copy all files in your source path /dbbackup/backupdb/ontape/fullsize/ to destinations paths /mybackup/ontape_tmp_backup/YYYYmmdd and /home/mybackup/ontape/YYYYmmdd, the YYYYmmdd represents today's date and dir which you are creating in your script
            – αғsнιη
            Aug 18 '17 at 19:40








          • 1




            Thank you so much for you help with this. I really do appreciate it. I am new to scripting but needed to get this done and working and while doing this I realized how much i enjoy scripting and learning it.
            – D.F.
            Aug 18 '17 at 19:56






          • 1




            Congratulations; now your password for the remote host is stored in cleartext in your .history for anyone with pysical access to your box to see.
            – DopeGhoti
            Aug 18 '17 at 20:15










          • I got this script to run locally however over the weekend when I try to run the cronjob i notice the new directories were created, however the files did not get copied over. Here is the error message I got cat: servers1.txt: No such file or directory, cat: servers.txt: No such file or directory here is what my cron looks like...
            – D.F.
            Aug 21 '17 at 14:10




















          • Thank you so much.. I used this one sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)" and it worked great. So this will create a new directory and copy the file to it everyday correct if I run it has a cronjob?
            – D.F.
            Aug 18 '17 at 19:34












          • Yes, it will copy all files in your source path /dbbackup/backupdb/ontape/fullsize/ to destinations paths /mybackup/ontape_tmp_backup/YYYYmmdd and /home/mybackup/ontape/YYYYmmdd, the YYYYmmdd represents today's date and dir which you are creating in your script
            – αғsнιη
            Aug 18 '17 at 19:40








          • 1




            Thank you so much for you help with this. I really do appreciate it. I am new to scripting but needed to get this done and working and while doing this I realized how much i enjoy scripting and learning it.
            – D.F.
            Aug 18 '17 at 19:56






          • 1




            Congratulations; now your password for the remote host is stored in cleartext in your .history for anyone with pysical access to your box to see.
            – DopeGhoti
            Aug 18 '17 at 20:15










          • I got this script to run locally however over the weekend when I try to run the cronjob i notice the new directories were created, however the files did not get copied over. Here is the error message I got cat: servers1.txt: No such file or directory, cat: servers.txt: No such file or directory here is what my cron looks like...
            – D.F.
            Aug 21 '17 at 14:10


















          Thank you so much.. I used this one sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)" and it worked great. So this will create a new directory and copy the file to it everyday correct if I run it has a cronjob?
          – D.F.
          Aug 18 '17 at 19:34






          Thank you so much.. I used this one sshpass -p 'PASSWORD' scp /dbbackup/backupdb/ontape/fullsize/* "$server/$(date +%Y-%m-%d)" and it worked great. So this will create a new directory and copy the file to it everyday correct if I run it has a cronjob?
          – D.F.
          Aug 18 '17 at 19:34














          Yes, it will copy all files in your source path /dbbackup/backupdb/ontape/fullsize/ to destinations paths /mybackup/ontape_tmp_backup/YYYYmmdd and /home/mybackup/ontape/YYYYmmdd, the YYYYmmdd represents today's date and dir which you are creating in your script
          – αғsнιη
          Aug 18 '17 at 19:40






          Yes, it will copy all files in your source path /dbbackup/backupdb/ontape/fullsize/ to destinations paths /mybackup/ontape_tmp_backup/YYYYmmdd and /home/mybackup/ontape/YYYYmmdd, the YYYYmmdd represents today's date and dir which you are creating in your script
          – αғsнιη
          Aug 18 '17 at 19:40






          1




          1




          Thank you so much for you help with this. I really do appreciate it. I am new to scripting but needed to get this done and working and while doing this I realized how much i enjoy scripting and learning it.
          – D.F.
          Aug 18 '17 at 19:56




          Thank you so much for you help with this. I really do appreciate it. I am new to scripting but needed to get this done and working and while doing this I realized how much i enjoy scripting and learning it.
          – D.F.
          Aug 18 '17 at 19:56




          1




          1




          Congratulations; now your password for the remote host is stored in cleartext in your .history for anyone with pysical access to your box to see.
          – DopeGhoti
          Aug 18 '17 at 20:15




          Congratulations; now your password for the remote host is stored in cleartext in your .history for anyone with pysical access to your box to see.
          – DopeGhoti
          Aug 18 '17 at 20:15












          I got this script to run locally however over the weekend when I try to run the cronjob i notice the new directories were created, however the files did not get copied over. Here is the error message I got cat: servers1.txt: No such file or directory, cat: servers.txt: No such file or directory here is what my cron looks like...
          – D.F.
          Aug 21 '17 at 14:10






          I got this script to run locally however over the weekend when I try to run the cronjob i notice the new directories were created, however the files did not get copied over. Here is the error message I got cat: servers1.txt: No such file or directory, cat: servers.txt: No such file or directory here is what my cron looks like...
          – D.F.
          Aug 21 '17 at 14:10




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f387041%2fscp-file-to-new-directory%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          List directoties down one level, excluding some named directories and files

          list processes belonging to a network namespace

          list systemd RuntimeDirectory mounts