skip to main | skip to sidebar
Showing posts with label backup. Show all posts
Showing posts with label backup. Show all posts

Tuesday, December 12, 2006

Using Rsync for Remote Backup

I've been using Rsync for a while now for keeping a backup of my stuff on a remote server. Rsync is an open source program for fast incremental file transfer. It can be used to synchronize two remote directories and to synchronize a local folder with a remote folder (which is what I use it for). This also allows me to work on different machines on the same stuff: I download (or update) the stuff from the remote server, work on it, and the commit the changes back on the server.

Notice that rsync can only synchronize in one way (a source with a target, or the other way round, but not in both directions simulataneously); if you need a two way file synchronizer you might want to take a look at Unison.

Anyway, this is how I update my local version of a directory with the version on the server:

rsync -e ssh -avzu --delete -c -C \
--exclude-from=exclude-files \
USER@REMOTEMACHINE:REMOTEDIR LOCALDIR
where
  • -e ssh instructs rsync to use ssh as a remote shell (thus authentication and transfer will use ssh)
  • -a is archive mode (refer to documentation): basically recurs into directories and keeps permissions
  • -v increments verbosity
  • -z compresses the transferred data (very useful when using slow connections)
  • -u skips files that are newer on the receiver
  • --delete deletes files that are in the local folder but not in the remote folder
  • -c uses checksums to determine which files need to be synchronized
  • -C auto-ignores files in the same way CVS does
  • --exclude-from excludes all the files (using also regular expressions) specified in the file exclude-files.
  • USER is my user name on the REMOTEMACHINE
  • REMOTEDIR is the complete path of the folder on the remote machine and LOCALDIR is the path of the local folder (IMPORTANT: do not append the final slash on these paths otherwise the semantics changes, see the documentation)
Another useful option is -n or --dry-run that only simulates the transfer (so that you can see which files would be transferred).

And exclude files contains these regular expressions:
*~
*.dvi
*.aux
*.log
*.bbl
*.blg
*.toc
*.bak
*.idx
*.ilg
*.ind
*.class
*.out
core
But of course you can modify it as you see fit.

When I want to commit my local changes to the remote server I basically run the above command inverting source and destination:
rsync -e ssh -avzu --delete -c -C \
--exclude-from=exclude-files \
LOCALDIR USER@REMOTEMACHINE:REMOTEDIR
This is only one use of rsync. I refer to the documentation for more advanced usage. Hope you enjoy rsync as much as I do :-)

Pubblicato da betto a 1:24 PM 0 commenti

Etichette: ,

Wednesday, November 29, 2006

Cron and MySql Database Backups

This is nothing very special or exciting, it's just a script that I run as a cron job to make the backup of a mysql database. It can be customized according to several parameters. The script is intended to be run once a day, in fact the file name that is created contains the date in the shape of month_day_year. If you need to run this script more than once per day, you need to add also the time in the file name (otherwise the previous backup in the same day gets overwritten).

You can also customize the parameters that you pass to the programs, e.g., mysqldump and gzip. And of course, you need to set the right values for the username, the password and the database name (and also the path where the backup will be save, which, by default it's the backup directory in your home directory, so please make sure this directory exists).

Here's the script, hoping you find it useful somehow :-)

#!/bin/sh

DATABASE=bibliography
USER=root
PASSWORD=pippo
FILENAME=backup_
PATH=$HOME/backup
MYSQLDUMP=/usr/bin/mysqldump
GZIP="/bin/gzip --best"

BACKUP=$PATH/$FILENAME`/bin/date +%m_%d_%y`.sql.gz

if $MYSQLDUMP -u $USER -p$PASSWORD $DATABASE | $GZIP > $BACKUP; then
echo "backup successful: $BACKUP";
true;
else
echo "errors during backup";
false;
fi

Pubblicato da betto a 4:56 PM 0 commenti

Etichette: , ,

Subscribe to: Posts (Atom)
 

AltStyle によって変換されたページ (->オリジナル) /