Backup a File to a Remote Server
A bash script is one method to backup files to a remote server. An account is needed with a SSH key pair and a file needs to be created at ~/.ssh/config so the script can automatically login to the remote server. Also, you can set up a CRON job to run the script nightly. Be sure to not add a passphrase to the private key. Setting up SSH authentication is beyond the scope of this article.
The primary command in the script is the ‘scp’ command. SCP is good if you need a copy a single file at a time. The scp command uses SSH underneath the hood. As such, it will automatically check for a a ~/.ssh/config file and use it to authenticate to the remote server.
If you need to backup multiple files, modify the script and add additional variables, updates checks, and scp commands. If you need to transfer entire directories use the rsync command. However, rsync can sometime have issues using an identity file. Rsync like SCP should automatically use the ~/.ssh/conf file by default.
Lets set up the head of the script and call bash and add any comments.
#!/bin/bash
# backup.sh
Next, let’s set up the script variables.
DATE=`date`
DAY=$(date -d "1 day ago" +%d)
MONTH=$(date -d "1 day ago" +%b)
YEAR=$(date -d "1 day ago" +%Y)
SRC_DIR="/var/logs/$YEAR/$MONTH"
DST_DIR="/user1/backup/$YEAR/$MONTH"
FILE="filename-$DAY.log"
Next, lets test if the file path exists!
# test if path exist before trying to copy file
# -e=test for file,-d=test for file & if file exists is it a dir
# Remote server hostname is 'backup'
TEST=`ssh backup.company.com "test -e $DST_DIR && echo 1 || echo 0"`
if [ $TEST = 0 ] ; then
echo "$DATE : $DST_DIR was not found. Making ..."
# -p=make all dir in path, if they do not exist
ssh backup.company.com "mkdir -p $DST_DIR"
else
echo "$DATE : $DST_DIR was found."
fi
Finally, we will copy the file to the remote server using scp or rsync.
# scp & rync will automatically use the SSH config file at /home/<user1>/.ssh/config
scp -p $SRC_DIR/$FILE user1@server1:$DST_DIR
#-p=preserve permissions
#rsync -a $SRC_DIR user1@server1:$DST_DIR
#-a = preserve permissions & timestamps
Ref. https://unix.stackexchange.com/questions/127352/specify-identity-file-id-rsa-with-rsync