In this article we introduce a shell script that uses the rsync command to back up files/directories on your local Linux machine to a remote Linux server. Using this script will implement the backup interactively, and you will need to provide the host name/IP address and folder location of the remote backup server. We use a separate list file in which you need to list the files/directories to be backed up. We have added two scripts, and the first script asks for the password after each copy of the file (if you enable SSH key verification, then the password is not asked), and in the second script, you will only be prompted to enter the password at a time.
We intend to back up bckup.txt,dataconfig.txt,docs and orcledb.
[Email protected] tmp]# ls-l
Total 12
-rw-r--r--. 1 root root 0 may 10:43 bckrsync.sh
-rw-r--r--. 1 root root 0 may 10:44 Bckup.txt
-rw-r--r--. 1 root root 0 may 10:46 Dataconfig.txt
Drwxr-xr-x. 2 root root 4096 may 10:45 docs
Drwxr-xr-x. 2 root root 4096 may 10:44 OracleDB
The Bckup.txt file contains the details of the files/directories that need to be backed up
[Email protected] tmp]# Cat/tmp/bckup.txt
/tmp/oracledb
/tmp/dataconfig.txt
/tmp/docs
[Email protected] tmp]#
Script 1:
#!/bin/bash
# Save the path of the backup list file to a variable
backupf= '/tmp/bckup.txt '
# Enter a hint message
echo "Shell Script Backup Your files/directories Using rsync"
# Check if the target server is entered and prompt the user for input if empty
while [x$desthost = "x"]; Do
# Prompt user to enter destination server address and save to variable
Read-p "Destination backup Server:" Desthost
# End Loop
Done
# Check if the destination folder is entered, and if it is empty, prompt the user again to enter
while [X$destpath = "x"]; Do
# Prompt user to enter target folder and save to variable
Read-p "Destination Folder:" DestPath
# End Loop
Done
# Read the backup list file row by line
For line in ' Cat $BACKUPF '
# work on every line
Do
# Displays the file/folder name to be copied
echo "Copying $line ... "
# Copy files/folders to target location via rsync
Rsync-ar "$line" "$desthost": "$destpath"
# show Complete
echo "Done"
# End
Done
Run a script with output results
[R[email protected] tmp]#./bckrsync.sh
Shell Script Backup Your files/directories Using rsync
Destination Backup server:104.*.*.41
Destination Folder:/tmp
Copying/tmp/oracledb ...
The authenticity of host ' 104.*.*.41 (104.*.*.41) ' can ' t be established.
ECDSA key fingerprint is 96:11:61:17:7F:FA: ...
Is you sure want to continue connecting (yes/no)? Yes
warning:permanently added ' 104.*.*.41 ' (ECDSA) to the list of known hosts.
[Email protected]*.*.41 ' s password:
Done
Copying/tmp/dataconfig.txt ...
[Email protected]*.*.41 ' s password:
Done
Copying/tmp/docs ...
[Email protected]*.*.41 ' s password:
Done
[Email protected] tmp]#
Script 2:
#!/bin/bash
# Save the path of the backup list file to a variable
backupf= '/tmp/bckup.txt '
# Enter a hint message
echo "Shell Script Backup Your files/directories Using rsync"
# Check if the target server is entered and prompt the user for input if empty
while [x$desthost = "x"]; Do
# Prompt user to enter destination server address and save to variable
Read-p "Destination backup Server:" Desthost
# End Loop
Done
# Check if the destination folder is entered, and if it is empty, prompt the user again to enter
while [X$destpath = "x"]; Do
# Prompt user to enter target folder and save to variable
Read-p "Destination Folder:" DestPath
# End Loop
Done
# Check if the target server password is entered, and if it is empty, prompt the user to enter
while [X$password = "x"]; Do
# Prompt user to enter password and save to variable
# using the-S option does not echo the entered password
Read-sp "Password:" Password
# End Loop
Done
# Read the backup list file row by line
For line in ' Cat $BACKUPF '
# work on every line
Do
# Displays the file/folder name to be copied
echo "Copying $line ... "
# Use expect to enter the password in the script
/usr/bin/expect << EOD
# Recommended setting timeout is-1
Set Timeout-1
# Copy files/folders to target location via rsync, use expect components spawn command
Spawn Rsync-ar ${line} ${desthost}:${destpath}
# The previous line command waits for "password" prompt
Expect "*?assword:*"
# provide the password in the script
Send "${password}\r"
# Wait for the file terminator (the remote server has finished processing everything)
Expect EOF
# End Expect script
EOD
# Show End
echo "Done"
# complete
Done
Hopefully these scripts will help you with your backup!!
Free pick up Brother Lian IT Education Original Linux Operations Engineer video/Detailed Linux tutorials, details of the website customer service: http://www.lampbrother.net/linux/
or hooking up with Q2430675018.
Welcome to the Linux Communication Group 478068715
Shell script: Use rsync to back up files/directories