How to backup using rsync to local and remote server

Simple check before rsync

# Remove all files and directories
rm -rf ./*

# Count total 
find html | wc -l

# Count total file only
find . -type f | wc -l

# Dry run
rsync --stats --dry-run -ax /source /destination

# Count total directories and files
tree html | tail -l

# check disk usage
sudo du -sh .

Give permission rwx to the group

chmod g+rwx /directory -R
sudo chown -R www-data:www-data /var/www/html/

How to backup using rsync to local server

sudo rsync -avh --delete --progress /source/ /destination/
sudo rsync -avh --delete --stats --progress /source/ /destination/

-v verbose
-h human-readable format
-z is to enable compression
-a archive mode 
(recursive, preserves symbolic links, permissions, timestamsp, owners and groups)

–delete delete any files in /destination/ that are not present in /source/
–stats file-transfer stats
–progress show progress during transfer

I personally like below for showing status

sudo rsync -avh --delete --stats --progress /source/ /destination/

Change the ownership of your destination folder to your liking after completing rsync backup

sudo chown nobody.nogroup /srv/samba/Monday/

 

How to backup to remote server

sudo rsync -av --delete -e ssh /local/directory/ kim@192.168.xxx.xxx:/remote/directory/

If your ssh is running on another port (eg 443)

sudo rsync -av --delete -e 'ssh -p 443' /local/Directory/ kim@192.168.x.x:/remote/directory/

Backup using private key instead of password

sudo rsync -avh --delete --stats --progress -e "ssh -i /path/to/private/key" /source/folder kim@hostname:/destination/

Automate your backup with crontab -e

0 23 * * * rsync -av --delete /source/ /destination/

 

Backup a single large file to remote server

rsync -avz --stats --progress /source/file.zip root@<remote-ip>:/remote/directory/

Leave a Comment

Your email address will not be published.