You are here

Efficient Backup Scripting: A Guide to Automated Backup Rotation and Management with FTP in Bash


Efficient Backup Scripting: A Guide to Automated Backup Rotation and Management with FTP in Bash

Welcome to our detailed guide on efficient automated backup scripting, aimed to help you manage and safeguard your valuable data. In today’s era, where data is a pivotal asset, having a reliable backup strategy is paramount. By leveraging FTP in Bash scripting, this guide provides a seamless approach to data management and backup rotation.

Understanding the Script:

The script demonstrated here is a refined approach to automated backups, where we utilize FTP to transmit files to the backup server from the hosting server. This strategy employs a Bash script, allowing for straightforward setup and execution. This script is especially useful for those looking to implement efficient data management procedures with minimal hassle.

Setting up the Environment:

Before diving into the script, it's crucial to ensure that the requisite folders, ‘tmp’ and ‘backup’, are created in your home folder, and the ‘backup_test’ folder is present on the remote FTP server. Proper setup is a stepping stone to executing smooth automated backups and maintaining data integrity.

Script Implementation:

Here’s a glimpse of the script’s syntax and parameters:

#!/bin/bash
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# Define user home path; it can be found by connecting and executing the pwd command.
HOME=/home/sanglyb
# Define backup name
FILE=site_ru_$(date +"%Y-%m-%d_%H:%M").tgz
# Define old backup to be removed from the server. Assumes a 30-day rotation, so the filename will have a date 30 days earlier than the current date.
OLDFILE=site_ru_$(date -d 'now -30 days' +"%Y-%m-%d")*
# $HOME/web - the folder containing your site's files.
tar czf $HOME/tmp/files.tgz $HOME/web
# Connect to MySQL, using your credentials.
mysqldump -h 127.0.0.1 -u sanglyb_sanglyb -ppassw0rd sanglyb_test > $HOME/tmp/mysql.sql
tar czf $HOME/backup/$FILE $HOME/tmp/*
cd $HOME/backup
rm -rf $HOME/tmp/*
# Define FTP connection parameters.
HOST='mytechnote.ru'
USER='[email protected]'
PASSWD='passw0rd'
# Use passive mode for the connection. If active mode is required, remove the -p flag here and below.
ftp -np $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
binary
cd backup_test
put $FILE
quit
END_SCRIPT
# Calculate the number of files in the FTP server directory.
CONTENT="$(ftp -inp $HOST <<HERE
user $USER $PASSWD
cd backup_test
ls
bye
HERE
)"
NUMBER=`echo "$CONTENT" | egrep -v "user|ls|^d|bye" | wc -l`
NUMBER=$((NUMBER-1))
# If there are more than 30 files, remove the oldest one.
if [ $NUMBER -gt 30 ]
then
ftp -np $HOST <<END_SCRIPT1
quote USER $USER
quote PASS $PASSWD
binary
prompt
cd backup_test
mdelete $OLDFILE
quit
END_SCRIPT1
fi
# Remove the local backup copy.
rm -rf $HOME/backup/$FILE
exit 0

This backup script is not just a set of commands but a comprehensive solution for those seeking automated and efficient data management. While the script is designed to be user-friendly and robust, it’s essential to follow each step meticulously and understand each parameter to avoid undesired outcomes.

Noteworthy Features:

  • Simple Backup Rotation: The script employs a simple rotation mechanism, ensuring your backups are always up to date without unnecessary clutter.
  • Automated Execution: By adding this script to cron, you can automate the backup process, relieving you from manual intervention and guaranteeing regular backups.

Conclusion:

Whether you’re a seasoned developer or a beginner exploring the realms of data management and Bash scripting, this guide offers a versatile solution to automated backup and rotation using FTP. Dive deep into the script, understand its intricacies, and embrace the convenience of automated backups!

We hope this guide on backup scripting and data management proves helpful. For more insights and guides on Bash scripting and data safety, stay tuned to our blog. Feel free to share your thoughts, experiences, or any queries in the comments below or join our forum discussion.

Found this guide useful? Share it with your peers and let us know your thoughts or any additional tips you might have on efficient data management and backup scripting!

0 0

Share the article with your friends in social networks, maybe it will be useful to them.


If the article helped you, you can >>thank the author<<