Home > Net >  How to automatically backup a directory from a Linux machine to a Windows computer?
How to automatically backup a directory from a Linux machine to a Windows computer?

Time:01-03

My agenda is to transfer a certain directory from a Linux system to a Windows PC. Every day at 0:00:01, this procedure has to be carried out. Because the password is always changing, it is prohibited to use the password in source codes. The directory that was downloaded needs to be zipped and timestamped.

To copy data from a directory to a backup directory, I tried using a shell script. The script is provided below:

echo "---------------SRCIPT START---------------"

date

TIMESTAMP=`date " %Y%m%d%H%M"`

archive_files ()
{
    mkdir -p $DESTINATION_PATH
    cd $DESTINATION_PATH

    if [ `pwd` = "$DESTINATION_PATH" ]; then
        echo NOW IN `pwd`
        echo STARTING RSYNC FROM $SOURCE_PATH  TO $DESTINATION_PATH
        rsync -avp $SOURCE_PATH $DESTINATION_PATH
    else
        echo %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% FAILED %%%%%%%%%%%%%%%%%%%%%%%%%%%
        exit
    fi
}


#########################################
#       ARCHIVE LOG DIRECTORY           #
#########################################
echo "---------------STARTTING ARCHIVE OF LOG---------------"
date
echo ========================================================

SOURCE_PATH=/LOG
DESTINATION_PATH=BACKUP/$TIMESTAMP$SOURCE_PATH
echo $DESTINATION_PATH
archive_files $SOURCE_PATH $DESTINATION_PATH

##########################################
#        BACKUP FILE COMPRESSION         #
##########################################

cd  /LOG/BACKUP/
tar -cvzf $TIMESTAMP.tar.gz $TIMESTAMP

Please make a suggestion on how to automate directory backups.

Thanks

CodePudding user response:

To automate the script, you can add it as a cron job. On Linux, run crontab -e to edit jobs, then add the job like so:

1 0 * * * sh /the/full/path/to/your/script.sh

This should run your script every day at 00:00:01.

When it comes to the password protection, you could use sshpass to include a password with your rsync command. Since you can't store the passwords in source code, you could store it in a chmod 400 protected password file and use it like so:

sshpass -f '/path/to/password/file' rsync -avp $SOURCE_PATH $DESTINATION_PATH

CodePudding user response:

Apparently, your computers have fixed IP's or domain names, otherwise rsync would not work. For backup, I suggest to install syncthing (https://syncthing.net/) on both computers and to set up a one-way synchronization via the GUI or the web interface.

  • Related