With the advent of the cloud computing, storage has become extremely cheap. We can use this storage to keep backups of our data and keep them safe from any potential data loss disaster. One such extremely cost-effective solution is Amazon S3. Some of the pros of S3 are:
- The storage costs at S3 are very low
- You have virtually unlimited storage capacity
- You only pay for how much you use
In this “how to” we will see how to use Amazon S3 to backup your server data.
Pre-requisites:
- Linux Server with root access (Assumed Debian based)
- An Amazon S3 account i.e. S3 access key and S3 secret key (Sign Up at http://aws.amazon.com/s3/ to get it)
Applications/Softwares Used:
- FUSE Libraries: Filesystem in User Space
- S3FS: Filesystem based on Amazon S3
- Rsync: Remote data synchronization tool
STEPS:
- Login to your server as root using SSH and install the pre-requisites and dependencies by keying the following command:
apt-get install build-essential libcurl4-openssl-dev libxml2-dev libfuse-dev comerr-dev libfuse2 libidn11-dev libkdb5-4 libkrb5-dev libldap2-dev libselinux1-dev libsepol1-dev pkg-config fuse-utils sshfs rsync mailutils
- Get a copy of the latest version of S3FS Source, extract it and install it:
wget -c http://s3fs.googlecode.com/files/s3fs-r191-source.tar.gz
tar xvf s3fs-r191-source.tar.gz
cd s3fs
make&&make install
- To allow non-root users to the fuse drives open /etc/fuse.conf in your favorite editor, find and un-comment user_allow_other
- Create a bucket for your backups on S3 using the S3 console. In this case we created “server-backup-bucket” as our backup bucket.
- Create a mount point for our backups and mount our S3 drive:
mkdir -p /mnt/backups/
/usr/bin/s3fs -o allow_other server-backup-bucket -o accessKeyId=XYZ -o secretAccessKey=ABC /mnt/backups/
Now, the backup directory is mounted to /mnt/backups/. - Create a shell script inside /opt/backup_scripts/ named mount-s3-backups which uses the above mount method:
#!/bin/bash
IS_MOUNTED=`df | grep -c '/mnt/backups'` # Check if the drive is already mounted
if [ "$IS_MOUNTED" -eq "0" ]; then
/usr/bin/s3fs -o allow_other server-backup-bucketname -o accessKeyId=XYZ -o secretAccessKey=ABC /mnt/backups/
echo "S3 Backup Drive mounted successfully!"
else
echo "S3 Backup Drive already mounted!"
fi
Make this script executable:
chmod +x /opt/backup_scripts/mount-s3-backups
- Setup a shell script in /opt/backup_scripts/s3-home-backup.sh. In this case I assume we are going to backup the /home/ directory:
#!/bin/bash
modprobe fuse # Loads the Fuse Kernel module
/opt/backup_scripts/mount-s3-backups # Mount S3 Backup directory if not mounted
IS_MOUNTED=`df | grep -c '/mnt/backups'` # Check if the mount was successful
if [ "$IS_MOUNTED" -eq "1" ]; then
TIME_STARTED=`date`
echo "Started Backup..."
echo "Backing up to S3 Drive" > /opt/backup_scripts/tmp.log
echo "START TIME: $TIME_STARTED" >> /opt/backup_scripts/tmp.log
echo "-----------------------------------------" >> /opt/backup_scripts/tmp.log
/usr/bin/rsync -avz --delete /home/ /mnt/backups/home/ >> /opt/backup_scripts/tmp.log # Rsync the /home directory to /mnt/backups/home --delete will ensure deleted file in the filesystem are deleted on the backup.
TIME_ENDED=`date`
echo "-----------------------------------------" >> /opt/backup_scripts/tmp.log
echo "END TIME: $TIME_ENDED" >> /opt/backup_scripts/tmp.log
echo "-----------------------------------------" >> /opt/backup_scripts/tmp.log
mail logs@your-domain.com -s "[S3 Backups] Daily Home Backup Complete" < /opt/backup_scripts/tmp.log # Sends the logs of the backup to your email
umount /mnt/backups # Unmount the Backups Directory
echo "Backup complete!"
else
echo "S3Drive not Mounted" > /opt/backup_scripts/tmp.log
mail logs@your-domain.com -s "[ERROR] S3Drive not Mounted!" < /opt/backup_scripts/tmp.log
fi
We are almost there. Now lets make the /opt/backup_scripts/s3-home-backup.sh executable and run it:
chmod +x /opt/backup_scripts/s3-home-backup.sh
/opt/backup_scripts/s3-home-backup.sh
If everything is OK, the script should run successfully and a mail should be delivered to [email protected] with the logs of the backup. - For the last step lets add the script to crontab, open crontab in your favorite editor by running crontab -e and insert the following code:
@daily /opt/backup_scripts/s3-home-backup.sh
The backups interval can be configured by changing the cron interval.
That’s it.. Eight simple steps to securely backup your server data on Amazon S3.
P.S. : Data Backup is a critical operation. Any script that is written must be thoroughly tested before running over the actual data to prevent data loss.
Pingback: Tweets that mention [How To] Back up your Server Data to Amazon S3 | TechTatva -- Topsy.com