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
scripting directory scp
add a comment |
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
scripting directory scp
add a comment |
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
scripting directory scp
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
scripting directory scp
edited Nov 25 at 22:29
Rui F Ribeiro
38.3k1477127
38.3k1477127
asked Aug 18 '17 at 19:05
D.F.
62
62
add a comment |
add a comment |
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
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/YYYYmmddand/home/mybackup/ontape/YYYYmmdd, theYYYYmmddrepresents 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.historyfor 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
|
show 1 more comment
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
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/YYYYmmddand/home/mybackup/ontape/YYYYmmdd, theYYYYmmddrepresents 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.historyfor 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
|
show 1 more comment
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
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/YYYYmmddand/home/mybackup/ontape/YYYYmmdd, theYYYYmmddrepresents 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.historyfor 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
|
show 1 more comment
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
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
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/YYYYmmddand/home/mybackup/ontape/YYYYmmdd, theYYYYmmddrepresents 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.historyfor 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
|
show 1 more comment
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/YYYYmmddand/home/mybackup/ontape/YYYYmmdd, theYYYYmmddrepresents 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.historyfor 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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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