0

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
1

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/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

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.