Use rsync to transfer files between Linux servers

We use rsync to transfer files across servers and to assist with a variety of tasks associated with backup and restore. We found this great tutorial over at Nixcraft. Check it out. They do a great job of explaining advanced uses of rsync.

In a nutshell, this is one of the most useful commands we use and thought we would share with you.

[code]
# copy files from server1(local) to server2

rsync -avz /var/www/html/* user@server2.com:/var/www/html/

[/code]

How to backup to Amazon S3

## UPDATE 2017 ##
The method described below was in use 5 years ago. It may still work on your host, but there are more elegant ways to do this now. I keep it here for archive purposes only.
##############

Need to backup your Linux server files and databases to Amazon S3? Here is a great tool – http://s3tools.org/download

Follow the install instructions.

Once you set up your S3 bucket, you can use this bash script to copy files to the S3 bucket:


#!/bin/sh
#
## Lanexa.net - file name = BACKUP_s3.sh
## backup script to push archive files to Amazon S3 bucket
## 2012-01-04
#
# define where your backup files are stored locally on your server
FILEDIR=/BACKUP/files/
DBDIR=/BACKUP/mysql/
#
cd $FILEDIR
#
# use s3cmd to upload files to S3 instance
s3cmd put FILENAME s3://BUCKET-NAME/
#
cd $DBDIR
s3cmd put DATABASES s3://BUCKET-NAME/
#
# email a note to yourself that backup is complete
mail -s "backup files and databases copied to S3" youremail@address.com

How to export your MySQL database using mysqldump

Use your command line to backup your mysql database by exporting it to an .sql file. All you need to complete this task is shell access & a mysql account.

A major use for this command is to easily migrate your sites from one server to another. Since this is often part of a larger process, I like to decide ahead of time which directory will contain this file. So I’ll start by navigating to my desired directory and scoping it out:
[code]user@server:~$ cd /path/to/dir/; ls -al[/code]
Now that you’re sure about using this directory. Here is the basic mysqldump command:
[code]user@server:/path/to/dir$ mysqldump db_name > db_backup.sql[/code]
But, depending on your level of access, you may need to modify it:
[code]user@server:/path/to/dir$ mysqldump -u mysql_user -p db_name > db_backup.sql
Enter password:[/code]
After entering your mysql password, a file called db_backup.sql will be created in the default location, which is your current directory (three cheers for planning ahead!).
Of course, we could have just as easily added a location statement within the command:
[code]user@server:~$ mysqldump -u mysql_user -p db_name > /path/to/dir/db_backup.sql[/code]
But, you’re going to want to cd there anyways, just to verify that everything’s in order 🙂

Be sure to browse our related posts describing how to get migrate files from one server to another, and how to import an .sql file into an empty MySQL databse.