One of the dedicated customer wanted to backup his web site to Amazon S3. To upload files to Amazon S3, i installed awscli with
pip install awscli
and configured it with
aws configure
Now to take backup, i created following shell script
mkdir /usr/hostonnet touch /usr/hostonnet/backup.sh chmod 755 /usr/hostonnet/backup.sh
Edit the file
vi /usr/hostonnet/backup.sh
Add following to the file
#!/bin/bash # https://blog.hostonnet.com/backup-amazon-s3 # 2 12 * * * /usr/hostonnet/backup.sh >> /var/log/s3-backup.log BACKUP_TIME=`date +%Y-%m-%d-%H-%I-%S` echo "Backup starting at $BACKUP_TIME" # Check for directory if [ ! -d /home/backup/ ]; then mkdir /home/backup/ fi mysqldump hostonnet > /home/backup/mysql-db-$BACKUP_TIME.sql # Generate tar file. tar -cpzf /home/backup/hostonnet-$BACKUP_TIME.tgz /home/hostonnet/public_html /home/backup/mysql-db-$BACKUP_TIME.sql rm -f /home/backup/mysql-db-$BACKUP_TIME.sql export AWS_CONFIG_FILE=/root/.aws/config /usr/local/bin/aws s3 cp /home/backup/hostonnet-$BACKUP_TIME.tgz s3://hostonnet-bak # Delete backup from local HDD after 12 days. find /home/backup/ -maxdepth 1 -type d -mtime +12 -exec rm -rf {} \; echo "=====================================================" echo " "
s3://hostonnet-bak is the Amazon S3 bucket name. You need to create it.
Now set cronjob
2 12 * * * /usr/hostonnet/backup.sh >> /var/log/s3-backup.log
Adjust time as needed. Here it is set to run 12:02 hours everyday.