Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Agenda #
- : '
- - Basic SSH usage
- - Basic Rsync CLI usage
- - Basic usage of Rsync with cron
- - Using SSHFS to mount remote folders
- - Taste of advanced Rsync script (if there is time)
- '
- ## Example objects used ##
- : '
- For the purpose of this session, the following example objects will be used:
- User: artur
- Host: myserver
- Source dirs: /home/artur/documents, /home/artur/pictures, /home/artur/videos
- Destination: /mnt/backup
- '
- # Basic SSH usage #
- : '
- Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. SSH can be used for many things, including:
- - For login to a shell on a remote host
- - For setting up automatic (passwordless) login to a remote host
- - To backup and copy files efficiently and securely using SCP, SFTP or Rsync
- - Plus many other things
- SSH operates with a keypair of a private and public key, so you can login remotely without using password (you can check the .ssh folder in your homedir to see if you already have a keypair with: ls ~/.ssh)
- '
- : 'Creating an SSH keypair'
- ssh-keygen
- : 'For me, this would create the files id_rsa (private key) and id_rsa.pub (public key) in the folder /home/artur/.ssh'
- : 'Copy the public key to the remote host (may require password login)'
- ssh-copy-id artur@myserver
- : 'After this, you can login to a remote shell (if SSH server is running on the remote host) without a password using'
- ssh artur@myserver
- : '
- You can also add the public key manually to the file /home/artur/.ssh/authorized_keys on the remote host
- !!! NEVER give out your PRIVATE key !!! (the file only named id_rsa, and the largest of the 2 files)
- This post explain some more details:
- https://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login/46935
- '
- # Basic Rsync CLI usage #
- : 'General usage'
- rsync [Options] [Source(s)] [Destination]
- : 'Syncing locally (ignore the options for now)'
- rsync -zav /home/artur/documents /mnt/backup
- : 'Syncing from local to remote'
- rsync -zav /home/artur/documents artur@myserver:/mnt/backup
- : 'Syncing from remote to local'
- rsync -zav artur@myserver:/home/artur/documents /mnt/backup
- : '
- The username is the user to log in to the remote host with SSH. Typically, this would be the normal user (in this case artur) or root.
- The hostname of myserver can be either an IP address or a FQDN (fully qualified domain name). For instance, if I had local DNS, and my domain name was arturmeinild.net, then a FQDN could be: myserver.arturmeinild.net
- Adding a public SSH key to the remote host is the preferred way to login - alternatively a password file can be used.
- '
- ## Useful CLI options (inspired by Grsync) ##
- : '
- -t, --times preserve modification times
- -p, --perms preserve permissions
- -o, --owner preserve owner
- -g, --group preserve group
- --delete delete extraneous files from destination dir
- -x, --one-file-system do not cross filesystem boundaries
- -v, --verbose increase verbosity
- --progress show progress during transfer
- --ignore-existing skip updating files that exist on receiver
- --size-only skip files that match in size
- -u, --update skip files that are newer on receiver
- --modify-window=1 compare mod-times with reduced accuracy (Windows compatibility)
- -r, --recursive recurse into directories
- -l, --links copy symlinks as symlinks (preserve)
- -n, --dry-run perform a trial run with no changes made
- -z, --compress compress file data during transfer
- -a, --archive archive mode; equals -rlptgoD (-D are more special filetypes)
- --password-file=FILE read remote-access password from FILE
- '
- # Basic usage of Rsync with cron #
- : '
- There are generally 3 different ways to run a command or script with cron:
- - Add a cronjob definition in crontab
- - Add a cronjob definition file in /etc/cron.d/
- - Put a script file directly in /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/ or /etc/cron.monthly/
- Each method can have advantages for certain usecases, or sometimes it just comes down to personal preference.
- In the following examples we will add cronjob definitions under /etc/cron.d/, which have the following format:
- '
- [Minute] [Hour] [Day of month] [Month] [Day of week] [User] [Command]
- : '
- The time and date fields are: (first 5 fields)
- Field Allowed values
- ----- --------------
- Minute 0-59
- Hour 0-23
- Day of month 1-31
- Month 1-12
- Day of week 0-7 (0 or 7 is Sunday)
- In addition, for all fields * is wildcard meaning any value.
- '
- ## Example cronjob definitions ##
- : 'For a cronjob that runs rsync for a single folder each day at 22:30, this would be the cronjob definition to put in a file under /etc/cron.d/ (create a text file with any name you like - and notice the PATH must be included in the job file as well)'
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- 30 22 * * * artur rsync -zav /home/artur/documents /mnt/backup
- : 'You can also chain more commands with a semicolon to several destinations'
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- 30 22 * * * artur rsync -zav /home/artur/documents /mnt/backup; rsync -zav /home/artur/documents artur@myserver:/mnt/backup
- ## Cron exercise ##
- : 'Edit a new file in /etc/cron.d/ as root'
- sudo nano /etc/cron.d/crontest
- : 'Add the following lines, where you replace [Minute] and [Hour] so the script runs 15 minutes from now in your Timezone, and [User] with your username'
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- [Minute] [Hour] * * * [User] echo "Hello" > /home/[User]/hello.txt
- : 'Save the file - and let us see if the file hello.txt is created in about 15 minutes!'
- ## A simple Rsync script ##
- : 'However, if you have several files and folders or need to sync to multiple destinations, it might be better to write a simple bash script. I could create a bash script in my homedir: /home/artur/rsyncbackup.sh (remember to make it executable with: chmod +x ~/rsyncbackup.sh)'
- #!/bin/bash
- rsync -zav /home/artur/{documents,pictures,videos} /mnt/backup
- rsync -zav /home/artur/{documents,pictures,videos} artur@myserver:/mnt/backup
- : 'And then this script is added to the cronjob file instead'
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- 30 22 * * * artur /home/artur/rsyncbackup.sh
- : '
- A nice tool to help with cron expressions is grontab.guru:
- https://crontab.guru/
- '
- # Using SSHFS to mount remote folders #
- : 'Installing SSHFS'
- sudo apt install sshfs
- : 'Mounting remote folder (folder /mnt/backup/remote must exist and be empty)'
- sshfs artur@myserver:/mnt/backup /mnt/backup/remote
- : 'Now the remote folder /mnt/backup on myserver is available in /mnt/backup/remote on the local machine. Now we can use a local rsync command to actually perform a remote sync'
- rsync -zav /home/artur/{documents,pictures,videos} /mnt/backup/remote
- : '
- One important aspect of SSHFS is that it cannot cross between users - so if you mount a folder with the user artur, only this user can access the contents, regardless of read permissions.
- '
- # Taste of advanced Rsync script #
- : '
- I am currently writing a script that reads some simple configuration files for syncing specific files and folders to multiple locations - both locally and remote.
- The script works with 2 different types of files: Homefiles and Rootfiles.
- - Homefiles are files or directories in my homedir that must be synced.
- - Rootfiles are various config files and directories owned by root that must be synced.
- The file format is like this:
- '
- # Homefiles for Rsync Backup
- ::LOCAL-ONLY::
- .gnupg
- .ssh
- .tmux.conf
- ::REMOTE-ONLY::
- ::LOCAL+REMOTE::
- .config
- .bash_aliases
- .bashrc
- bash
- ::END::
- : '
- As you can see, the config file syntax directs the files into groups, depending on whether they should be synced only locally, only remotely, or both.
- .gnupg, .ssh, .config and bash are directories
- .tmux.conf, .bash_aliases and .bashrc are files
- The script reads another config file that defines:
- - Destination paths for homefiles and rootfiles when the category is ::LOCAL::
- - A list of remote hosts to connect to when the category is ::REMOTE::
- The script then has the following functionality:
- - Checks if the remote hosts are online
- - Reads the definition file for first homefiles then rootfiles, and for each line executes the relevant rsync command to:
- * Sync locally
- * Sync remotely (logging in as either normal user or root)
- * Sync both locally and remotely
- - Logging nicely formatted output while doing so (log function not finished yet)
- I run this script under /etc/cron.daily - so I get automatic backup and sync of important config files
- This is just to give an idea about what you can do with a more customized use of Rsync.
- '
Add Comment
Please, Sign In to add comment