https://github.com/takuya/python_rclone_backup
python_rclone_backup
https://github.com/takuya/python_rclone_backup
Last synced: 8 months ago
JSON representation
python_rclone_backup
- Host: GitHub
- URL: https://github.com/takuya/python_rclone_backup
- Owner: takuya
- License: agpl-3.0
- Created: 2022-02-26T13:23:35.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-04-18T03:49:07.000Z (about 4 years ago)
- Last Synced: 2025-02-09T01:13:53.096Z (over 1 year ago)
- Language: Python
- Size: 19.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# python_rclone_backup
python_rclone_backup
## install
sample, install by pip from github.
```shell
pip install git+https://github.com/takuya/python_rclone_backup.git#egg=rclone_backup
```
using pyenv and pipenv
```shell
pyenv local 3.9.8
pyenv exec pipenv install --python=3.9.8
pyenv exec pipenv install git+https://github.com/takuya/python_rclone_backup.git#egg=rclone_backup
pyenv exec pipenv install
```
## for rclone backup
- pre/post scripts
- Archiving
- pipe
- config
We know `rclone` is great. but backup come with messy procedure.
This will make archiving more simple. add pipe, with `post backup` / `pre backup` commands to maintain.
### pre/post scripts
for achive , pre/post scripts.
### Archiving and Pipe
tar for archive , adding `archive` option alternatively to use `rclone sync`.
```python
['./backup', 'my-remote:/archive-test.tgz', 'archive'],
```
pipe for archive , adding `pipe` option alternatively to use `rclone sync`.
```python
['mysqldump | gzip -c - ', 'my-remote:/mysqldump.sql.gz', 'pipe'],
```
### backup config
config using dict / list, for reducing shell script.
### config example
```python
import os
import sys
import rclone_backup
project_root = os.path.realpath(os.path.dirname(os.path.realpath(__file__)) + "/..")
sys.path.insert(0, project_root)
from rclone_backup.Backup import Backup
def main():
config = {
'pre_backup': {
"stop service": "sudo systemctl stop xxx.service"
},
'backup_pair': [
['./backup/Backup.py', 'my-remote:/', 'copy'],
['./backup', 'my-remote:/archive-test.tgz', 'archive'],
['apt list --installed | gzip -c - ', 'my-remote:/apt-list.gz', 'pipe'],
],
'post_backup': {
"start service": "sudo systemctl start xxx.service"
},
}
rclone_config = "~/.rclone.conf"
rclone_opts = [
'--links',
'-q',
]
bk = Backup(rclone_config, rclone_opts, dry_run=(os.environ.get('dry_run') or False))
bk.add_backup_list(config['backup_pair'])
bk.add_pre_backup_cmd(config["pre_backup"])
bk.add_post_backup_cmd(config["post_backup"])
bk.start(verbose=(os.environ.get('verbose') or False))
if __name__ == '__main__':
main()
main()
```
This will execute these shell commands.
```shell
sudo systemctl stop xxx.service
sudo tar cvzf /var/www www.tgz
rclone --config ~/.rclone.conf --links copy ./backup/Backup.py my-remote:/Backup.py
tar czf - ./backup | rclone --config ~/.rclone.conf --links rcat my-remote:/archive-test.tgz
apt list --installed | gzip -c - | rclone --config ~/.rclone.conf --links rcat my-remote:/apt-list.gz
sudo systemctl start xxx.service
```
## mysqldump sample : pipe
mysqldump pipe to rclone
```python
import os
from rclone_backup.Backup import Backup
db_host, db_user, db_pass, db_name = ['localhost', 'mysql', 'password', 'db_name']
config = {
'backup_pair':[
[f'mysqldump --single-transaction -h {db_host} -u {db_user} --password={db_pass} {db_name}'
'| gzip -',
'my-remote:/mysqldump.gz', 'pipe'],
]
}
def main():
bk = Backup(dry_run=(os.environ.get('dry_run') or False))
bk.add_backup_list(config['backup_pair'])
bk.start(verbose=(os.environ.get('verbose') or False))
if __name__ == '__main__':
main()
main()
```