Currently I follow the following steps for copying a file from server_x to server_y (Let's suppose I run this code on 20th April 2021 = 202104)
sshpass -p "my_server_y_password" ssh myusernameY@server_y
sshpass -p "my_server_x_password" scp myusernameX@server_x:server_x/file/path/file_202104.txt server_y/file/path
I want to get this code to run on 20th of every month and also use the YYYYMM date format for searching the file. If the file does not exist, it shouldn't do anything
I think that the code should look like (inside my_bash_script.sh)
year_month="$(date +'%Y%m')"
FILE=file/path/file_$(year_month).txt
# somehow check if file from server_x exists (currently I don't know how to do it on a different server)
if test -f "$FILE"; then
sshpass -p my_server_y_password ssh myusernameY@server_y
sshpass -p "my_server_x_password" scp myusernameX@server_x:${FILE} server_y/file/path
fi
And, after this, I should run my script
* * 20 * * my_bash_script.sh
I am sure there are much more elegant solutions to approach my problem. Also, I think some parts of my code don't even work properly. Please help me finding a proper solution for my task
Edit reasons: I replaced my former code with crontab command as suggested by @looppool