Last updated on March 22nd, 2018 at 08:27 am
How to Backup Files to a Remote FTP Server With Bash Script
After reading our old article titled “How to Backup Files and Directories in Linux Using Tar & Cron Jobs”, many readers were asking How to Backup Files to a Remote FTP Server with bash script, so i am writing this quick guide in response to our readers queries.
Some Queries from Readers:
1: If i want to transfer my Linux system backups to an external server, how i can do it?
2: sir please write a command to transfer file from one system to another.
Alright..! Definitely its sounds interesting, when a simple bash script and cronjob will do all FTP login process, and safely upload local files to an external ftp server automatically while you are away from your desk. Right ?.. So here is a quick guide on transferring Linux Backup to an external ftp server using bash script and cronjob.
FTP Transfer Bash Script:
Issue vi /script-ftp.sh command to create script file and paste below contents.
#!/bin/bash # 1 HOST=172.20.10.8 # 2 USER=broexperts # 3 PASSWORD=redhat # 4 ftp -inv $HOST <<EOF # 5 user $USER $PASSWORD # 6 lcd /mybackupfolder # 7 mput *.tar.gz # 8 bye # 9 EOF
Script Explanation line by line.
1- Specifying bash as our shell.
2- Assigning FTP Server IP/Hostname to HOST variable.
3- Assigning valid User on FTP Server to USER variable.
4- Assigning Password to PASSWORD Variable.
5- Connecting with FTP Server by issuing FTP command.
6- Providing credentials to FTP connection.
7- Navigating to our source directory containing backup Files.
8- Upload all files having extension .tar.gz to broexperts’s user home directory.
9- Ending the FTP session.
Now save this file and set appropriate permission on script file by issuing below command.
chmod 700 /script-ftp.sh
This script will first navigate to our mentioned backup folder (/mybackupfolder) and copy all files with .tar.gz extension to the mentioned ftp server, all files will be saved in broexperts user home directory /home/broexperts. To automate this process we need to add this script in cronjob
Open cronab editor utility:
Suppose i want to run this backup process on every Mon and Sat at 12:pm. In this condition my crontab file will be like this:
# M H DOM M DOW CMND 01 12 * * 1,6 /bin/bash /script-ftp.sh
This Script will run at 12:01:00 at every Monday and Saturday.
leave comments if you need any help or contact me directly on [email protected].
You might be interested in other articles on Linux Backup
- Incremental Backup in Linux Using tar utility
- Guide to Setup Crontab with Example
- Email Notification Upon Success or Failure of Backup Script
- Delete Files Older than X Number of Days in Linux