Backup

----------

Automated remote backup using ftp commands and cron job

In Linux Operating System cron daemon is used to schedule jobs and commands at regular interval. One can execute any script file containing as a jobs at specified date and time. Following script will use standard Linux FTP commands to place one system’s backup file to other remote system. 

Here, the script is working in a cron job which uploads backup file at every 6 hours, then it will overwrite older backup file. If anyone wants to have backup every hour, he/she has to make changes accordingly in crontab file. 

Below mentioned script name is: daybackup.script.sh

                  ---------------------------

#!/bin/sh

# Put FTP server details here

SERVER="Remote FTP server"

USERNAME="FTP user"

PASSWORD="FTP password"

# local directory containing source backup file

SOURCEFILES="/home/mybackup"

# remote server directory path in which backup needs to be put in

BACKUPDIRCTORY="/rsystem/backupdir/"

# login to remote server

ftp -n -i $SERVER <<EOF

user $USERNAME $PASSWORD

cd $ BACKUPDIRCTORY

mput $ SOURCEFILES/*.tar.gz

quit

EOF

                     ---------------------------------

Make another file using same code given above and give a name to that file as: nightbackup.script.sh as it will put backup to remote system at midnight.

Both the script should have executable permissions, like:

$ chmod +x daybackup.script.sh

$ chmod +x nightbackup.script.sh

Now we have to setup a cron job to run above script at regular intervals.

You need to edit your crontab file. Enter the following command:

$ crontab -e

Following lines should append to available code as there may be a chance to have some another job running in the crontab file.

0 12 * * * daybackup.script.sh >>/tmp/backup.log 2>&1

0 0 * * * nightbackup.script.sh >>/tmp/backup.log 2>&1

Save and close the file.

To store the output of cron commands, we have used /tmp/backup.log file.

Please use crontab –l command to confirm crontab job uses above code or not.

      ----------------------------------------XXXXXXXX---------------------------------------

a quick script to upload file:

#!/bin/shUSERNAME="your-ftp-user-name"PASSWORD="your-ftp-password"SERVER="your-ftp.server.com"   # local directory to pickup *.tar.gz fileFILE="/tmp/backup"   # remote server directory to upload backupBACKUPDIR="/pro/backup/sql"   # login to remote serverftp -n -i $SERVER <<EOF user $USERNAME $PASSWORD cd $BACKUPDIR mput $FILE/*.tar.gz quit EOF

Make sure script has executable permissions:

$ chmod +x /path/to/ftp.backup.script.sh

Setup a cron job to run script at 15:30 (24 hr clock time) times:

30 15 * * * /path/to/ftp.backup.script.sh

Above script should work with all modern ftp client under any Linux / UNIX version.

                 OR

#!/bin/sh USERNAME="user" PASSWORD="password" SERVER="someip" SOURCEDIR="/home/backup" DATE="`date +%Y-%m-%d `" BACKUPDIR="/${DATE}/" cd /home/backup for i in $(find /home/backup -mtime -1 -mmin +59 -type f -name "*.tar.gz*" -exec basename \{} . \;)do/bin/ftp -inv $SERVER >> /tmp/ftp.log <<EOF user $USERNAME $PASSWORD mkdir $BACKUPDIR cd $BACKUPDIR put $i EOF done

__________________XXXXXXXXXXXXXXXXXXX_____________________________________________

Backup and tar directory to a remote FTP server via bash

 backup_ftp.rc

FTP_BACKUP_HOST=

FTP_BACKUP_USERNAME=

FTP_BACKUP_PASSWORD=

-------